blob: b9e483d9031e58959fe30d0ef13a3ddc03b6a204 [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;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500120 unsigned int allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500121 /* time when first request from queue completed and slice started. */
122 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200123 unsigned long slice_end;
124 long slice_resid;
125 unsigned int slice_dispatch;
126
127 /* pending metadata requests */
128 int meta_pending;
129 /* number of requests that are on the dispatch list or inside driver */
130 int dispatched;
131
132 /* io prio of this group */
133 unsigned short ioprio, org_ioprio;
134 unsigned short ioprio_class, org_ioprio_class;
135
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400136 unsigned int seek_samples;
137 u64 seek_total;
138 sector_t seek_mean;
139 sector_t last_request_pos;
Jeff Moyere6c5bc72009-10-23 17:14:52 -0400140 unsigned long seeky_start;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400141
Jens Axboe6118b702009-06-30 09:34:12 +0200142 pid_t pid;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400143
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100144 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400145 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500146 struct cfq_group *cfqg;
Vivek Goyal22084192009-12-03 12:59:49 -0500147 /* Sectors dispatched in current dispatch round */
148 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200149};
150
151/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100152 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100153 * IDLE is handled separately, so it has negative index
154 */
155enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100156 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500157 RT_WORKLOAD = 1,
158 IDLE_WORKLOAD = 2,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100159};
160
161/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100162 * Second index in the service_trees.
163 */
164enum wl_type_t {
165 ASYNC_WORKLOAD = 0,
166 SYNC_NOIDLE_WORKLOAD = 1,
167 SYNC_WORKLOAD = 2
168};
169
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500170/* This is per cgroup per device grouping structure */
171struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500172 /* group service_tree member */
173 struct rb_node rb_node;
174
175 /* group service_tree key */
176 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500177 unsigned int weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500178 bool on_st;
179
180 /* number of cfqq currently on this group */
181 int nr_cfqq;
182
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500183 /* Per group busy queus average. Useful for workload slice calc. */
184 unsigned int busy_queues_avg[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200185 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100186 * rr lists of queues with requests, onle rr for each priority class.
187 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200188 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100189 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100190 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500191
192 unsigned long saved_workload_slice;
193 enum wl_type_t saved_workload;
194 enum wl_prio_t saved_serving_prio;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500195 struct blkio_group blkg;
196#ifdef CONFIG_CFQ_GROUP_IOSCHED
197 struct hlist_node cfqd_node;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500198 atomic_t ref;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500199#endif
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500200};
201
202/*
203 * Per block device queue structure
204 */
205struct cfq_data {
206 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500207 /* Root service tree for cfq_groups */
208 struct cfq_rb_root grp_service_tree;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500209 struct cfq_group root_group;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500210 /* Number of active cfq groups on group service tree */
211 int nr_groups;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500212
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100213 /*
214 * The priority currently being served
215 */
216 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100217 enum wl_type_t serving_type;
218 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500219 struct cfq_group *serving_group;
Corrado Zoccolo8e550632009-11-26 10:02:58 +0100220 bool noidle_tree_requires_idle;
Jens Axboea36e71f2009-04-15 12:15:11 +0200221
222 /*
223 * Each priority tree is sorted by next_request position. These
224 * trees are used when determining if two or more queues are
225 * interleaving requests (see cfq_close_cooperator).
226 */
227 struct rb_root prio_trees[CFQ_PRIO_LISTS];
228
Jens Axboe22e2c502005-06-27 10:55:12 +0200229 unsigned int busy_queues;
230
Jens Axboe5ad531d2009-07-03 12:57:48 +0200231 int rq_in_driver[2];
Jens Axboe3ed9a292007-04-23 08:33:33 +0200232 int sync_flight;
Aaron Carroll45333d52008-08-26 15:52:36 +0200233
234 /*
235 * queue-depth detection
236 */
237 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200238 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100239 /*
240 * hw_tag can be
241 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
242 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
243 * 0 => no NCQ
244 */
245 int hw_tag_est_depth;
246 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200247
248 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200249 * idle window management
250 */
251 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200252 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200253
254 struct cfq_queue *active_queue;
255 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200256
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200257 /*
258 * async queue for each priority case
259 */
260 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
261 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200262
Jens Axboe6d048f52007-04-25 12:44:27 +0200263 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * tunables, see top of file
267 */
268 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200269 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int cfq_back_penalty;
271 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200272 unsigned int cfq_slice[2];
273 unsigned int cfq_slice_async_rq;
274 unsigned int cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200275 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500276
277 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jens Axboe6118b702009-06-30 09:34:12 +0200279 /*
280 * Fallback dummy cfqq for extreme OOM conditions
281 */
282 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200283
284 unsigned long last_end_sync_rq;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500285
286 /* List of cfq groups being managed on this device*/
287 struct hlist_head cfqg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
Vivek Goyal25fb5162009-12-03 12:59:46 -0500290static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
291
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500292static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
293 enum wl_prio_t prio,
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100294 enum wl_type_t type,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100295 struct cfq_data *cfqd)
296{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500297 if (!cfqg)
298 return NULL;
299
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100300 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500301 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100302
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500303 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100304}
305
Jens Axboe3b181522005-06-27 10:56:24 +0200306enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100307 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
308 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200309 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d742007-01-19 11:35:30 +1100310 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d742007-01-19 11:35:30 +1100311 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
312 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
313 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100314 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200315 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400316 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100317 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500318 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
319 CFQ_CFQQ_FLAG_wait_busy_done, /* Got new request. Expire the queue */
Jens Axboe3b181522005-06-27 10:56:24 +0200320};
321
322#define CFQ_CFQQ_FNS(name) \
323static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
324{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100325 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200326} \
327static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
328{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100329 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200330} \
331static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
332{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100333 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200334}
335
336CFQ_CFQQ_FNS(on_rr);
337CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200338CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200339CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200340CFQ_CFQQ_FNS(fifo_expire);
341CFQ_CFQQ_FNS(idle_window);
342CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100343CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200344CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200345CFQ_CFQQ_FNS(coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100346CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500347CFQ_CFQQ_FNS(wait_busy);
348CFQ_CFQQ_FNS(wait_busy_done);
Jens Axboe3b181522005-06-27 10:56:24 +0200349#undef CFQ_CFQQ_FNS
350
Vivek Goyal2868ef72009-12-03 12:59:48 -0500351#ifdef CONFIG_DEBUG_CFQ_IOSCHED
352#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
353 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
354 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
355 blkg_path(&(cfqq)->cfqg->blkg), ##args);
356
357#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
358 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
359 blkg_path(&(cfqg)->blkg), ##args); \
360
361#else
Jens Axboe7b679132008-05-30 12:23:07 +0200362#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
363 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500364#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
365#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200366#define cfq_log(cfqd, fmt, args...) \
367 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
368
Vivek Goyal615f0252009-12-03 12:59:39 -0500369/* Traverses through cfq group service trees */
370#define for_each_cfqg_st(cfqg, i, j, st) \
371 for (i = 0; i <= IDLE_WORKLOAD; i++) \
372 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
373 : &cfqg->service_tree_idle; \
374 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
375 (i == IDLE_WORKLOAD && j == 0); \
376 j++, st = i < IDLE_WORKLOAD ? \
377 &cfqg->service_trees[i][j]: NULL) \
378
379
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100380static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
381{
382 if (cfq_class_idle(cfqq))
383 return IDLE_WORKLOAD;
384 if (cfq_class_rt(cfqq))
385 return RT_WORKLOAD;
386 return BE_WORKLOAD;
387}
388
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100389
390static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
391{
392 if (!cfq_cfqq_sync(cfqq))
393 return ASYNC_WORKLOAD;
394 if (!cfq_cfqq_idle_window(cfqq))
395 return SYNC_NOIDLE_WORKLOAD;
396 return SYNC_WORKLOAD;
397}
398
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500399static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
400 struct cfq_data *cfqd,
401 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100402{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500403 if (wl == IDLE_WORKLOAD)
404 return cfqg->service_tree_idle.count;
405
406 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
407 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
408 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100409}
410
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500411static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
412 struct cfq_group *cfqg)
413{
414 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
415 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
416}
417
Jens Axboe165125e2007-07-24 09:28:11 +0200418static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200419static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100420 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100421static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200422 struct io_context *);
423
Jens Axboe5ad531d2009-07-03 12:57:48 +0200424static inline int rq_in_driver(struct cfq_data *cfqd)
425{
426 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
427}
428
Vasily Tarasov91fac312007-04-25 12:29:51 +0200429static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200430 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200431{
Jens Axboea6151c32009-10-07 20:02:57 +0200432 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200433}
434
435static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200436 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200437{
Jens Axboea6151c32009-10-07 20:02:57 +0200438 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200439}
440
441/*
442 * We regard a request as SYNC, if it's either a read or has the SYNC bit
443 * set (in which case it could also be direct WRITE).
444 */
Jens Axboea6151c32009-10-07 20:02:57 +0200445static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200446{
Jens Axboea6151c32009-10-07 20:02:57 +0200447 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700451 * scheduler run of queue, if there are requests pending and no one in the
452 * driver that will restart queueing
453 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200454static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700455{
Jens Axboe7b679132008-05-30 12:23:07 +0200456 if (cfqd->busy_queues) {
457 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200458 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200459 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700460}
461
Jens Axboe165125e2007-07-24 09:28:11 +0200462static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700463{
464 struct cfq_data *cfqd = q->elevator->elevator_data;
465
Vivek Goyalf04a6422009-12-03 12:59:40 -0500466 return !cfqd->rq_queued;
Andrew Morton99f95e52005-06-27 20:14:05 -0700467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100470 * Scale schedule slice based on io priority. Use the sync time slice only
471 * if a queue is marked sync and has sync io queued. A sync queue with async
472 * io only, should not get full sync slice length.
473 */
Jens Axboea6151c32009-10-07 20:02:57 +0200474static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200475 unsigned short prio)
476{
477 const int base_slice = cfqd->cfq_slice[sync];
478
479 WARN_ON(prio >= IOPRIO_BE_NR);
480
481 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
482}
483
Jens Axboe44f7c162007-01-19 11:51:58 +1100484static inline int
485cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
486{
Jens Axboed9e76202007-04-20 14:27:50 +0200487 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100488}
489
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500490static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
491{
492 u64 d = delta << CFQ_SERVICE_SHIFT;
493
494 d = d * BLKIO_WEIGHT_DEFAULT;
495 do_div(d, cfqg->weight);
496 return d;
497}
498
499static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
500{
501 s64 delta = (s64)(vdisktime - min_vdisktime);
502 if (delta > 0)
503 min_vdisktime = vdisktime;
504
505 return min_vdisktime;
506}
507
508static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
509{
510 s64 delta = (s64)(vdisktime - min_vdisktime);
511 if (delta < 0)
512 min_vdisktime = vdisktime;
513
514 return min_vdisktime;
515}
516
517static void update_min_vdisktime(struct cfq_rb_root *st)
518{
519 u64 vdisktime = st->min_vdisktime;
520 struct cfq_group *cfqg;
521
522 if (st->active) {
523 cfqg = rb_entry_cfqg(st->active);
524 vdisktime = cfqg->vdisktime;
525 }
526
527 if (st->left) {
528 cfqg = rb_entry_cfqg(st->left);
529 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
530 }
531
532 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
533}
534
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100535/*
536 * get averaged number of queues of RT/BE priority.
537 * average is updated, with a formula that gives more weight to higher numbers,
538 * to quickly follows sudden increases and decrease slowly
539 */
540
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500541static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
542 struct cfq_group *cfqg, bool rt)
Jens Axboe58696192009-10-28 09:27:07 +0100543{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100544 unsigned min_q, max_q;
545 unsigned mult = cfq_hist_divisor - 1;
546 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500547 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100548
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500549 min_q = min(cfqg->busy_queues_avg[rt], busy);
550 max_q = max(cfqg->busy_queues_avg[rt], busy);
551 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100552 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500553 return cfqg->busy_queues_avg[rt];
554}
555
556static inline unsigned
557cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
558{
559 struct cfq_rb_root *st = &cfqd->grp_service_tree;
560
561 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100562}
563
Jens Axboe44f7c162007-01-19 11:51:58 +1100564static inline void
565cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
566{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100567 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
568 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500569 /*
570 * interested queues (we consider only the ones with the same
571 * priority class in the cfq group)
572 */
573 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
574 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100575 unsigned sync_slice = cfqd->cfq_slice[1];
576 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500577 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
578
579 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100580 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
581 /* scale low_slice according to IO priority
582 * and sync vs async */
583 unsigned low_slice =
584 min(slice, base_low_slice * slice / sync_slice);
585 /* the adapted slice value is scaled to fit all iqs
586 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500587 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100588 low_slice);
589 }
590 }
Vivek Goyaldae739e2009-12-03 12:59:45 -0500591 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100592 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500593 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200594 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100595}
596
597/*
598 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
599 * isn't valid until the first request from the dispatch is activated
600 * and the slice time set.
601 */
Jens Axboea6151c32009-10-07 20:02:57 +0200602static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100603{
604 if (cfq_cfqq_slice_new(cfqq))
605 return 0;
606 if (time_before(jiffies, cfqq->slice_end))
607 return 0;
608
609 return 1;
610}
611
612/*
Jens Axboe5e705372006-07-13 12:39:25 +0200613 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200615 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Jens Axboe5e705372006-07-13 12:39:25 +0200617static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100618cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100620 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200622#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
623#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
624 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Jens Axboe5e705372006-07-13 12:39:25 +0200626 if (rq1 == NULL || rq1 == rq2)
627 return rq2;
628 if (rq2 == NULL)
629 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200630
Jens Axboe5e705372006-07-13 12:39:25 +0200631 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
632 return rq1;
633 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
634 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200635 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
636 return rq1;
637 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
638 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Tejun Heo83096eb2009-05-07 22:24:39 +0900640 s1 = blk_rq_pos(rq1);
641 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * by definition, 1KiB is 2 sectors
645 */
646 back_max = cfqd->cfq_back_max * 2;
647
648 /*
649 * Strict one way elevator _except_ in the case where we allow
650 * short backward seeks which are biased as twice the cost of a
651 * similar forward seek.
652 */
653 if (s1 >= last)
654 d1 = s1 - last;
655 else if (s1 + back_max >= last)
656 d1 = (last - s1) * cfqd->cfq_back_penalty;
657 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200658 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (s2 >= last)
661 d2 = s2 - last;
662 else if (s2 + back_max >= last)
663 d2 = (last - s2) * cfqd->cfq_back_penalty;
664 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200665 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Andreas Mohre8a99052006-03-28 08:59:49 +0200669 /*
670 * By doing switch() on the bit mask "wrap" we avoid having to
671 * check two variables for all permutations: --> faster!
672 */
673 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200674 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200675 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200676 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200677 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200678 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200679 else {
680 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200681 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200682 else
Jens Axboe5e705372006-07-13 12:39:25 +0200683 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200684 }
685
686 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200687 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200688 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200689 return rq2;
690 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200691 default:
692 /*
693 * Since both rqs are wrapped,
694 * start with the one that's further behind head
695 * (--> only *one* back seek required),
696 * since back seek takes more time than forward.
697 */
698 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200699 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 else
Jens Axboe5e705372006-07-13 12:39:25 +0200701 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703}
704
Jens Axboe498d3aa2007-04-26 12:54:48 +0200705/*
706 * The below is leftmost cache rbtree addon
707 */
Jens Axboe08717142008-01-28 11:38:15 +0100708static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200709{
Vivek Goyal615f0252009-12-03 12:59:39 -0500710 /* Service tree is empty */
711 if (!root->count)
712 return NULL;
713
Jens Axboecc09e292007-04-26 12:53:50 +0200714 if (!root->left)
715 root->left = rb_first(&root->rb);
716
Jens Axboe08717142008-01-28 11:38:15 +0100717 if (root->left)
718 return rb_entry(root->left, struct cfq_queue, rb_node);
719
720 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200721}
722
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500723static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
724{
725 if (!root->left)
726 root->left = rb_first(&root->rb);
727
728 if (root->left)
729 return rb_entry_cfqg(root->left);
730
731 return NULL;
732}
733
Jens Axboea36e71f2009-04-15 12:15:11 +0200734static void rb_erase_init(struct rb_node *n, struct rb_root *root)
735{
736 rb_erase(n, root);
737 RB_CLEAR_NODE(n);
738}
739
Jens Axboecc09e292007-04-26 12:53:50 +0200740static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
741{
742 if (root->left == n)
743 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200744 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100745 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * would be nice to take fifo expire time into account as well
750 */
Jens Axboe5e705372006-07-13 12:39:25 +0200751static struct request *
752cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
753 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Jens Axboe21183b02006-07-13 12:33:14 +0200755 struct rb_node *rbnext = rb_next(&last->rb_node);
756 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200757 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jens Axboe21183b02006-07-13 12:33:14 +0200759 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200762 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200765 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200766 else {
767 rbnext = rb_first(&cfqq->sort_list);
768 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200769 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100772 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Jens Axboed9e76202007-04-20 14:27:50 +0200775static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
776 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Jens Axboed9e76202007-04-20 14:27:50 +0200778 /*
779 * just an approximation, should be ok.
780 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500781 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100782 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200783}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500785static inline s64
786cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
787{
788 return cfqg->vdisktime - st->min_vdisktime;
789}
790
791static void
792__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
793{
794 struct rb_node **node = &st->rb.rb_node;
795 struct rb_node *parent = NULL;
796 struct cfq_group *__cfqg;
797 s64 key = cfqg_key(st, cfqg);
798 int left = 1;
799
800 while (*node != NULL) {
801 parent = *node;
802 __cfqg = rb_entry_cfqg(parent);
803
804 if (key < cfqg_key(st, __cfqg))
805 node = &parent->rb_left;
806 else {
807 node = &parent->rb_right;
808 left = 0;
809 }
810 }
811
812 if (left)
813 st->left = &cfqg->rb_node;
814
815 rb_link_node(&cfqg->rb_node, parent, node);
816 rb_insert_color(&cfqg->rb_node, &st->rb);
817}
818
819static void
820cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
821{
822 struct cfq_rb_root *st = &cfqd->grp_service_tree;
823 struct cfq_group *__cfqg;
824 struct rb_node *n;
825
826 cfqg->nr_cfqq++;
827 if (cfqg->on_st)
828 return;
829
830 /*
831 * Currently put the group at the end. Later implement something
832 * so that groups get lesser vtime based on their weights, so that
833 * if group does not loose all if it was not continously backlogged.
834 */
835 n = rb_last(&st->rb);
836 if (n) {
837 __cfqg = rb_entry_cfqg(n);
838 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
839 } else
840 cfqg->vdisktime = st->min_vdisktime;
841
842 __cfq_group_service_tree_add(st, cfqg);
843 cfqg->on_st = true;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500844 cfqd->nr_groups++;
845 st->total_weight += cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500846}
847
848static void
849cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
850{
851 struct cfq_rb_root *st = &cfqd->grp_service_tree;
852
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500853 if (st->active == &cfqg->rb_node)
854 st->active = NULL;
855
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500856 BUG_ON(cfqg->nr_cfqq < 1);
857 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500858
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500859 /* If there are other cfq queues under this group, don't delete it */
860 if (cfqg->nr_cfqq)
861 return;
862
Vivek Goyal2868ef72009-12-03 12:59:48 -0500863 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500864 cfqg->on_st = false;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500865 cfqd->nr_groups--;
866 st->total_weight -= cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500867 if (!RB_EMPTY_NODE(&cfqg->rb_node))
868 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500869 cfqg->saved_workload_slice = 0;
Vivek Goyal22084192009-12-03 12:59:49 -0500870 blkiocg_update_blkio_group_dequeue_stats(&cfqg->blkg, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500871}
872
873static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
874{
Vivek Goyalf75edf22009-12-03 12:59:53 -0500875 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500876
877 /*
878 * Queue got expired before even a single request completed or
879 * got expired immediately after first request completion.
880 */
881 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
882 /*
883 * Also charge the seek time incurred to the group, otherwise
884 * if there are mutiple queues in the group, each can dispatch
885 * a single request on seeky media and cause lots of seek time
886 * and group will never know it.
887 */
888 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
889 1);
890 } else {
891 slice_used = jiffies - cfqq->slice_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500892 if (slice_used > cfqq->allocated_slice)
893 slice_used = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500894 }
895
Vivek Goyal22084192009-12-03 12:59:49 -0500896 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u sect=%lu", slice_used,
897 cfqq->nr_sectors);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500898 return slice_used;
899}
900
901static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
902 struct cfq_queue *cfqq)
903{
904 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500905 unsigned int used_sl, charge_sl;
906 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
907 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500908
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500909 BUG_ON(nr_sync < 0);
910 used_sl = charge_sl = cfq_cfqq_slice_usage(cfqq);
911
912 if (!cfq_cfqq_sync(cfqq) && !nr_sync)
913 charge_sl = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500914
915 /* Can't update vdisktime while group is on service tree */
916 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500917 cfqg->vdisktime += cfq_scale_slice(charge_sl, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500918 __cfq_group_service_tree_add(st, cfqg);
919
920 /* This group is being expired. Save the context */
921 if (time_after(cfqd->workload_expires, jiffies)) {
922 cfqg->saved_workload_slice = cfqd->workload_expires
923 - jiffies;
924 cfqg->saved_workload = cfqd->serving_type;
925 cfqg->saved_serving_prio = cfqd->serving_prio;
926 } else
927 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -0500928
929 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
930 st->min_vdisktime);
Vivek Goyal22084192009-12-03 12:59:49 -0500931 blkiocg_update_blkio_group_stats(&cfqg->blkg, used_sl,
932 cfqq->nr_sectors);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500933}
934
Vivek Goyal25fb5162009-12-03 12:59:46 -0500935#ifdef CONFIG_CFQ_GROUP_IOSCHED
936static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
937{
938 if (blkg)
939 return container_of(blkg, struct cfq_group, blkg);
940 return NULL;
941}
942
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500943void
944cfq_update_blkio_group_weight(struct blkio_group *blkg, unsigned int weight)
945{
946 cfqg_of_blkg(blkg)->weight = weight;
947}
948
Vivek Goyal25fb5162009-12-03 12:59:46 -0500949static struct cfq_group *
950cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
951{
952 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
953 struct cfq_group *cfqg = NULL;
954 void *key = cfqd;
955 int i, j;
956 struct cfq_rb_root *st;
Vivek Goyal22084192009-12-03 12:59:49 -0500957 struct backing_dev_info *bdi = &cfqd->queue->backing_dev_info;
958 unsigned int major, minor;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500959
960 /* Do we need to take this reference */
961 if (!css_tryget(&blkcg->css))
962 return NULL;;
963
964 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
965 if (cfqg || !create)
966 goto done;
967
968 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
969 if (!cfqg)
970 goto done;
971
972 cfqg->weight = blkcg->weight;
973 for_each_cfqg_st(cfqg, i, j, st)
974 *st = CFQ_RB_ROOT;
975 RB_CLEAR_NODE(&cfqg->rb_node);
976
Vivek Goyalb1c35762009-12-03 12:59:47 -0500977 /*
978 * Take the initial reference that will be released on destroy
979 * This can be thought of a joint reference by cgroup and
980 * elevator which will be dropped by either elevator exit
981 * or cgroup deletion path depending on who is exiting first.
982 */
983 atomic_set(&cfqg->ref, 1);
984
Vivek Goyal25fb5162009-12-03 12:59:46 -0500985 /* Add group onto cgroup list */
Vivek Goyal22084192009-12-03 12:59:49 -0500986 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
987 blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd,
988 MKDEV(major, minor));
Vivek Goyal25fb5162009-12-03 12:59:46 -0500989
990 /* Add group on cfqd list */
991 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
992
993done:
994 css_put(&blkcg->css);
995 return cfqg;
996}
997
998/*
999 * Search for the cfq group current task belongs to. If create = 1, then also
1000 * create the cfq group if it does not exist. request_queue lock must be held.
1001 */
1002static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1003{
1004 struct cgroup *cgroup;
1005 struct cfq_group *cfqg = NULL;
1006
1007 rcu_read_lock();
1008 cgroup = task_cgroup(current, blkio_subsys_id);
1009 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
1010 if (!cfqg && create)
1011 cfqg = &cfqd->root_group;
1012 rcu_read_unlock();
1013 return cfqg;
1014}
1015
1016static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1017{
1018 /* Currently, all async queues are mapped to root group */
1019 if (!cfq_cfqq_sync(cfqq))
1020 cfqg = &cfqq->cfqd->root_group;
1021
1022 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001023 /* cfqq reference on cfqg */
1024 atomic_inc(&cfqq->cfqg->ref);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001025}
Vivek Goyalb1c35762009-12-03 12:59:47 -05001026
1027static void cfq_put_cfqg(struct cfq_group *cfqg)
1028{
1029 struct cfq_rb_root *st;
1030 int i, j;
1031
1032 BUG_ON(atomic_read(&cfqg->ref) <= 0);
1033 if (!atomic_dec_and_test(&cfqg->ref))
1034 return;
1035 for_each_cfqg_st(cfqg, i, j, st)
1036 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
1037 kfree(cfqg);
1038}
1039
1040static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1041{
1042 /* Something wrong if we are trying to remove same group twice */
1043 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1044
1045 hlist_del_init(&cfqg->cfqd_node);
1046
1047 /*
1048 * Put the reference taken at the time of creation so that when all
1049 * queues are gone, group can be destroyed.
1050 */
1051 cfq_put_cfqg(cfqg);
1052}
1053
1054static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1055{
1056 struct hlist_node *pos, *n;
1057 struct cfq_group *cfqg;
1058
1059 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1060 /*
1061 * If cgroup removal path got to blk_group first and removed
1062 * it from cgroup list, then it will take care of destroying
1063 * cfqg also.
1064 */
1065 if (!blkiocg_del_blkio_group(&cfqg->blkg))
1066 cfq_destroy_cfqg(cfqd, cfqg);
1067 }
1068}
1069
1070/*
1071 * Blk cgroup controller notification saying that blkio_group object is being
1072 * delinked as associated cgroup object is going away. That also means that
1073 * no new IO will come in this group. So get rid of this group as soon as
1074 * any pending IO in the group is finished.
1075 *
1076 * This function is called under rcu_read_lock(). key is the rcu protected
1077 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1078 * read lock.
1079 *
1080 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1081 * it should not be NULL as even if elevator was exiting, cgroup deltion
1082 * path got to it first.
1083 */
1084void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1085{
1086 unsigned long flags;
1087 struct cfq_data *cfqd = key;
1088
1089 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1090 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1091 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1092}
1093
Vivek Goyal25fb5162009-12-03 12:59:46 -05001094#else /* GROUP_IOSCHED */
1095static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1096{
1097 return &cfqd->root_group;
1098}
1099static inline void
1100cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1101 cfqq->cfqg = cfqg;
1102}
1103
Vivek Goyalb1c35762009-12-03 12:59:47 -05001104static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1105static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1106
Vivek Goyal25fb5162009-12-03 12:59:46 -05001107#endif /* GROUP_IOSCHED */
1108
Jens Axboe498d3aa2007-04-26 12:54:48 +02001109/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001110 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa2007-04-26 12:54:48 +02001111 * requests waiting to be processed. It is sorted in the order that
1112 * we will service the queues.
1113 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001114static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001115 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001116{
Jens Axboe08717142008-01-28 11:38:15 +01001117 struct rb_node **p, *parent;
1118 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001119 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001120 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa2007-04-26 12:54:48 +02001121 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001122 int new_cfqq = 1;
Jens Axboed9e76202007-04-20 14:27:50 +02001123
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001124 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
1125 cfqq_type(cfqq), cfqd);
Jens Axboe08717142008-01-28 11:38:15 +01001126 if (cfq_class_idle(cfqq)) {
1127 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001128 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001129 if (parent && parent != &cfqq->rb_node) {
1130 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1131 rb_key += __cfqq->rb_key;
1132 } else
1133 rb_key += jiffies;
1134 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001135 /*
1136 * Get our rb key offset. Subtract any residual slice
1137 * value carried from last service. A negative resid
1138 * count indicates slice overrun, and this should position
1139 * the next service time further away in the tree.
1140 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001141 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001142 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001143 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001144 } else {
1145 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001146 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001147 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1148 }
Jens Axboed9e76202007-04-20 14:27:50 +02001149
1150 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001151 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001152 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001153 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001154 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001155 if (rb_key == cfqq->rb_key &&
1156 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001157 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001158
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001159 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1160 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001161 }
Jens Axboed9e76202007-04-20 14:27:50 +02001162
Jens Axboe498d3aa2007-04-26 12:54:48 +02001163 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001164 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001165 cfqq->service_tree = service_tree;
1166 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001167 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001168 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001169
Jens Axboed9e76202007-04-20 14:27:50 +02001170 parent = *p;
1171 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1172
Jens Axboe0c534e02007-04-18 20:01:57 +02001173 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001174 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001175 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001176 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001177 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001178 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001179 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001180 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001181 }
Jens Axboe67060e32007-04-18 20:13:32 +02001182
1183 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001184 }
1185
Jens Axboecc09e292007-04-26 12:53:50 +02001186 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001187 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001188
Jens Axboed9e76202007-04-20 14:27:50 +02001189 cfqq->rb_key = rb_key;
1190 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001191 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1192 service_tree->count++;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001193 if (add_front || !new_cfqq)
1194 return;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001195 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Jens Axboea36e71f2009-04-15 12:15:11 +02001198static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001199cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1200 sector_t sector, struct rb_node **ret_parent,
1201 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001202{
Jens Axboea36e71f2009-04-15 12:15:11 +02001203 struct rb_node **p, *parent;
1204 struct cfq_queue *cfqq = NULL;
1205
1206 parent = NULL;
1207 p = &root->rb_node;
1208 while (*p) {
1209 struct rb_node **n;
1210
1211 parent = *p;
1212 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1213
1214 /*
1215 * Sort strictly based on sector. Smallest to the left,
1216 * largest to the right.
1217 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001218 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001219 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001220 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001221 n = &(*p)->rb_left;
1222 else
1223 break;
1224 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001225 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001226 }
1227
1228 *ret_parent = parent;
1229 if (rb_link)
1230 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001231 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001232}
1233
1234static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1235{
Jens Axboea36e71f2009-04-15 12:15:11 +02001236 struct rb_node **p, *parent;
1237 struct cfq_queue *__cfqq;
1238
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001239 if (cfqq->p_root) {
1240 rb_erase(&cfqq->p_node, cfqq->p_root);
1241 cfqq->p_root = NULL;
1242 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001243
1244 if (cfq_class_idle(cfqq))
1245 return;
1246 if (!cfqq->next_rq)
1247 return;
1248
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001249 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001250 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1251 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001252 if (!__cfqq) {
1253 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001254 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1255 } else
1256 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001257}
1258
Jens Axboe498d3aa2007-04-26 12:54:48 +02001259/*
1260 * Update cfqq's position in the service tree.
1261 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001262static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001263{
Jens Axboe6d048f52007-04-25 12:44:27 +02001264 /*
1265 * Resorting requires the cfqq to be on the RR list already.
1266 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001267 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001268 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001269 cfq_prio_tree_add(cfqd, cfqq);
1270 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/*
1274 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001275 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Jens Axboefebffd62008-01-28 13:19:43 +01001277static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jens Axboe7b679132008-05-30 12:23:07 +02001279 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001280 BUG_ON(cfq_cfqq_on_rr(cfqq));
1281 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 cfqd->busy_queues++;
1283
Jens Axboeedd75ff2007-04-19 12:03:34 +02001284 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Jens Axboe498d3aa2007-04-26 12:54:48 +02001287/*
1288 * Called when the cfqq no longer has requests pending, remove it from
1289 * the service tree.
1290 */
Jens Axboefebffd62008-01-28 13:19:43 +01001291static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Jens Axboe7b679132008-05-30 12:23:07 +02001293 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001294 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1295 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001297 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1298 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1299 cfqq->service_tree = NULL;
1300 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001301 if (cfqq->p_root) {
1302 rb_erase(&cfqq->p_node, cfqq->p_root);
1303 cfqq->p_root = NULL;
1304 }
Jens Axboed9e76202007-04-20 14:27:50 +02001305
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001306 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 BUG_ON(!cfqd->busy_queues);
1308 cfqd->busy_queues--;
1309}
1310
1311/*
1312 * rb tree support functions
1313 */
Jens Axboefebffd62008-01-28 13:19:43 +01001314static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Jens Axboe5e705372006-07-13 12:39:25 +02001316 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001317 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jens Axboeb4878f22005-10-20 16:42:29 +02001319 BUG_ON(!cfqq->queued[sync]);
1320 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jens Axboe5e705372006-07-13 12:39:25 +02001322 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Vivek Goyalf04a6422009-12-03 12:59:40 -05001324 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1325 /*
1326 * Queue will be deleted from service tree when we actually
1327 * expire it later. Right now just remove it from prio tree
1328 * as it is empty.
1329 */
1330 if (cfqq->p_root) {
1331 rb_erase(&cfqq->p_node, cfqq->p_root);
1332 cfqq->p_root = NULL;
1333 }
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Jens Axboe5e705372006-07-13 12:39:25 +02001337static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Jens Axboe5e705372006-07-13 12:39:25 +02001339 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +02001341 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jens Axboe5380a102006-07-13 12:37:56 +02001343 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
1346 * looks a little odd, but the first insert might return an alias.
1347 * if that happens, put the alias on the dispatch list
1348 */
Jens Axboe21183b02006-07-13 12:33:14 +02001349 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +02001350 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001351
1352 if (!cfq_cfqq_on_rr(cfqq))
1353 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001354
1355 /*
1356 * check if this request is a better next-serve candidate
1357 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001358 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001359 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001360
1361 /*
1362 * adjust priority tree position, if ->next_rq changes
1363 */
1364 if (prev != cfqq->next_rq)
1365 cfq_prio_tree_add(cfqd, cfqq);
1366
Jens Axboe5044eed2007-04-25 11:53:48 +02001367 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Jens Axboefebffd62008-01-28 13:19:43 +01001370static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Jens Axboe5380a102006-07-13 12:37:56 +02001372 elv_rb_del(&cfqq->sort_list, rq);
1373 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +02001374 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Jens Axboe206dc692006-03-28 13:03:44 +02001377static struct request *
1378cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Jens Axboe206dc692006-03-28 13:03:44 +02001380 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001381 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001382 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Jens Axboe4ac845a2008-01-24 08:44:49 +01001384 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001385 if (!cic)
1386 return NULL;
1387
1388 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001389 if (cfqq) {
1390 sector_t sector = bio->bi_sector + bio_sectors(bio);
1391
Jens Axboe21183b02006-07-13 12:33:14 +02001392 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return NULL;
1396}
1397
Jens Axboe165125e2007-07-24 09:28:11 +02001398static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001399{
1400 struct cfq_data *cfqd = q->elevator->elevator_data;
1401
Jens Axboe5ad531d2009-07-03 12:57:48 +02001402 cfqd->rq_in_driver[rq_is_sync(rq)]++;
Jens Axboe7b679132008-05-30 12:23:07 +02001403 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001404 rq_in_driver(cfqd));
Jens Axboe25776e32006-06-01 10:12:26 +02001405
Tejun Heo5b936292009-05-07 22:24:38 +09001406 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001407}
1408
Jens Axboe165125e2007-07-24 09:28:11 +02001409static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Jens Axboe22e2c502005-06-27 10:55:12 +02001411 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5ad531d2009-07-03 12:57:48 +02001412 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Jens Axboe5ad531d2009-07-03 12:57:48 +02001414 WARN_ON(!cfqd->rq_in_driver[sync]);
1415 cfqd->rq_in_driver[sync]--;
Jens Axboe7b679132008-05-30 12:23:07 +02001416 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +02001417 rq_in_driver(cfqd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Jens Axboeb4878f22005-10-20 16:42:29 +02001420static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Jens Axboe5e705372006-07-13 12:39:25 +02001422 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001423
Jens Axboe5e705372006-07-13 12:39:25 +02001424 if (cfqq->next_rq == rq)
1425 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Jens Axboeb4878f22005-10-20 16:42:29 +02001427 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001428 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001429
Aaron Carroll45333d52008-08-26 15:52:36 +02001430 cfqq->cfqd->rq_queued--;
Jens Axboe374f84a2006-07-23 01:42:19 +02001431 if (rq_is_meta(rq)) {
1432 WARN_ON(!cfqq->meta_pending);
1433 cfqq->meta_pending--;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Jens Axboe165125e2007-07-24 09:28:11 +02001437static int cfq_merge(struct request_queue *q, struct request **req,
1438 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct cfq_data *cfqd = q->elevator->elevator_data;
1441 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Jens Axboe206dc692006-03-28 13:03:44 +02001443 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001444 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001445 *req = __rq;
1446 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
1449 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Jens Axboe165125e2007-07-24 09:28:11 +02001452static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001453 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Jens Axboe21183b02006-07-13 12:33:14 +02001455 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001456 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Jens Axboe5e705372006-07-13 12:39:25 +02001458 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462static void
Jens Axboe165125e2007-07-24 09:28:11 +02001463cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct request *next)
1465{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001466 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001467 /*
1468 * reposition in fifo if next is older than rq
1469 */
1470 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001471 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001472 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001473 rq_set_fifo_time(rq, rq_fifo_time(next));
1474 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001475
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001476 if (cfqq->next_rq == next)
1477 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001478 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +02001479}
1480
Jens Axboe165125e2007-07-24 09:28:11 +02001481static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001482 struct bio *bio)
1483{
1484 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001485 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001486 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001487
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001488 /* Deny merge if bio and rq don't belong to same cfq group */
1489 if ((RQ_CFQQ(rq))->cfqg != cfq_get_cfqg(cfqd, 0))
1490 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001491 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001492 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001493 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001494 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001495 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001496
1497 /*
Jens Axboe719d3402006-12-22 09:38:53 +01001498 * Lookup the cfqq that this bio will be queued with. Allow
1499 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001500 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01001501 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001502 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +02001503 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001504
Vasily Tarasov91fac312007-04-25 12:29:51 +02001505 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001506 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001507}
1508
Jens Axboefebffd62008-01-28 13:19:43 +01001509static void __cfq_set_active_queue(struct cfq_data *cfqd,
1510 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001511{
1512 if (cfqq) {
Jens Axboe7b679132008-05-30 12:23:07 +02001513 cfq_log_cfqq(cfqd, cfqq, "set_active");
Vivek Goyaldae739e2009-12-03 12:59:45 -05001514 cfqq->slice_start = 0;
1515 cfqq->dispatch_start = jiffies;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001516 cfqq->allocated_slice = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001517 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001518 cfqq->slice_dispatch = 0;
Vivek Goyal22084192009-12-03 12:59:49 -05001519 cfqq->nr_sectors = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001520
Jens Axboe2f5cb732009-04-07 08:51:19 +02001521 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +02001522 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001523 cfq_clear_cfqq_must_alloc_slice(cfqq);
1524 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +11001525 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001526
1527 del_timer(&cfqd->idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02001528 }
1529
1530 cfqd->active_queue = cfqq;
1531}
1532
1533/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001534 * current cfqq expired its slice (or was too idle), select new one
1535 */
1536static void
1537__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001538 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001539{
Jens Axboe7b679132008-05-30 12:23:07 +02001540 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1541
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001542 if (cfq_cfqq_wait_request(cfqq))
1543 del_timer(&cfqd->idle_slice_timer);
1544
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001545 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001546 cfq_clear_cfqq_wait_busy(cfqq);
1547 cfq_clear_cfqq_wait_busy_done(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001548
1549 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001550 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001551 */
Jens Axboe7b679132008-05-30 12:23:07 +02001552 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +11001553 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001554 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1555 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001556
Vivek Goyaldae739e2009-12-03 12:59:45 -05001557 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
1558
Vivek Goyalf04a6422009-12-03 12:59:40 -05001559 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1560 cfq_del_cfqq_rr(cfqd, cfqq);
1561
Jens Axboeedd75ff2007-04-19 12:03:34 +02001562 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001563
1564 if (cfqq == cfqd->active_queue)
1565 cfqd->active_queue = NULL;
1566
Vivek Goyaldae739e2009-12-03 12:59:45 -05001567 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1568 cfqd->grp_service_tree.active = NULL;
1569
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001570 if (cfqd->active_cic) {
1571 put_io_context(cfqd->active_cic->ioc);
1572 cfqd->active_cic = NULL;
1573 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001574}
1575
Jens Axboea6151c32009-10-07 20:02:57 +02001576static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001577{
1578 struct cfq_queue *cfqq = cfqd->active_queue;
1579
1580 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +02001581 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001582}
1583
Jens Axboe498d3aa2007-04-26 12:54:48 +02001584/*
1585 * Get next queue for service. Unless we have a queue preemption,
1586 * we'll simply select the first cfqq in the service tree.
1587 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001588static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001589{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001590 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001591 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
1592 cfqd->serving_type, cfqd);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001593
Vivek Goyalf04a6422009-12-03 12:59:40 -05001594 if (!cfqd->rq_queued)
1595 return NULL;
1596
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001597 /* There is nothing to dispatch */
1598 if (!service_tree)
1599 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001600 if (RB_EMPTY_ROOT(&service_tree->rb))
1601 return NULL;
1602 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001603}
1604
Vivek Goyalf04a6422009-12-03 12:59:40 -05001605static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1606{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001607 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001608 struct cfq_queue *cfqq;
1609 int i, j;
1610 struct cfq_rb_root *st;
1611
1612 if (!cfqd->rq_queued)
1613 return NULL;
1614
Vivek Goyal25fb5162009-12-03 12:59:46 -05001615 cfqg = cfq_get_next_cfqg(cfqd);
1616 if (!cfqg)
1617 return NULL;
1618
Vivek Goyalf04a6422009-12-03 12:59:40 -05001619 for_each_cfqg_st(cfqg, i, j, st)
1620 if ((cfqq = cfq_rb_first(st)) != NULL)
1621 return cfqq;
1622 return NULL;
1623}
1624
Jens Axboe498d3aa2007-04-26 12:54:48 +02001625/*
1626 * Get and set a new active queue for service.
1627 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001628static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1629 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001630{
Jens Axboee00ef792009-11-04 08:54:55 +01001631 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001632 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001633
Jens Axboe22e2c502005-06-27 10:55:12 +02001634 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001635 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001636}
1637
Jens Axboed9e76202007-04-20 14:27:50 +02001638static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1639 struct request *rq)
1640{
Tejun Heo83096eb2009-05-07 22:24:39 +09001641 if (blk_rq_pos(rq) >= cfqd->last_position)
1642 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001643 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001644 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001645}
1646
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001647#define CFQQ_SEEK_THR 8 * 1024
1648#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001649
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001650static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1651 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001652{
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001653 sector_t sdist = cfqq->seek_mean;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001654
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001655 if (!sample_valid(cfqq->seek_samples))
1656 sdist = CFQQ_SEEK_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001657
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001658 return cfq_dist_from_last(cfqd, rq) <= sdist;
Jens Axboe6d048f52007-04-25 12:44:27 +02001659}
1660
Jens Axboea36e71f2009-04-15 12:15:11 +02001661static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1662 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001663{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001664 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001665 struct rb_node *parent, *node;
1666 struct cfq_queue *__cfqq;
1667 sector_t sector = cfqd->last_position;
1668
1669 if (RB_EMPTY_ROOT(root))
1670 return NULL;
1671
1672 /*
1673 * First, if we find a request starting at the end of the last
1674 * request, choose it.
1675 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001676 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001677 if (__cfqq)
1678 return __cfqq;
1679
1680 /*
1681 * If the exact sector wasn't found, the parent of the NULL leaf
1682 * will contain the closest sector.
1683 */
1684 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001685 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001686 return __cfqq;
1687
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001688 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001689 node = rb_next(&__cfqq->p_node);
1690 else
1691 node = rb_prev(&__cfqq->p_node);
1692 if (!node)
1693 return NULL;
1694
1695 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001696 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001697 return __cfqq;
1698
1699 return NULL;
1700}
1701
1702/*
1703 * cfqd - obvious
1704 * cur_cfqq - passed in so that we don't decide that the current queue is
1705 * closely cooperating with itself.
1706 *
1707 * So, basically we're assuming that that cur_cfqq has dispatched at least
1708 * one request, and that cfqd->last_position reflects a position on the disk
1709 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1710 * assumption.
1711 */
1712static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001713 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001714{
1715 struct cfq_queue *cfqq;
1716
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001717 if (!cfq_cfqq_sync(cur_cfqq))
1718 return NULL;
1719 if (CFQQ_SEEKY(cur_cfqq))
1720 return NULL;
1721
Jens Axboea36e71f2009-04-15 12:15:11 +02001722 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001723 * We should notice if some of the queues are cooperating, eg
1724 * working closely on the same area of the disk. In that case,
1725 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001726 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001727 cfqq = cfqq_close(cfqd, cur_cfqq);
1728 if (!cfqq)
1729 return NULL;
1730
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001731 /* If new queue belongs to different cfq_group, don't choose it */
1732 if (cur_cfqq->cfqg != cfqq->cfqg)
1733 return NULL;
1734
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001735 /*
1736 * It only makes sense to merge sync queues.
1737 */
1738 if (!cfq_cfqq_sync(cfqq))
1739 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001740 if (CFQQ_SEEKY(cfqq))
1741 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001742
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001743 /*
1744 * Do not merge queues of different priority classes
1745 */
1746 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1747 return NULL;
1748
Jens Axboea36e71f2009-04-15 12:15:11 +02001749 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001750}
1751
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001752/*
1753 * Determine whether we should enforce idle window for this queue.
1754 */
1755
1756static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1757{
1758 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001759 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001760
Vivek Goyalf04a6422009-12-03 12:59:40 -05001761 BUG_ON(!service_tree);
1762 BUG_ON(!service_tree->count);
1763
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001764 /* We never do for idle class queues. */
1765 if (prio == IDLE_WORKLOAD)
1766 return false;
1767
1768 /* We do for queues that were marked with idle window flag. */
1769 if (cfq_cfqq_idle_window(cfqq))
1770 return true;
1771
1772 /*
1773 * Otherwise, we do only if they are the last ones
1774 * in their service tree.
1775 */
Vivek Goyalf04a6422009-12-03 12:59:40 -05001776 return service_tree->count == 1;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001777}
1778
Jens Axboe6d048f52007-04-25 12:44:27 +02001779static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001780{
Jens Axboe17926692007-01-19 11:59:30 +11001781 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001782 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001783 unsigned long sl;
1784
Jens Axboea68bbdd2008-09-24 13:03:33 +02001785 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001786 * SSD device without seek penalty, disable idling. But only do so
1787 * for devices that support queuing, otherwise we still have a problem
1788 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001789 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001790 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001791 return;
1792
Jens Axboedd67d052006-06-21 09:36:18 +02001793 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001794 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001795
1796 /*
1797 * idle is disabled, either manually or by past process history
1798 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001799 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
Jens Axboe6d048f52007-04-25 12:44:27 +02001800 return;
1801
Jens Axboe22e2c502005-06-27 10:55:12 +02001802 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001803 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001804 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001805 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001806 return;
1807
1808 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001809 * task has exited, don't wait
1810 */
Jens Axboe206dc692006-03-28 13:03:44 +02001811 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001812 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001813 return;
1814
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001815 /*
1816 * If our average think time is larger than the remaining time
1817 * slice, then don't idle. This avoids overrunning the allotted
1818 * time slice.
1819 */
1820 if (sample_valid(cic->ttime_samples) &&
1821 (cfqq->slice_end - jiffies < cic->ttime_mean))
1822 return;
1823
Jens Axboe3b181522005-06-27 10:56:24 +02001824 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001825
Jens Axboe6d048f52007-04-25 12:44:27 +02001826 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001827
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001828 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001829 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Jens Axboe498d3aa2007-04-26 12:54:48 +02001832/*
1833 * Move request from internal lists to the request queue dispatch list.
1834 */
Jens Axboe165125e2007-07-24 09:28:11 +02001835static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001837 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001838 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001839
Jens Axboe7b679132008-05-30 12:23:07 +02001840 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1841
Jeff Moyer06d21882009-09-11 17:08:59 +02001842 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001843 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001844 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001845 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001846
1847 if (cfq_cfqq_sync(cfqq))
1848 cfqd->sync_flight++;
Vivek Goyal22084192009-12-03 12:59:49 -05001849 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852/*
1853 * return expired entry, or NULL to just start from scratch in rbtree
1854 */
Jens Axboefebffd62008-01-28 13:19:43 +01001855static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Jens Axboe30996f42009-10-05 11:03:39 +02001857 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Jens Axboe3b181522005-06-27 10:56:24 +02001859 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001861
1862 cfq_mark_cfqq_fifo_expire(cfqq);
1863
Jens Axboe89850f72006-07-22 16:48:31 +02001864 if (list_empty(&cfqq->fifo))
1865 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Jens Axboe89850f72006-07-22 16:48:31 +02001867 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001868 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001869 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jens Axboe30996f42009-10-05 11:03:39 +02001871 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001872 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Jens Axboe22e2c502005-06-27 10:55:12 +02001875static inline int
1876cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1877{
1878 const int base_rq = cfqd->cfq_slice_async_rq;
1879
1880 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1881
1882 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1883}
1884
1885/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001886 * Must be called with the queue_lock held.
1887 */
1888static int cfqq_process_refs(struct cfq_queue *cfqq)
1889{
1890 int process_refs, io_refs;
1891
1892 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1893 process_refs = atomic_read(&cfqq->ref) - io_refs;
1894 BUG_ON(process_refs < 0);
1895 return process_refs;
1896}
1897
1898static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1899{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001900 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001901 struct cfq_queue *__cfqq;
1902
1903 /* Avoid a circular list and skip interim queue merges */
1904 while ((__cfqq = new_cfqq->new_cfqq)) {
1905 if (__cfqq == cfqq)
1906 return;
1907 new_cfqq = __cfqq;
1908 }
1909
1910 process_refs = cfqq_process_refs(cfqq);
1911 /*
1912 * If the process for the cfqq has gone away, there is no
1913 * sense in merging the queues.
1914 */
1915 if (process_refs == 0)
1916 return;
1917
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001918 /*
1919 * Merge in the direction of the lesser amount of work.
1920 */
1921 new_process_refs = cfqq_process_refs(new_cfqq);
1922 if (new_process_refs >= process_refs) {
1923 cfqq->new_cfqq = new_cfqq;
1924 atomic_add(process_refs, &new_cfqq->ref);
1925 } else {
1926 new_cfqq->new_cfqq = cfqq;
1927 atomic_add(new_process_refs, &cfqq->ref);
1928 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001929}
1930
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001931static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
1932 struct cfq_group *cfqg, enum wl_prio_t prio,
1933 bool prio_changed)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001934{
1935 struct cfq_queue *queue;
1936 int i;
1937 bool key_valid = false;
1938 unsigned long lowest_key = 0;
1939 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1940
1941 if (prio_changed) {
1942 /*
1943 * When priorities switched, we prefer starting
1944 * from SYNC_NOIDLE (first choice), or just SYNC
1945 * over ASYNC
1946 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001947 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001948 return cur_best;
1949 cur_best = SYNC_WORKLOAD;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001950 if (service_tree_for(cfqg, prio, cur_best, cfqd)->count)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001951 return cur_best;
1952
1953 return ASYNC_WORKLOAD;
1954 }
1955
1956 for (i = 0; i < 3; ++i) {
1957 /* otherwise, select the one with lowest rb_key */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001958 queue = cfq_rb_first(service_tree_for(cfqg, prio, i, cfqd));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001959 if (queue &&
1960 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1961 lowest_key = queue->rb_key;
1962 cur_best = i;
1963 key_valid = true;
1964 }
1965 }
1966
1967 return cur_best;
1968}
1969
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001970static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001971{
1972 enum wl_prio_t previous_prio = cfqd->serving_prio;
1973 bool prio_changed;
1974 unsigned slice;
1975 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001976 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001977 unsigned group_slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001978
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001979 if (!cfqg) {
1980 cfqd->serving_prio = IDLE_WORKLOAD;
1981 cfqd->workload_expires = jiffies + 1;
1982 return;
1983 }
1984
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001985 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001986 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001987 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001988 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001989 cfqd->serving_prio = BE_WORKLOAD;
1990 else {
1991 cfqd->serving_prio = IDLE_WORKLOAD;
1992 cfqd->workload_expires = jiffies + 1;
1993 return;
1994 }
1995
1996 /*
1997 * For RT and BE, we have to choose also the type
1998 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
1999 * expiration time
2000 */
2001 prio_changed = (cfqd->serving_prio != previous_prio);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002002 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
2003 cfqd);
2004 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002005
2006 /*
2007 * If priority didn't change, check workload expiration,
2008 * and that we still have other queues ready
2009 */
2010 if (!prio_changed && count &&
2011 !time_after(jiffies, cfqd->workload_expires))
2012 return;
2013
2014 /* otherwise select new workload type */
2015 cfqd->serving_type =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002016 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio, prio_changed);
2017 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type,
2018 cfqd);
2019 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002020
2021 /*
2022 * the workload slice is computed as a fraction of target latency
2023 * proportional to the number of queues in that workload, over
2024 * all the queues in the same priority class
2025 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002026 group_slice = cfq_group_slice(cfqd, cfqg);
2027
2028 slice = group_slice * count /
2029 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2030 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002031
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002032 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2033 unsigned int tmp;
2034
2035 /*
2036 * Async queues are currently system wide. Just taking
2037 * proportion of queues with-in same group will lead to higher
2038 * async ratio system wide as generally root group is going
2039 * to have higher weight. A more accurate thing would be to
2040 * calculate system wide asnc/sync ratio.
2041 */
2042 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2043 tmp = tmp/cfqd->busy_queues;
2044 slice = min_t(unsigned, slice, tmp);
2045
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002046 /* async workload slice is scaled down according to
2047 * the sync/async slice ratio. */
2048 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002049 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002050 /* sync workload slice is at least 2 * cfq_slice_idle */
2051 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2052
2053 slice = max_t(unsigned, slice, CFQ_MIN_TT);
2054 cfqd->workload_expires = jiffies + slice;
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002055 cfqd->noidle_tree_requires_idle = false;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002056}
2057
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002058static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2059{
2060 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002061 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002062
2063 if (RB_EMPTY_ROOT(&st->rb))
2064 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002065 cfqg = cfq_rb_first_group(st);
2066 st->active = &cfqg->rb_node;
2067 update_min_vdisktime(st);
2068 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002069}
2070
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002071static void cfq_choose_cfqg(struct cfq_data *cfqd)
2072{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002073 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2074
2075 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002076
2077 /* Restore the workload type data */
2078 if (cfqg->saved_workload_slice) {
2079 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2080 cfqd->serving_type = cfqg->saved_workload;
2081 cfqd->serving_prio = cfqg->saved_serving_prio;
2082 }
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002083 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002084}
2085
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002086/*
Jens Axboe498d3aa2007-04-26 12:54:48 +02002087 * Select a queue for service. If we have a current active queue,
2088 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002089 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002090static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002091{
Jens Axboea36e71f2009-04-15 12:15:11 +02002092 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002093
2094 cfqq = cfqd->active_queue;
2095 if (!cfqq)
2096 goto new_queue;
2097
Vivek Goyalf04a6422009-12-03 12:59:40 -05002098 if (!cfqd->rq_queued)
2099 return NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002100 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002101 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002102 */
Vivek Goyalf75edf22009-12-03 12:59:53 -05002103 if ((cfq_slice_used(cfqq) || cfq_cfqq_wait_busy_done(cfqq))
2104 && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02002105 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02002106
2107 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002108 * The active queue has requests and isn't expired, allow it to
2109 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002110 */
Jens Axboedd67d052006-06-21 09:36:18 +02002111 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002112 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002113
2114 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002115 * If another queue has a request waiting within our mean seek
2116 * distance, let it run. The expire code will check for close
2117 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002118 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002119 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002120 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002121 if (new_cfqq) {
2122 if (!cfqq->new_cfqq)
2123 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002124 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002125 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002126
2127 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002128 * No requests pending. If the active queue still has requests in
2129 * flight or is idling for a new request, allow either of these
2130 * conditions to happen (or time out) before selecting a new queue.
2131 */
Jens Axboecc197472007-04-20 20:45:39 +02002132 if (timer_pending(&cfqd->idle_slice_timer) ||
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002133 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002134 cfqq = NULL;
2135 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002136 }
2137
Jens Axboe3b181522005-06-27 10:56:24 +02002138expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002139 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002140new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002141 /*
2142 * Current queue expired. Check if we have to switch to a new
2143 * service tree
2144 */
2145 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002146 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002147
Jens Axboea36e71f2009-04-15 12:15:11 +02002148 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002149keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002150 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002151}
2152
Jens Axboefebffd62008-01-28 13:19:43 +01002153static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002154{
2155 int dispatched = 0;
2156
2157 while (cfqq->next_rq) {
2158 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2159 dispatched++;
2160 }
2161
2162 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002163
2164 /* By default cfqq is not expired if it is empty. Do it explicitly */
2165 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002166 return dispatched;
2167}
2168
Jens Axboe498d3aa2007-04-26 12:54:48 +02002169/*
2170 * Drain our current requests. Used for barriers and when switching
2171 * io schedulers on-the-fly.
2172 */
Jens Axboed9e76202007-04-20 14:27:50 +02002173static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002174{
Jens Axboe08717142008-01-28 11:38:15 +01002175 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002176 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002177
Vivek Goyalf04a6422009-12-03 12:59:40 -05002178 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL)
2179 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002180
Jens Axboe6084cdd2007-04-23 08:25:00 +02002181 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002182 BUG_ON(cfqd->busy_queues);
2183
Jeff Moyer69237152009-06-12 15:29:30 +02002184 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002185 return dispatched;
2186}
2187
Jens Axboe0b182d62009-10-06 20:49:37 +02002188static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002189{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002190 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jens Axboe2f5cb732009-04-07 08:51:19 +02002192 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002193 * Drain async requests before we start sync IO
2194 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002195 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002196 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002197
2198 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002199 * If this is an async queue and we have sync IO in flight, let it wait
2200 */
2201 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002202 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002203
2204 max_dispatch = cfqd->cfq_quantum;
2205 if (cfq_class_idle(cfqq))
2206 max_dispatch = 1;
2207
2208 /*
2209 * Does this cfqq already have too much IO in flight?
2210 */
2211 if (cfqq->dispatched >= max_dispatch) {
2212 /*
2213 * idle queue must always only have a single IO in flight
2214 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002215 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002216 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002217
Jens Axboe2f5cb732009-04-07 08:51:19 +02002218 /*
2219 * We have other queues, don't allow more IO from this one
2220 */
2221 if (cfqd->busy_queues > 1)
Jens Axboe0b182d62009-10-06 20:49:37 +02002222 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002223
Jens Axboe2f5cb732009-04-07 08:51:19 +02002224 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002225 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002226 */
Shaohua Li474b18c2009-12-03 12:58:05 +01002227 max_dispatch = -1;
Jens Axboe8e296752009-10-03 16:26:03 +02002228 }
2229
2230 /*
2231 * Async queues must wait a bit before being allowed dispatch.
2232 * We also ramp up the dispatch depth gradually for async IO,
2233 * based on the last sync IO we serviced
2234 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002235 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jens Axboe8e296752009-10-03 16:26:03 +02002236 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
2237 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002238
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002239 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002240 if (!depth && !cfqq->dispatched)
2241 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002242 if (depth < max_dispatch)
2243 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
2245
Jens Axboe0b182d62009-10-06 20:49:37 +02002246 /*
2247 * If we're below the current max, allow a dispatch
2248 */
2249 return cfqq->dispatched < max_dispatch;
2250}
2251
2252/*
2253 * Dispatch a request from cfqq, moving them to the request queue
2254 * dispatch list.
2255 */
2256static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2257{
2258 struct request *rq;
2259
2260 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2261
2262 if (!cfq_may_dispatch(cfqd, cfqq))
2263 return false;
2264
2265 /*
2266 * follow expired path, else get first next available
2267 */
2268 rq = cfq_check_fifo(cfqq);
2269 if (!rq)
2270 rq = cfqq->next_rq;
2271
2272 /*
2273 * insert request into driver dispatch list
2274 */
2275 cfq_dispatch_insert(cfqd->queue, rq);
2276
2277 if (!cfqd->active_cic) {
2278 struct cfq_io_context *cic = RQ_CIC(rq);
2279
2280 atomic_long_inc(&cic->ioc->refcount);
2281 cfqd->active_cic = cic;
2282 }
2283
2284 return true;
2285}
2286
2287/*
2288 * Find the cfqq that we need to service and move a request from that to the
2289 * dispatch list
2290 */
2291static int cfq_dispatch_requests(struct request_queue *q, int force)
2292{
2293 struct cfq_data *cfqd = q->elevator->elevator_data;
2294 struct cfq_queue *cfqq;
2295
2296 if (!cfqd->busy_queues)
2297 return 0;
2298
2299 if (unlikely(force))
2300 return cfq_forced_dispatch(cfqd);
2301
2302 cfqq = cfq_select_queue(cfqd);
2303 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002304 return 0;
2305
Jens Axboe2f5cb732009-04-07 08:51:19 +02002306 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002307 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002308 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002309 if (!cfq_dispatch_request(cfqd, cfqq))
2310 return 0;
2311
Jens Axboe2f5cb732009-04-07 08:51:19 +02002312 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002313 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002314
2315 /*
2316 * expire an async queue immediately if it has used up its slice. idle
2317 * queue always expire after 1 dispatch round.
2318 */
2319 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2320 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2321 cfq_class_idle(cfqq))) {
2322 cfqq->slice_end = jiffies + 1;
2323 cfq_slice_expired(cfqd, 0);
2324 }
2325
Shan Weib217a902009-09-01 10:06:42 +02002326 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002327 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
Jens Axboe5e705372006-07-13 12:39:25 +02002331 * task holds one reference to the queue, dropped when task exits. each rq
2332 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002334 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 * queue lock must be held here.
2336 */
2337static void cfq_put_queue(struct cfq_queue *cfqq)
2338{
Jens Axboe22e2c502005-06-27 10:55:12 +02002339 struct cfq_data *cfqd = cfqq->cfqd;
Vivek Goyalb1c35762009-12-03 12:59:47 -05002340 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002341
2342 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 if (!atomic_dec_and_test(&cfqq->ref))
2345 return;
2346
Jens Axboe7b679132008-05-30 12:23:07 +02002347 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002349 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002350 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002352 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002353 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002354 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002355 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002356
Vivek Goyalf04a6422009-12-03 12:59:40 -05002357 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 kmem_cache_free(cfq_pool, cfqq);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002359 cfq_put_cfqg(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Jens Axboed6de8be2008-05-28 14:46:59 +02002362/*
2363 * Must always be called with the rcu_read_lock() held
2364 */
Jens Axboe07416d22008-05-07 09:17:12 +02002365static void
2366__call_for_each_cic(struct io_context *ioc,
2367 void (*func)(struct io_context *, struct cfq_io_context *))
2368{
2369 struct cfq_io_context *cic;
2370 struct hlist_node *n;
2371
2372 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2373 func(ioc, cic);
2374}
2375
Jens Axboe4ac845a2008-01-24 08:44:49 +01002376/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002377 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002378 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002379static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002380call_for_each_cic(struct io_context *ioc,
2381 void (*func)(struct io_context *, struct cfq_io_context *))
2382{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002383 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02002384 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002385 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002386}
Jens Axboe4ac845a2008-01-24 08:44:49 +01002387
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002388static void cfq_cic_free_rcu(struct rcu_head *head)
2389{
2390 struct cfq_io_context *cic;
2391
2392 cic = container_of(head, struct cfq_io_context, rcu_head);
2393
2394 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09002395 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002396
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002397 if (ioc_gone) {
2398 /*
2399 * CFQ scheduler is exiting, grab exit lock and check
2400 * the pending io context count. If it hits zero,
2401 * complete ioc_gone and set it back to NULL
2402 */
2403 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09002404 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002405 complete(ioc_gone);
2406 ioc_gone = NULL;
2407 }
2408 spin_unlock(&ioc_gone_lock);
2409 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002410}
2411
2412static void cfq_cic_free(struct cfq_io_context *cic)
2413{
2414 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002415}
2416
2417static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2418{
2419 unsigned long flags;
2420
2421 BUG_ON(!cic->dead_key);
2422
2423 spin_lock_irqsave(&ioc->lock, flags);
2424 radix_tree_delete(&ioc->radix_root, cic->dead_key);
Jens Axboeffc4e752008-02-19 10:02:29 +01002425 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002426 spin_unlock_irqrestore(&ioc->lock, flags);
2427
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002428 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002429}
2430
Jens Axboed6de8be2008-05-28 14:46:59 +02002431/*
2432 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2433 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2434 * and ->trim() which is called with the task lock held
2435 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002436static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002438 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002439 * ioc->refcount is zero here, or we are called from elv_unregister(),
2440 * so no more cic's are allowed to be linked into this ioc. So it
2441 * should be ok to iterate over the known list, we will see all cic's
2442 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002443 */
Jens Axboe07416d22008-05-07 09:17:12 +02002444 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Jens Axboe89850f72006-07-22 16:48:31 +02002447static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2448{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002449 struct cfq_queue *__cfqq, *next;
2450
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002451 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02002452 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002453 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002454 }
Jens Axboe89850f72006-07-22 16:48:31 +02002455
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002456 /*
2457 * If this queue was scheduled to merge with another queue, be
2458 * sure to drop the reference taken on that queue (and others in
2459 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2460 */
2461 __cfqq = cfqq->new_cfqq;
2462 while (__cfqq) {
2463 if (__cfqq == cfqq) {
2464 WARN(1, "cfqq->new_cfqq loop detected\n");
2465 break;
2466 }
2467 next = __cfqq->new_cfqq;
2468 cfq_put_queue(__cfqq);
2469 __cfqq = next;
2470 }
2471
Jens Axboe89850f72006-07-22 16:48:31 +02002472 cfq_put_queue(cfqq);
2473}
2474
2475static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2476 struct cfq_io_context *cic)
2477{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002478 struct io_context *ioc = cic->ioc;
2479
Jens Axboefc463792006-08-29 09:05:44 +02002480 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002481
2482 /*
2483 * Make sure key == NULL is seen for dead queues
2484 */
Jens Axboefc463792006-08-29 09:05:44 +02002485 smp_wmb();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002486 cic->dead_key = (unsigned long) cic->key;
Jens Axboefc463792006-08-29 09:05:44 +02002487 cic->key = NULL;
2488
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002489 if (ioc->ioc_data == cic)
2490 rcu_assign_pointer(ioc->ioc_data, NULL);
2491
Jens Axboeff6657c2009-04-08 10:58:57 +02002492 if (cic->cfqq[BLK_RW_ASYNC]) {
2493 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2494 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002495 }
2496
Jens Axboeff6657c2009-04-08 10:58:57 +02002497 if (cic->cfqq[BLK_RW_SYNC]) {
2498 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2499 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002500 }
Jens Axboe89850f72006-07-22 16:48:31 +02002501}
2502
Jens Axboe4ac845a2008-01-24 08:44:49 +01002503static void cfq_exit_single_io_context(struct io_context *ioc,
2504 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002505{
Al Viro478a82b2006-03-18 13:25:24 -05002506 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02002507
Jens Axboe89850f72006-07-22 16:48:31 +02002508 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02002509 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002510 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02002511
Jens Axboe4ac845a2008-01-24 08:44:49 +01002512 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01002513
2514 /*
2515 * Ensure we get a fresh copy of the ->key to prevent
2516 * race between exiting task and queue
2517 */
2518 smp_read_barrier_depends();
2519 if (cic->key)
2520 __cfq_exit_single_io_context(cfqd, cic);
2521
Jens Axboe4ac845a2008-01-24 08:44:49 +01002522 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05002523 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002524}
2525
Jens Axboe498d3aa2007-04-26 12:54:48 +02002526/*
2527 * The process that ioc belongs to has exited, we need to clean up
2528 * and put the internal structures we have that belongs to that process.
2529 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002530static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002532 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Jens Axboe22e2c502005-06-27 10:55:12 +02002535static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04002536cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Jens Axboeb5deef92006-07-19 23:39:40 +02002538 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Christoph Lameter94f60302007-07-17 04:03:29 -07002540 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2541 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002543 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02002544 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01002545 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02002546 cic->dtor = cfq_free_io_context;
2547 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09002548 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
2551 return cic;
2552}
2553
Jens Axboefd0928d2008-01-24 08:52:45 +01002554static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002555{
2556 struct task_struct *tsk = current;
2557 int ioprio_class;
2558
Jens Axboe3b181522005-06-27 10:56:24 +02002559 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002560 return;
2561
Jens Axboefd0928d2008-01-24 08:52:45 +01002562 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002563 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002564 default:
2565 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2566 case IOPRIO_CLASS_NONE:
2567 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002568 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002569 */
2570 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002571 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002572 break;
2573 case IOPRIO_CLASS_RT:
2574 cfqq->ioprio = task_ioprio(ioc);
2575 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2576 break;
2577 case IOPRIO_CLASS_BE:
2578 cfqq->ioprio = task_ioprio(ioc);
2579 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2580 break;
2581 case IOPRIO_CLASS_IDLE:
2582 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2583 cfqq->ioprio = 7;
2584 cfq_clear_cfqq_idle_window(cfqq);
2585 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002586 }
2587
2588 /*
2589 * keep track of original prio settings in case we have to temporarily
2590 * elevate the priority of this queue
2591 */
2592 cfqq->org_ioprio = cfqq->ioprio;
2593 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02002594 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002595}
2596
Jens Axboefebffd62008-01-28 13:19:43 +01002597static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002598{
Al Viro478a82b2006-03-18 13:25:24 -05002599 struct cfq_data *cfqd = cic->key;
2600 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01002601 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02002602
Jens Axboecaaa5f92006-06-16 11:23:00 +02002603 if (unlikely(!cfqd))
2604 return;
2605
Jens Axboec1b707d2006-10-30 19:54:23 +01002606 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002607
Jens Axboeff6657c2009-04-08 10:58:57 +02002608 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002609 if (cfqq) {
2610 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02002611 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2612 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002613 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002614 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002615 cfq_put_queue(cfqq);
2616 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002617 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002618
Jens Axboeff6657c2009-04-08 10:58:57 +02002619 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002620 if (cfqq)
2621 cfq_mark_cfqq_prio_changed(cfqq);
2622
Jens Axboec1b707d2006-10-30 19:54:23 +01002623 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02002624}
2625
Jens Axboefc463792006-08-29 09:05:44 +02002626static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002628 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02002629 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Jens Axboed5036d72009-06-26 10:44:34 +02002632static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002633 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002634{
2635 RB_CLEAR_NODE(&cfqq->rb_node);
2636 RB_CLEAR_NODE(&cfqq->p_node);
2637 INIT_LIST_HEAD(&cfqq->fifo);
2638
2639 atomic_set(&cfqq->ref, 0);
2640 cfqq->cfqd = cfqd;
2641
2642 cfq_mark_cfqq_prio_changed(cfqq);
2643
2644 if (is_sync) {
2645 if (!cfq_class_idle(cfqq))
2646 cfq_mark_cfqq_idle_window(cfqq);
2647 cfq_mark_cfqq_sync(cfqq);
2648 }
2649 cfqq->pid = pid;
2650}
2651
Vivek Goyal24610332009-12-03 12:59:51 -05002652#ifdef CONFIG_CFQ_GROUP_IOSCHED
2653static void changed_cgroup(struct io_context *ioc, struct cfq_io_context *cic)
2654{
2655 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
2656 struct cfq_data *cfqd = cic->key;
2657 unsigned long flags;
2658 struct request_queue *q;
2659
2660 if (unlikely(!cfqd))
2661 return;
2662
2663 q = cfqd->queue;
2664
2665 spin_lock_irqsave(q->queue_lock, flags);
2666
2667 if (sync_cfqq) {
2668 /*
2669 * Drop reference to sync queue. A new sync queue will be
2670 * assigned in new group upon arrival of a fresh request.
2671 */
2672 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2673 cic_set_cfqq(cic, NULL, 1);
2674 cfq_put_queue(sync_cfqq);
2675 }
2676
2677 spin_unlock_irqrestore(q->queue_lock, flags);
2678}
2679
2680static void cfq_ioc_set_cgroup(struct io_context *ioc)
2681{
2682 call_for_each_cic(ioc, changed_cgroup);
2683 ioc->cgroup_changed = 0;
2684}
2685#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002688cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002689 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002692 struct cfq_io_context *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002693 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695retry:
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002696 cfqg = cfq_get_cfqg(cfqd, 1);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002697 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002698 /* cic always exists here */
2699 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Jens Axboe6118b702009-06-30 09:34:12 +02002701 /*
2702 * Always try a new alloc if we fell back to the OOM cfqq
2703 * originally, since it should just be a temporary situation.
2704 */
2705 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2706 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (new_cfqq) {
2708 cfqq = new_cfqq;
2709 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002710 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002712 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002713 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002714 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002716 if (new_cfqq)
2717 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002718 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002719 cfqq = kmem_cache_alloc_node(cfq_pool,
2720 gfp_mask | __GFP_ZERO,
2721 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Jens Axboe6118b702009-06-30 09:34:12 +02002724 if (cfqq) {
2725 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2726 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002727 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002728 cfq_log_cfqq(cfqd, cfqq, "alloced");
2729 } else
2730 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732
2733 if (new_cfqq)
2734 kmem_cache_free(cfq_pool, new_cfqq);
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return cfqq;
2737}
2738
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002739static struct cfq_queue **
2740cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2741{
Jens Axboefe094d92008-01-31 13:08:54 +01002742 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002743 case IOPRIO_CLASS_RT:
2744 return &cfqd->async_cfqq[0][ioprio];
2745 case IOPRIO_CLASS_BE:
2746 return &cfqd->async_cfqq[1][ioprio];
2747 case IOPRIO_CLASS_IDLE:
2748 return &cfqd->async_idle_cfqq;
2749 default:
2750 BUG();
2751 }
2752}
2753
Jens Axboe15c31be2007-07-10 13:43:25 +02002754static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002755cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002756 gfp_t gfp_mask)
2757{
Jens Axboefd0928d2008-01-24 08:52:45 +01002758 const int ioprio = task_ioprio(ioc);
2759 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002760 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002761 struct cfq_queue *cfqq = NULL;
2762
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002763 if (!is_sync) {
2764 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2765 cfqq = *async_cfqq;
2766 }
2767
Jens Axboe6118b702009-06-30 09:34:12 +02002768 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002769 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002770
2771 /*
2772 * pin the queue now that it's allocated, scheduler exit will prune it
2773 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002774 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02002775 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002776 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002777 }
2778
2779 atomic_inc(&cfqq->ref);
2780 return cfqq;
2781}
2782
Jens Axboe498d3aa2007-04-26 12:54:48 +02002783/*
2784 * We drop cfq io contexts lazily, so we may find a dead one.
2785 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002786static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002787cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2788 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002789{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002790 unsigned long flags;
2791
Jens Axboefc463792006-08-29 09:05:44 +02002792 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02002793
Jens Axboe4ac845a2008-01-24 08:44:49 +01002794 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02002795
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002796 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002797
2798 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
Jens Axboeffc4e752008-02-19 10:02:29 +01002799 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002800 spin_unlock_irqrestore(&ioc->lock, flags);
2801
2802 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002803}
2804
Jens Axboee2d74ac2006-03-28 08:59:01 +02002805static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01002806cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002807{
Jens Axboee2d74ac2006-03-28 08:59:01 +02002808 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002809 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002810 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002811
Vasily Tarasov91fac312007-04-25 12:29:51 +02002812 if (unlikely(!ioc))
2813 return NULL;
2814
Jens Axboed6de8be2008-05-28 14:46:59 +02002815 rcu_read_lock();
2816
Jens Axboe597bc482007-04-24 21:23:53 +02002817 /*
2818 * we maintain a last-hit cache, to avoid browsing over the tree
2819 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002820 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02002821 if (cic && cic->key == cfqd) {
2822 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02002823 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002824 }
Jens Axboe597bc482007-04-24 21:23:53 +02002825
Jens Axboe4ac845a2008-01-24 08:44:49 +01002826 do {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002827 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2828 rcu_read_unlock();
2829 if (!cic)
2830 break;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02002831 /* ->key must be copied to avoid race with cfq_exit_queue() */
2832 k = cic->key;
2833 if (unlikely(!k)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002834 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002835 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002836 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002837 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002838
Jens Axboed6de8be2008-05-28 14:46:59 +02002839 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002840 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002841 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002842 break;
2843 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002844
Jens Axboe4ac845a2008-01-24 08:44:49 +01002845 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002846}
2847
Jens Axboe4ac845a2008-01-24 08:44:49 +01002848/*
2849 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2850 * the process specific cfq io context when entered from the block layer.
2851 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2852 */
Jens Axboefebffd62008-01-28 13:19:43 +01002853static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2854 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002855{
Jens Axboe0261d682006-10-30 19:07:48 +01002856 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002857 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002858
Jens Axboe4ac845a2008-01-24 08:44:49 +01002859 ret = radix_tree_preload(gfp_mask);
2860 if (!ret) {
2861 cic->ioc = ioc;
2862 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002863
Jens Axboe4ac845a2008-01-24 08:44:49 +01002864 spin_lock_irqsave(&ioc->lock, flags);
2865 ret = radix_tree_insert(&ioc->radix_root,
2866 (unsigned long) cfqd, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01002867 if (!ret)
2868 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002869 spin_unlock_irqrestore(&ioc->lock, flags);
2870
2871 radix_tree_preload_end();
2872
2873 if (!ret) {
2874 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2875 list_add(&cic->queue_list, &cfqd->cic_list);
2876 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002877 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002878 }
2879
Jens Axboe4ac845a2008-01-24 08:44:49 +01002880 if (ret)
2881 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02002882
Jens Axboe4ac845a2008-01-24 08:44:49 +01002883 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002884}
2885
Jens Axboe22e2c502005-06-27 10:55:12 +02002886/*
2887 * Setup general io context and cfq io context. There can be several cfq
2888 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02002889 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02002890 */
2891static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02002892cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Jens Axboe22e2c502005-06-27 10:55:12 +02002894 struct io_context *ioc = NULL;
2895 struct cfq_io_context *cic;
2896
2897 might_sleep_if(gfp_mask & __GFP_WAIT);
2898
Jens Axboeb5deef92006-07-19 23:39:40 +02002899 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02002900 if (!ioc)
2901 return NULL;
2902
Jens Axboe4ac845a2008-01-24 08:44:49 +01002903 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002904 if (cic)
2905 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02002906
Jens Axboee2d74ac2006-03-28 08:59:01 +02002907 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2908 if (cic == NULL)
2909 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02002910
Jens Axboe4ac845a2008-01-24 08:44:49 +01002911 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2912 goto err_free;
2913
Jens Axboe22e2c502005-06-27 10:55:12 +02002914out:
Jens Axboefc463792006-08-29 09:05:44 +02002915 smp_read_barrier_depends();
2916 if (unlikely(ioc->ioprio_changed))
2917 cfq_ioc_set_ioprio(ioc);
2918
Vivek Goyal24610332009-12-03 12:59:51 -05002919#ifdef CONFIG_CFQ_GROUP_IOSCHED
2920 if (unlikely(ioc->cgroup_changed))
2921 cfq_ioc_set_cgroup(ioc);
2922#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02002923 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002924err_free:
2925 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002926err:
2927 put_io_context(ioc);
2928 return NULL;
2929}
2930
2931static void
2932cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2933{
Jens Axboeaaf12282007-01-19 11:30:16 +11002934 unsigned long elapsed = jiffies - cic->last_end_request;
2935 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002936
2937 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2938 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2939 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2940}
2941
Jens Axboe206dc692006-03-28 13:03:44 +02002942static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002943cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002944 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002945{
2946 sector_t sdist;
2947 u64 total;
2948
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002949 if (!cfqq->last_request_pos)
Jeff Moyer4d00aa42009-04-21 07:25:04 +02002950 sdist = 0;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002951 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2952 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02002953 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002954 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
Jens Axboe206dc692006-03-28 13:03:44 +02002955
2956 /*
2957 * Don't allow the seek distance to get too large from the
2958 * odd fragment, pagein, etc
2959 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002960 if (cfqq->seek_samples <= 60) /* second&third seek */
2961 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
Jens Axboe206dc692006-03-28 13:03:44 +02002962 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002963 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
Jens Axboe206dc692006-03-28 13:03:44 +02002964
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002965 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2966 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2967 total = cfqq->seek_total + (cfqq->seek_samples/2);
2968 do_div(total, cfqq->seek_samples);
2969 cfqq->seek_mean = (sector_t)total;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002970
2971 /*
2972 * If this cfqq is shared between multiple processes, check to
2973 * make sure that those processes are still issuing I/Os within
2974 * the mean seek distance. If not, it may be time to break the
2975 * queues apart again.
2976 */
2977 if (cfq_cfqq_coop(cfqq)) {
2978 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2979 cfqq->seeky_start = jiffies;
2980 else if (!CFQQ_SEEKY(cfqq))
2981 cfqq->seeky_start = 0;
2982 }
Jens Axboe206dc692006-03-28 13:03:44 +02002983}
Jens Axboe22e2c502005-06-27 10:55:12 +02002984
2985/*
2986 * Disable idle window if the process thinks too long or seeks so much that
2987 * it doesn't matter
2988 */
2989static void
2990cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2991 struct cfq_io_context *cic)
2992{
Jens Axboe7b679132008-05-30 12:23:07 +02002993 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002994
Jens Axboe08717142008-01-28 11:38:15 +01002995 /*
2996 * Don't idle for async or idle io prio class
2997 */
2998 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002999 return;
3000
Jens Axboec265a7f2008-06-26 13:49:33 +02003001 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003002
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003003 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3004 cfq_mark_cfqq_deep(cfqq);
3005
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01003006 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003007 (!cfq_cfqq_deep(cfqq) && sample_valid(cfqq->seek_samples)
3008 && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003009 enable_idle = 0;
3010 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003011 if (cic->ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003012 enable_idle = 0;
3013 else
3014 enable_idle = 1;
3015 }
3016
Jens Axboe7b679132008-05-30 12:23:07 +02003017 if (old_idle != enable_idle) {
3018 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3019 if (enable_idle)
3020 cfq_mark_cfqq_idle_window(cfqq);
3021 else
3022 cfq_clear_cfqq_idle_window(cfqq);
3023 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003024}
3025
Jens Axboe22e2c502005-06-27 10:55:12 +02003026/*
3027 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3028 * no or if we aren't sure, a 1 will cause a preempt.
3029 */
Jens Axboea6151c32009-10-07 20:02:57 +02003030static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003031cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003032 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003033{
Jens Axboe6d048f52007-04-25 12:44:27 +02003034 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003035
Jens Axboe6d048f52007-04-25 12:44:27 +02003036 cfqq = cfqd->active_queue;
3037 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003038 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003039
Jens Axboe6d048f52007-04-25 12:44:27 +02003040 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003041 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003042
3043 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003044 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003045
Jens Axboe22e2c502005-06-27 10:55:12 +02003046 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003047 * if the new request is sync, but the currently running queue is
3048 * not, let the sync request have priority.
3049 */
Jens Axboe5e705372006-07-13 12:39:25 +02003050 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003051 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003052
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003053 if (new_cfqq->cfqg != cfqq->cfqg)
3054 return false;
3055
3056 if (cfq_slice_used(cfqq))
3057 return true;
3058
3059 /* Allow preemption only if we are idling on sync-noidle tree */
3060 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3061 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3062 new_cfqq->service_tree->count == 2 &&
3063 RB_EMPTY_ROOT(&cfqq->sort_list))
3064 return true;
3065
Jens Axboe374f84a2006-07-23 01:42:19 +02003066 /*
3067 * So both queues are sync. Let the new request get disk time if
3068 * it's a metadata request and the current queue is doing regular IO.
3069 */
3070 if (rq_is_meta(rq) && !cfqq->meta_pending)
Jens Axboee6ec4fe2009-11-03 20:21:35 +01003071 return true;
Jens Axboe22e2c502005-06-27 10:55:12 +02003072
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003073 /*
3074 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3075 */
3076 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003077 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003078
Jens Axboe1e3335d2007-02-14 19:59:49 +01003079 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003080 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003081
3082 /*
3083 * if this request is as-good as one we would expect from the
3084 * current cfqq, let it preempt
3085 */
Jens Axboee00ef792009-11-04 08:54:55 +01003086 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003087 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003088
Jens Axboea6151c32009-10-07 20:02:57 +02003089 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003090}
3091
3092/*
3093 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3094 * let it have half of its nominal slice.
3095 */
3096static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3097{
Jens Axboe7b679132008-05-30 12:23:07 +02003098 cfq_log_cfqq(cfqd, cfqq, "preempt");
Jens Axboe6084cdd2007-04-23 08:25:00 +02003099 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003100
Jens Axboebf572252006-07-19 20:29:12 +02003101 /*
3102 * Put the new queue at the front of the of the current list,
3103 * so we know that it will be selected next.
3104 */
3105 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003106
3107 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02003108
Jens Axboe44f7c162007-01-19 11:51:58 +11003109 cfqq->slice_end = 0;
3110 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003111}
3112
3113/*
Jens Axboe5e705372006-07-13 12:39:25 +02003114 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003115 * something we should do about it
3116 */
3117static void
Jens Axboe5e705372006-07-13 12:39:25 +02003118cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3119 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003120{
Jens Axboe5e705372006-07-13 12:39:25 +02003121 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003122
Aaron Carroll45333d52008-08-26 15:52:36 +02003123 cfqd->rq_queued++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003124 if (rq_is_meta(rq))
3125 cfqq->meta_pending++;
3126
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003127 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003128 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003129 cfq_update_idle_window(cfqd, cfqq, cic);
3130
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003131 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003132
3133 if (cfqq == cfqd->active_queue) {
Vivek Goyalf75edf22009-12-03 12:59:53 -05003134 if (cfq_cfqq_wait_busy(cfqq)) {
3135 cfq_clear_cfqq_wait_busy(cfqq);
3136 cfq_mark_cfqq_wait_busy_done(cfqq);
3137 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003138 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003139 * Remember that we saw a request from this process, but
3140 * don't start queuing just yet. Otherwise we risk seeing lots
3141 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003142 * and merging. If the request is already larger than a single
3143 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003144 * merging is already done. Ditto for a busy system that
3145 * has other work pending, don't risk delaying until the
3146 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003147 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003148 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003149 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3150 cfqd->busy_queues > 1) {
Jens Axboed6ceb252009-04-14 14:18:16 +02003151 del_timer(&cfqd->idle_slice_timer);
Vivek Goyalbf791932009-12-03 12:59:37 -05003152 __blk_run_queue(cfqd->queue);
3153 } else
3154 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboed6ceb252009-04-14 14:18:16 +02003155 }
Jens Axboe5e705372006-07-13 12:39:25 +02003156 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003157 /*
3158 * not the active queue - expire current slice if it is
3159 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003160 * has some old slice time left and is of higher priority or
3161 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003162 */
3163 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09003164 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003165 }
3166}
3167
Jens Axboe165125e2007-07-24 09:28:11 +02003168static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003169{
Jens Axboeb4878f22005-10-20 16:42:29 +02003170 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003171 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003172
Jens Axboe7b679132008-05-30 12:23:07 +02003173 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01003174 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Jens Axboe30996f42009-10-05 11:03:39 +02003176 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003177 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003178 cfq_add_rq_rb(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003179
Jens Axboe5e705372006-07-13 12:39:25 +02003180 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Aaron Carroll45333d52008-08-26 15:52:36 +02003183/*
3184 * Update hw_tag based on peak queue depth over 50 samples under
3185 * sufficient load.
3186 */
3187static void cfq_update_hw_tag(struct cfq_data *cfqd)
3188{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003189 struct cfq_queue *cfqq = cfqd->active_queue;
3190
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003191 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
3192 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
3193
3194 if (cfqd->hw_tag == 1)
3195 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003196
3197 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Jens Axboe5ad531d2009-07-03 12:57:48 +02003198 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003199 return;
3200
Shaohua Li1a1238a2009-10-27 08:46:23 +01003201 /*
3202 * If active queue hasn't enough requests and can idle, cfq might not
3203 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3204 * case
3205 */
3206 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3207 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
3208 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
3209 return;
3210
Aaron Carroll45333d52008-08-26 15:52:36 +02003211 if (cfqd->hw_tag_samples++ < 50)
3212 return;
3213
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003214 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003215 cfqd->hw_tag = 1;
3216 else
3217 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003218}
3219
Jens Axboe165125e2007-07-24 09:28:11 +02003220static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Jens Axboe5e705372006-07-13 12:39:25 +02003222 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003223 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003224 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003225 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Jens Axboeb4878f22005-10-20 16:42:29 +02003227 now = jiffies;
Vivek Goyal2868ef72009-12-03 12:59:48 -05003228 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", !!rq_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Aaron Carroll45333d52008-08-26 15:52:36 +02003230 cfq_update_hw_tag(cfqd);
3231
Jens Axboe5ad531d2009-07-03 12:57:48 +02003232 WARN_ON(!cfqd->rq_in_driver[sync]);
Jens Axboe6d048f52007-04-25 12:44:27 +02003233 WARN_ON(!cfqq->dispatched);
Jens Axboe5ad531d2009-07-03 12:57:48 +02003234 cfqd->rq_in_driver[sync]--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003235 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Jens Axboe3ed9a292007-04-23 08:33:33 +02003237 if (cfq_cfqq_sync(cfqq))
3238 cfqd->sync_flight--;
3239
Vivek Goyal365722b2009-10-03 15:21:27 +02003240 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02003241 RQ_CIC(rq)->last_end_request = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003242 cfqd->last_end_sync_rq = now;
3243 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003244
3245 /*
3246 * If this is the active queue, check if it needs to be expired,
3247 * or if we want to idle in case it has no pending requests.
3248 */
3249 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003250 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3251
Jens Axboe44f7c162007-01-19 11:51:58 +11003252 if (cfq_cfqq_slice_new(cfqq)) {
3253 cfq_set_prio_slice(cfqd, cfqq);
3254 cfq_clear_cfqq_slice_new(cfqq);
3255 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003256
3257 /*
3258 * If this queue consumed its slice and this is last queue
3259 * in the group, wait for next request before we expire
3260 * the queue
3261 */
3262 if (cfq_slice_used(cfqq) && cfqq->cfqg->nr_cfqq == 1) {
3263 cfqq->slice_end = jiffies + cfqd->cfq_slice_idle;
3264 cfq_mark_cfqq_wait_busy(cfqq);
3265 }
3266
Jens Axboea36e71f2009-04-15 12:15:11 +02003267 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003268 * Idling is not enabled on:
3269 * - expired queues
3270 * - idle-priority queues
3271 * - async queues
3272 * - queues with still some requests queued
3273 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003274 */
Jens Axboe08717142008-01-28 11:38:15 +01003275 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02003276 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003277 else if (sync && cfqq_empty &&
3278 !cfq_close_cooperator(cfqd, cfqq)) {
3279 cfqd->noidle_tree_requires_idle |= !rq_noidle(rq);
3280 /*
3281 * Idling is enabled for SYNC_WORKLOAD.
3282 * SYNC_NOIDLE_WORKLOAD idles at the end of the tree
3283 * only if we processed at least one !rq_noidle request
3284 */
3285 if (cfqd->serving_type == SYNC_WORKLOAD
3286 || cfqd->noidle_tree_requires_idle)
3287 cfq_arm_slice_timer(cfqd);
3288 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003289 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003290
Jens Axboe5ad531d2009-07-03 12:57:48 +02003291 if (!rq_in_driver(cfqd))
Jens Axboe23e018a2009-10-05 08:52:35 +02003292 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
Jens Axboe22e2c502005-06-27 10:55:12 +02003295/*
3296 * we temporarily boost lower priority queues if they are holding fs exclusive
3297 * resources. they are boosted to normal prio (CLASS_BE/4)
3298 */
3299static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Jens Axboe22e2c502005-06-27 10:55:12 +02003301 if (has_fs_excl()) {
3302 /*
3303 * boost idle prio on transactions that would lock out other
3304 * users of the filesystem
3305 */
3306 if (cfq_class_idle(cfqq))
3307 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3308 if (cfqq->ioprio > IOPRIO_NORM)
3309 cfqq->ioprio = IOPRIO_NORM;
3310 } else {
3311 /*
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003312 * unboost the queue (if needed)
Jens Axboe22e2c502005-06-27 10:55:12 +02003313 */
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003314 cfqq->ioprio_class = cfqq->org_ioprio_class;
3315 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003317}
3318
Jens Axboe89850f72006-07-22 16:48:31 +02003319static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003320{
Jens Axboe1b379d82009-08-11 08:26:11 +02003321 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003322 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003323 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003324 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003325
3326 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003327}
3328
Jens Axboe165125e2007-07-24 09:28:11 +02003329static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003330{
3331 struct cfq_data *cfqd = q->elevator->elevator_data;
3332 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02003333 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003334 struct cfq_queue *cfqq;
3335
3336 /*
3337 * don't force setup of a queue from here, as a call to may_queue
3338 * does not necessarily imply that a request actually will be queued.
3339 * so just lookup a possibly existing queue, or return 'may queue'
3340 * if that fails
3341 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003342 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003343 if (!cic)
3344 return ELV_MQUEUE_MAY;
3345
Jens Axboeb0b78f82009-04-08 10:56:08 +02003346 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003347 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003348 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003349 cfq_prio_boost(cfqq);
3350
Jens Axboe89850f72006-07-22 16:48:31 +02003351 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003352 }
3353
3354 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357/*
3358 * queue lock held here
3359 */
Jens Axboebb37b942006-12-01 10:42:33 +01003360static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Jens Axboe5e705372006-07-13 12:39:25 +02003362 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Jens Axboe5e705372006-07-13 12:39:25 +02003364 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003365 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Jens Axboe22e2c502005-06-27 10:55:12 +02003367 BUG_ON(!cfqq->allocated[rw]);
3368 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Jens Axboe5e705372006-07-13 12:39:25 +02003370 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02003373 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 cfq_put_queue(cfqq);
3376 }
3377}
3378
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003379static struct cfq_queue *
3380cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3381 struct cfq_queue *cfqq)
3382{
3383 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3384 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003385 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003386 cfq_put_queue(cfqq);
3387 return cic_to_cfqq(cic, 1);
3388}
3389
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003390static int should_split_cfqq(struct cfq_queue *cfqq)
3391{
3392 if (cfqq->seeky_start &&
3393 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
3394 return 1;
3395 return 0;
3396}
3397
3398/*
3399 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3400 * was the last process referring to said cfqq.
3401 */
3402static struct cfq_queue *
3403split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3404{
3405 if (cfqq_process_refs(cfqq) == 1) {
3406 cfqq->seeky_start = 0;
3407 cfqq->pid = current->pid;
3408 cfq_clear_cfqq_coop(cfqq);
3409 return cfqq;
3410 }
3411
3412 cic_set_cfqq(cic, NULL, 1);
3413 cfq_put_queue(cfqq);
3414 return NULL;
3415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003417 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003419static int
Jens Axboe165125e2007-07-24 09:28:11 +02003420cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct cfq_data *cfqd = q->elevator->elevator_data;
3423 struct cfq_io_context *cic;
3424 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003425 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003426 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 unsigned long flags;
3428
3429 might_sleep_if(gfp_mask & __GFP_WAIT);
3430
Jens Axboee2d74ac2006-03-28 08:59:01 +02003431 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02003432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 spin_lock_irqsave(q->queue_lock, flags);
3434
Jens Axboe22e2c502005-06-27 10:55:12 +02003435 if (!cic)
3436 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003438new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003439 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003440 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003441 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003442 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003443 } else {
3444 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003445 * If the queue was seeky for too long, break it apart.
3446 */
3447 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
3448 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3449 cfqq = split_cfqq(cic, cfqq);
3450 if (!cfqq)
3451 goto new_queue;
3452 }
3453
3454 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003455 * Check to see if this queue is scheduled to merge with
3456 * another, closely cooperating queue. The merging of
3457 * queues happens here as it must be done in process context.
3458 * The reference on new_cfqq was taken in merge_cfqqs.
3459 */
3460 if (cfqq->new_cfqq)
3461 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02003465 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 spin_unlock_irqrestore(q->queue_lock, flags);
3468
Jens Axboe5e705372006-07-13 12:39:25 +02003469 rq->elevator_private = cic;
3470 rq->elevator_private2 = cfqq;
3471 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02003472
Jens Axboe22e2c502005-06-27 10:55:12 +02003473queue_fail:
3474 if (cic)
3475 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02003476
Jens Axboe23e018a2009-10-05 08:52:35 +02003477 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02003479 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 1;
3481}
3482
David Howells65f27f32006-11-22 14:55:48 +00003483static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003484{
David Howells65f27f32006-11-22 14:55:48 +00003485 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003486 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003487 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003488
Jens Axboe40bb54d2009-04-15 12:11:10 +02003489 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09003490 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003491 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003492}
3493
3494/*
3495 * Timer running if the active_queue is currently idling inside its time slice
3496 */
3497static void cfq_idle_slice_timer(unsigned long data)
3498{
3499 struct cfq_data *cfqd = (struct cfq_data *) data;
3500 struct cfq_queue *cfqq;
3501 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003502 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003503
Jens Axboe7b679132008-05-30 12:23:07 +02003504 cfq_log(cfqd, "idle timer fired");
3505
Jens Axboe22e2c502005-06-27 10:55:12 +02003506 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3507
Jens Axboefe094d92008-01-31 13:08:54 +01003508 cfqq = cfqd->active_queue;
3509 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003510 timed_out = 0;
3511
Jens Axboe22e2c502005-06-27 10:55:12 +02003512 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003513 * We saw a request before the queue expired, let it through
3514 */
3515 if (cfq_cfqq_must_dispatch(cfqq))
3516 goto out_kick;
3517
3518 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003519 * expired
3520 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003521 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003522 goto expire;
3523
3524 /*
3525 * only expire and reinvoke request handler, if there are
3526 * other queues with pending requests
3527 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003528 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003529 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003530
3531 /*
3532 * not expired and it has a request pending, let it dispatch
3533 */
Jens Axboe75e50982009-04-07 08:56:14 +02003534 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003535 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003536
3537 /*
3538 * Queue depth flag is reset only when the idle didn't succeed
3539 */
3540 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003541 }
3542expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02003543 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003544out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003545 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003546out_cont:
3547 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3548}
3549
Jens Axboe3b181522005-06-27 10:56:24 +02003550static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3551{
3552 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003553 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003554}
Jens Axboe22e2c502005-06-27 10:55:12 +02003555
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003556static void cfq_put_async_queues(struct cfq_data *cfqd)
3557{
3558 int i;
3559
3560 for (i = 0; i < IOPRIO_BE_NR; i++) {
3561 if (cfqd->async_cfqq[0][i])
3562 cfq_put_queue(cfqd->async_cfqq[0][i]);
3563 if (cfqd->async_cfqq[1][i])
3564 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003565 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003566
3567 if (cfqd->async_idle_cfqq)
3568 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003569}
3570
Jens Axboeb374d182008-10-31 10:05:07 +01003571static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
Jens Axboe22e2c502005-06-27 10:55:12 +02003573 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003574 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003575
Jens Axboe3b181522005-06-27 10:56:24 +02003576 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003577
Al Virod9ff4182006-03-18 13:51:22 -05003578 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003579
Al Virod9ff4182006-03-18 13:51:22 -05003580 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02003581 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003582
3583 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05003584 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3585 struct cfq_io_context,
3586 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02003587
3588 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05003589 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02003590
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003591 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003592 cfq_release_cfq_groups(cfqd);
3593 blkiocg_del_blkio_group(&cfqd->root_group.blkg);
Jens Axboe15c31be2007-07-10 13:43:25 +02003594
Al Virod9ff4182006-03-18 13:51:22 -05003595 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003596
3597 cfq_shutdown_timer_wq(cfqd);
3598
Vivek Goyalb1c35762009-12-03 12:59:47 -05003599 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
3600 synchronize_rcu();
Al Viroa90d7422006-03-18 12:05:37 -05003601 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Jens Axboe165125e2007-07-24 09:28:11 +02003604static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 struct cfq_data *cfqd;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003607 int i, j;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003608 struct cfq_group *cfqg;
Vivek Goyal615f0252009-12-03 12:59:39 -05003609 struct cfq_rb_root *st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Christoph Lameter94f60302007-07-17 04:03:29 -07003611 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02003613 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003615 /* Init root service tree */
3616 cfqd->grp_service_tree = CFQ_RB_ROOT;
3617
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003618 /* Init root group */
3619 cfqg = &cfqd->root_group;
Vivek Goyal615f0252009-12-03 12:59:39 -05003620 for_each_cfqg_st(cfqg, i, j, st)
3621 *st = CFQ_RB_ROOT;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003622 RB_CLEAR_NODE(&cfqg->rb_node);
Jens Axboe26a2ac02009-04-23 12:13:27 +02003623
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003624 /* Give preference to root group over other groups */
3625 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3626
Vivek Goyal25fb5162009-12-03 12:59:46 -05003627#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyalb1c35762009-12-03 12:59:47 -05003628 /*
3629 * Take a reference to root group which we never drop. This is just
3630 * to make sure that cfq_put_cfqg() does not try to kfree root group
3631 */
3632 atomic_set(&cfqg->ref, 1);
Vivek Goyal22084192009-12-03 12:59:49 -05003633 blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg, (void *)cfqd,
3634 0);
Vivek Goyal25fb5162009-12-03 12:59:46 -05003635#endif
Jens Axboe26a2ac02009-04-23 12:13:27 +02003636 /*
3637 * Not strictly needed (since RB_ROOT just clears the node and we
3638 * zeroed cfqd on alloc), but better be safe in case someone decides
3639 * to add magic to the rb code
3640 */
3641 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3642 cfqd->prio_trees[i] = RB_ROOT;
3643
Jens Axboe6118b702009-06-30 09:34:12 +02003644 /*
3645 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3646 * Grab a permanent reference to it, so that the normal code flow
3647 * will not attempt to free it.
3648 */
3649 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3650 atomic_inc(&cfqd->oom_cfqq.ref);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003651 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
Jens Axboe6118b702009-06-30 09:34:12 +02003652
Al Virod9ff4182006-03-18 13:51:22 -05003653 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Jens Axboe22e2c502005-06-27 10:55:12 +02003657 init_timer(&cfqd->idle_slice_timer);
3658 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3659 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3660
Jens Axboe23e018a2009-10-05 08:52:35 +02003661 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003664 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3665 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 cfqd->cfq_back_max = cfq_back_max;
3667 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003668 cfqd->cfq_slice[0] = cfq_slice_async;
3669 cfqd->cfq_slice[1] = cfq_slice_sync;
3670 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3671 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003672 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003673 cfqd->hw_tag = -1;
Vivek Goyal365722b2009-10-03 15:21:27 +02003674 cfqd->last_end_sync_rq = jiffies;
Jens Axboebc1c1162006-06-08 08:49:06 +02003675 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
3678static void cfq_slab_kill(void)
3679{
Jens Axboed6de8be2008-05-28 14:46:59 +02003680 /*
3681 * Caller already ensured that pending RCU callbacks are completed,
3682 * so we should have no busy allocations at this point.
3683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 if (cfq_pool)
3685 kmem_cache_destroy(cfq_pool);
3686 if (cfq_ioc_pool)
3687 kmem_cache_destroy(cfq_ioc_pool);
3688}
3689
3690static int __init cfq_slab_setup(void)
3691{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003692 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 if (!cfq_pool)
3694 goto fail;
3695
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02003696 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (!cfq_ioc_pool)
3698 goto fail;
3699
3700 return 0;
3701fail:
3702 cfq_slab_kill();
3703 return -ENOMEM;
3704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706/*
3707 * sysfs parts below -->
3708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709static ssize_t
3710cfq_var_show(unsigned int var, char *page)
3711{
3712 return sprintf(page, "%d\n", var);
3713}
3714
3715static ssize_t
3716cfq_var_store(unsigned int *var, const char *page, size_t count)
3717{
3718 char *p = (char *) page;
3719
3720 *var = simple_strtoul(p, &p, 10);
3721 return count;
3722}
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003725static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003727 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 unsigned int __data = __VAR; \
3729 if (__CONV) \
3730 __data = jiffies_to_msecs(__data); \
3731 return cfq_var_show(__data, (page)); \
3732}
3733SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003734SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3735SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003736SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3737SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003738SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3739SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3740SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3741SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003742SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743#undef SHOW_FUNCTION
3744
3745#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003746static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003748 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 unsigned int __data; \
3750 int ret = cfq_var_store(&__data, (page), count); \
3751 if (__data < (MIN)) \
3752 __data = (MIN); \
3753 else if (__data > (MAX)) \
3754 __data = (MAX); \
3755 if (__CONV) \
3756 *(__PTR) = msecs_to_jiffies(__data); \
3757 else \
3758 *(__PTR) = __data; \
3759 return ret; \
3760}
3761STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003762STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3763 UINT_MAX, 1);
3764STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3765 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003766STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003767STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3768 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003769STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3770STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3771STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003772STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3773 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003774STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775#undef STORE_FUNCTION
3776
Al Viroe572ec72006-03-18 22:27:18 -05003777#define CFQ_ATTR(name) \
3778 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003779
Al Viroe572ec72006-03-18 22:27:18 -05003780static struct elv_fs_entry cfq_attrs[] = {
3781 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003782 CFQ_ATTR(fifo_expire_sync),
3783 CFQ_ATTR(fifo_expire_async),
3784 CFQ_ATTR(back_seek_max),
3785 CFQ_ATTR(back_seek_penalty),
3786 CFQ_ATTR(slice_sync),
3787 CFQ_ATTR(slice_async),
3788 CFQ_ATTR(slice_async_rq),
3789 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003790 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003791 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792};
3793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794static struct elevator_type iosched_cfq = {
3795 .ops = {
3796 .elevator_merge_fn = cfq_merge,
3797 .elevator_merged_fn = cfq_merged_request,
3798 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003799 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02003800 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003802 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 .elevator_deactivate_req_fn = cfq_deactivate_request,
3804 .elevator_queue_empty_fn = cfq_queue_empty,
3805 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003806 .elevator_former_req_fn = elv_rb_former_request,
3807 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 .elevator_set_req_fn = cfq_set_request,
3809 .elevator_put_req_fn = cfq_put_request,
3810 .elevator_may_queue_fn = cfq_may_queue,
3811 .elevator_init_fn = cfq_init_queue,
3812 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02003813 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 },
Al Viro3d1ab402006-03-18 18:35:43 -05003815 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 .elevator_name = "cfq",
3817 .elevator_owner = THIS_MODULE,
3818};
3819
3820static int __init cfq_init(void)
3821{
Jens Axboe22e2c502005-06-27 10:55:12 +02003822 /*
3823 * could be 0 on HZ < 1000 setups
3824 */
3825 if (!cfq_slice_async)
3826 cfq_slice_async = 1;
3827 if (!cfq_slice_idle)
3828 cfq_slice_idle = 1;
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (cfq_slab_setup())
3831 return -ENOMEM;
3832
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003833 elv_register(&iosched_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
3838static void __exit cfq_exit(void)
3839{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003840 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05003842 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02003843 /* ioc_gone's update must be visible before reading ioc_count */
3844 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02003845
3846 /*
3847 * this also protects us from entering cfq_slab_kill() with
3848 * pending RCU callbacks
3849 */
Tejun Heo245b2e72009-06-24 15:13:48 +09003850 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02003851 wait_for_completion(&all_gone);
Christoph Hellwig83521d32005-10-30 15:01:39 -08003852 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
3855module_init(cfq_init);
3856module_exit(cfq_exit);
3857
3858MODULE_AUTHOR("Jens Axboe");
3859MODULE_LICENSE("GPL");
3860MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");