blob: 86980023339a7273fb9aaff9825dfb662b5b0e19 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010013#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020016#include <linux/blktrace_api.h>
Tejun Heo6e736be2011-12-14 00:33:38 +010017#include "blk.h"
Vivek Goyale98ef892010-06-18 10:39:47 -040018#include "cfq.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Tejun Heo03814112012-03-05 13:15:14 -080020static struct blkio_policy_type blkio_policy_cfq;
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * tunables
24 */
Jens Axboefe094d92008-01-31 13:08:54 +010025/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010026static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010027static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010028/* maximum backwards seek, in KiB */
29static const int cfq_back_max = 16 * 1024;
30/* penalty of a backwards seek */
31static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010032static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020033static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020035static int cfq_slice_idle = HZ / 125;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +020036static int cfq_group_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010037static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
38static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020039
Jens Axboed9e76202007-04-20 14:27:50 +020040/*
Jens Axboe08717142008-01-28 11:38:15 +010041 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020042 */
Jens Axboe08717142008-01-28 11:38:15 +010043#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020044
45/*
46 * below this threshold, we consider thinktime immediate
47 */
48#define CFQ_MIN_TT (2)
49
Jens Axboe22e2c502005-06-27 10:55:12 +020050#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020051#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050052#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020053
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010054#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010055#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010056#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010057#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010058
Tejun Heoa612fdd2011-12-14 00:33:41 +010059#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
60#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
61#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Christoph Lametere18b8902006-12-06 20:33:20 -080063static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define CFQ_PRIO_LISTS IOPRIO_BE_NR
66#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020067#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
68
Jens Axboe206dc692006-03-28 13:03:44 +020069#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050070#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020071
Tejun Heoc5869802011-12-14 00:33:41 +010072struct cfq_ttime {
73 unsigned long last_end_request;
74
75 unsigned long ttime_total;
76 unsigned long ttime_samples;
77 unsigned long ttime_mean;
78};
79
Jens Axboe22e2c502005-06-27 10:55:12 +020080/*
Jens Axboecc09e292007-04-26 12:53:50 +020081 * Most of our rbtree usage is for sorting with min extraction, so
82 * if we cache the leftmost node we don't have to walk down the tree
83 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
84 * move this into the elevator for the rq sorting as well.
85 */
86struct cfq_rb_root {
87 struct rb_root rb;
88 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010089 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010090 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050091 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020092 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020093};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020094#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
95 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020096
97/*
Jens Axboe6118b702009-06-30 09:34:12 +020098 * Per process-grouping structure
99 */
100struct cfq_queue {
101 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100102 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200103 /* various state flags, see below */
104 unsigned int flags;
105 /* parent cfq_data */
106 struct cfq_data *cfqd;
107 /* service_tree member */
108 struct rb_node rb_node;
109 /* service_tree key */
110 unsigned long rb_key;
111 /* prio tree member */
112 struct rb_node p_node;
113 /* prio tree root we belong to, if any */
114 struct rb_root *p_root;
115 /* sorted list of pending requests */
116 struct rb_root sort_list;
117 /* if fifo isn't expired, next request to serve */
118 struct request *next_rq;
119 /* requests queued in sort_list */
120 int queued[2];
121 /* currently allocated requests */
122 int allocated[2];
123 /* fifo list of requests in sort_list */
124 struct list_head fifo;
125
Vivek Goyaldae739e2009-12-03 12:59:45 -0500126 /* time when queue got scheduled in to dispatch first request. */
127 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500128 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100129 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500130 /* time when first request from queue completed and slice started. */
131 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200132 unsigned long slice_end;
133 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200134
Christoph Hellwig65299a32011-08-23 14:50:29 +0200135 /* pending priority requests */
136 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200137 /* number of requests that are on the dispatch list or inside driver */
138 int dispatched;
139
140 /* io prio of this group */
141 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200142 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200143
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100144 pid_t pid;
145
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100146 u32 seek_history;
Jeff Moyerb2c18e1e2009-10-23 17:14:49 -0400147 sector_t last_request_pos;
148
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100149 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400150 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500151 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200152 /* Number of sectors dispatched from queue in single dispatch round */
153 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200154};
155
156/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100157 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100158 * IDLE is handled separately, so it has negative index
159 */
160enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100161 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500162 RT_WORKLOAD = 1,
163 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200164 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100165};
166
167/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100168 * Second index in the service_trees.
169 */
170enum wl_type_t {
171 ASYNC_WORKLOAD = 0,
172 SYNC_NOIDLE_WORKLOAD = 1,
173 SYNC_WORKLOAD = 2
174};
175
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500176/* This is per cgroup per device grouping structure */
177struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500178 /* group service_tree member */
179 struct rb_node rb_node;
180
181 /* group service_tree key */
182 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500183 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100184 unsigned int new_weight;
185 bool needs_update;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500186
187 /* number of cfqq currently on this group */
188 int nr_cfqq;
189
Jens Axboe22e2c502005-06-27 10:55:12 +0200190 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200191 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200192 * create the array for each prio class but at run time it is used
193 * only for RT and BE class and slot for IDLE class remains unused.
194 * This is primarily done to avoid confusion and a gcc warning.
195 */
196 unsigned int busy_queues_avg[CFQ_PRIO_NR];
197 /*
198 * rr lists of queues with requests. We maintain service trees for
199 * RT and BE classes. These trees are subdivided in subclasses
200 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
201 * class there is no subclassification and all the cfq queues go on
202 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100203 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200204 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100205 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100206 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500207
208 unsigned long saved_workload_slice;
209 enum wl_type_t saved_workload;
210 enum wl_prio_t saved_serving_prio;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500211#ifdef CONFIG_CFQ_GROUP_IOSCHED
212 struct hlist_node cfqd_node;
213#endif
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200214 /* number of requests that are on the dispatch list or inside driver */
215 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200216 struct cfq_ttime ttime;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500217};
218
Tejun Heoc5869802011-12-14 00:33:41 +0100219struct cfq_io_cq {
220 struct io_cq icq; /* must be the first member */
221 struct cfq_queue *cfqq[2];
222 struct cfq_ttime ttime;
223};
224
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500225/*
226 * Per block device queue structure
227 */
228struct cfq_data {
229 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500230 /* Root service tree for cfq_groups */
231 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800232 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500233
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100234 /*
235 * The priority currently being served
236 */
237 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100238 enum wl_type_t serving_type;
239 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500240 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200241
242 /*
243 * Each priority tree is sorted by next_request position. These
244 * trees are used when determining if two or more queues are
245 * interleaving requests (see cfq_close_cooperator).
246 */
247 struct rb_root prio_trees[CFQ_PRIO_LISTS];
248
Jens Axboe22e2c502005-06-27 10:55:12 +0200249 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100250 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200251
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100252 int rq_in_driver;
253 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200254
255 /*
256 * queue-depth detection
257 */
258 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200259 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100260 /*
261 * hw_tag can be
262 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
263 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
264 * 0 => no NCQ
265 */
266 int hw_tag_est_depth;
267 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200268
269 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200270 * idle window management
271 */
272 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200273 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200274
275 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100276 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200277
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200278 /*
279 * async queue for each priority case
280 */
281 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
282 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200283
Jens Axboe6d048f52007-04-25 12:44:27 +0200284 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /*
287 * tunables, see top of file
288 */
289 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200290 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned int cfq_back_penalty;
292 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200293 unsigned int cfq_slice[2];
294 unsigned int cfq_slice_async_rq;
295 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200296 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200297 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500298
Jens Axboe6118b702009-06-30 09:34:12 +0200299 /*
300 * Fallback dummy cfqq for extreme OOM conditions
301 */
302 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200303
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100304 unsigned long last_delayed_sync;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500305
306 /* List of cfq groups being managed on this device*/
307 struct hlist_head cfqg_list;
Vivek Goyal56edf7d2011-05-19 15:38:22 -0400308
309 /* Number of groups which are on blkcg->blkg_list */
310 unsigned int nr_blkcg_linked_grps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Tejun Heo03814112012-03-05 13:15:14 -0800313static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
314{
315 return blkg_to_pdata(blkg, &blkio_policy_cfq);
316}
317
318static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
319{
320 return pdata_to_blkg(cfqg, &blkio_policy_cfq);
321}
322
Vivek Goyal25fb5162009-12-03 12:59:46 -0500323static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
324
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500325static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
326 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500327 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100328{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500329 if (!cfqg)
330 return NULL;
331
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100332 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500333 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100334
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500335 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100336}
337
Jens Axboe3b181522005-06-27 10:56:24 +0200338enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100339 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
340 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200341 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100342 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100343 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
344 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
345 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100346 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200347 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400348 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100349 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100350 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500351 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200352};
353
354#define CFQ_CFQQ_FNS(name) \
355static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
356{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100357 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200358} \
359static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
360{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100361 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200362} \
363static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
364{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100365 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200366}
367
368CFQ_CFQQ_FNS(on_rr);
369CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200370CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200371CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200372CFQ_CFQQ_FNS(fifo_expire);
373CFQ_CFQQ_FNS(idle_window);
374CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100375CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200376CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200377CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100378CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100379CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500380CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200381#undef CFQ_CFQQ_FNS
382
Vivek Goyalafc24d42010-04-26 19:27:56 +0200383#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal2868ef72009-12-03 12:59:48 -0500384#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
385 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
386 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
Tejun Heo03814112012-03-05 13:15:14 -0800387 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500388
389#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
390 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800391 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
Vivek Goyal2868ef72009-12-03 12:59:48 -0500392
393#else
Jens Axboe7b679132008-05-30 12:23:07 +0200394#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
395 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200396#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500397#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200398#define cfq_log(cfqd, fmt, args...) \
399 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
400
Vivek Goyal615f0252009-12-03 12:59:39 -0500401/* Traverses through cfq group service trees */
402#define for_each_cfqg_st(cfqg, i, j, st) \
403 for (i = 0; i <= IDLE_WORKLOAD; i++) \
404 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
405 : &cfqg->service_tree_idle; \
406 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
407 (i == IDLE_WORKLOAD && j == 0); \
408 j++, st = i < IDLE_WORKLOAD ? \
409 &cfqg->service_trees[i][j]: NULL) \
410
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200411static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
412 struct cfq_ttime *ttime, bool group_idle)
413{
414 unsigned long slice;
415 if (!sample_valid(ttime->ttime_samples))
416 return false;
417 if (group_idle)
418 slice = cfqd->cfq_group_idle;
419 else
420 slice = cfqd->cfq_slice_idle;
421 return ttime->ttime_mean > slice;
422}
Vivek Goyal615f0252009-12-03 12:59:39 -0500423
Vivek Goyal02b35082010-08-23 12:23:53 +0200424static inline bool iops_mode(struct cfq_data *cfqd)
425{
426 /*
427 * If we are not idling on queues and it is a NCQ drive, parallel
428 * execution of requests is on and measuring time is not possible
429 * in most of the cases until and unless we drive shallower queue
430 * depths and that becomes a performance bottleneck. In such cases
431 * switch to start providing fairness in terms of number of IOs.
432 */
433 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
434 return true;
435 else
436 return false;
437}
438
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100439static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
440{
441 if (cfq_class_idle(cfqq))
442 return IDLE_WORKLOAD;
443 if (cfq_class_rt(cfqq))
444 return RT_WORKLOAD;
445 return BE_WORKLOAD;
446}
447
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100448
449static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
450{
451 if (!cfq_cfqq_sync(cfqq))
452 return ASYNC_WORKLOAD;
453 if (!cfq_cfqq_idle_window(cfqq))
454 return SYNC_NOIDLE_WORKLOAD;
455 return SYNC_WORKLOAD;
456}
457
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500458static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
459 struct cfq_data *cfqd,
460 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100461{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500462 if (wl == IDLE_WORKLOAD)
463 return cfqg->service_tree_idle.count;
464
465 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
466 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
467 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100468}
469
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500470static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
471 struct cfq_group *cfqg)
472{
473 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
474 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
475}
476
Jens Axboe165125e2007-07-24 09:28:11 +0200477static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200478static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100479 struct io_context *, gfp_t);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200480
Tejun Heoc5869802011-12-14 00:33:41 +0100481static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
482{
483 /* cic->icq is the first member, %NULL will convert to %NULL */
484 return container_of(icq, struct cfq_io_cq, icq);
485}
486
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100487static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
488 struct io_context *ioc)
489{
490 if (ioc)
491 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
492 return NULL;
493}
494
Tejun Heoc5869802011-12-14 00:33:41 +0100495static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200496{
Jens Axboea6151c32009-10-07 20:02:57 +0200497 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200498}
499
Tejun Heoc5869802011-12-14 00:33:41 +0100500static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
501 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200502{
Jens Axboea6151c32009-10-07 20:02:57 +0200503 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200504}
505
Tejun Heoc5869802011-12-14 00:33:41 +0100506static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400507{
Tejun Heoc5869802011-12-14 00:33:41 +0100508 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400509}
510
Vasily Tarasov91fac312007-04-25 12:29:51 +0200511/*
512 * We regard a request as SYNC, if it's either a read or has the SYNC bit
513 * set (in which case it could also be direct WRITE).
514 */
Jens Axboea6151c32009-10-07 20:02:57 +0200515static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200516{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200517 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700521 * scheduler run of queue, if there are requests pending and no one in the
522 * driver that will restart queueing
523 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200524static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700525{
Jens Axboe7b679132008-05-30 12:23:07 +0200526 if (cfqd->busy_queues) {
527 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200528 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200529 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100533 * Scale schedule slice based on io priority. Use the sync time slice only
534 * if a queue is marked sync and has sync io queued. A sync queue with async
535 * io only, should not get full sync slice length.
536 */
Jens Axboea6151c32009-10-07 20:02:57 +0200537static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200538 unsigned short prio)
539{
540 const int base_slice = cfqd->cfq_slice[sync];
541
542 WARN_ON(prio >= IOPRIO_BE_NR);
543
544 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
545}
546
Jens Axboe44f7c162007-01-19 11:51:58 +1100547static inline int
548cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
549{
Jens Axboed9e76202007-04-20 14:27:50 +0200550 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100551}
552
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500553static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
554{
555 u64 d = delta << CFQ_SERVICE_SHIFT;
556
557 d = d * BLKIO_WEIGHT_DEFAULT;
558 do_div(d, cfqg->weight);
559 return d;
560}
561
562static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
563{
564 s64 delta = (s64)(vdisktime - min_vdisktime);
565 if (delta > 0)
566 min_vdisktime = vdisktime;
567
568 return min_vdisktime;
569}
570
571static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
572{
573 s64 delta = (s64)(vdisktime - min_vdisktime);
574 if (delta < 0)
575 min_vdisktime = vdisktime;
576
577 return min_vdisktime;
578}
579
580static void update_min_vdisktime(struct cfq_rb_root *st)
581{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500582 struct cfq_group *cfqg;
583
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500584 if (st->left) {
585 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100586 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
587 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500588 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500589}
590
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100591/*
592 * get averaged number of queues of RT/BE priority.
593 * average is updated, with a formula that gives more weight to higher numbers,
594 * to quickly follows sudden increases and decrease slowly
595 */
596
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500597static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
598 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100599{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100600 unsigned min_q, max_q;
601 unsigned mult = cfq_hist_divisor - 1;
602 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500603 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100604
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500605 min_q = min(cfqg->busy_queues_avg[rt], busy);
606 max_q = max(cfqg->busy_queues_avg[rt], busy);
607 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100608 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500609 return cfqg->busy_queues_avg[rt];
610}
611
612static inline unsigned
613cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
614{
615 struct cfq_rb_root *st = &cfqd->grp_service_tree;
616
617 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100618}
619
Shaohua Lic553f8e2011-01-14 08:41:03 +0100620static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700621cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100622{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100623 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
624 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500625 /*
626 * interested queues (we consider only the ones with the same
627 * priority class in the cfq group)
628 */
629 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
630 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100631 unsigned sync_slice = cfqd->cfq_slice[1];
632 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500633 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
634
635 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100636 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
637 /* scale low_slice according to IO priority
638 * and sync vs async */
639 unsigned low_slice =
640 min(slice, base_low_slice * slice / sync_slice);
641 /* the adapted slice value is scaled to fit all iqs
642 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500643 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100644 low_slice);
645 }
646 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100647 return slice;
648}
649
650static inline void
651cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
652{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700653 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100654
Vivek Goyaldae739e2009-12-03 12:59:45 -0500655 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100656 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500657 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200658 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100659}
660
661/*
662 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
663 * isn't valid until the first request from the dispatch is activated
664 * and the slice time set.
665 */
Jens Axboea6151c32009-10-07 20:02:57 +0200666static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100667{
668 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100669 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100670 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100671 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100672
Shaohua Lic1e44752010-11-08 15:01:02 +0100673 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100674}
675
676/*
Jens Axboe5e705372006-07-13 12:39:25 +0200677 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200679 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Jens Axboe5e705372006-07-13 12:39:25 +0200681static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100682cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100684 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200686#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
687#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
688 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jens Axboe5e705372006-07-13 12:39:25 +0200690 if (rq1 == NULL || rq1 == rq2)
691 return rq2;
692 if (rq2 == NULL)
693 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200694
Namhyung Kim229836b2011-05-24 10:23:21 +0200695 if (rq_is_sync(rq1) != rq_is_sync(rq2))
696 return rq_is_sync(rq1) ? rq1 : rq2;
697
Christoph Hellwig65299a32011-08-23 14:50:29 +0200698 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
699 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +0200700
Tejun Heo83096eb2009-05-07 22:24:39 +0900701 s1 = blk_rq_pos(rq1);
702 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * by definition, 1KiB is 2 sectors
706 */
707 back_max = cfqd->cfq_back_max * 2;
708
709 /*
710 * Strict one way elevator _except_ in the case where we allow
711 * short backward seeks which are biased as twice the cost of a
712 * similar forward seek.
713 */
714 if (s1 >= last)
715 d1 = s1 - last;
716 else if (s1 + back_max >= last)
717 d1 = (last - s1) * cfqd->cfq_back_penalty;
718 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200719 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (s2 >= last)
722 d2 = s2 - last;
723 else if (s2 + back_max >= last)
724 d2 = (last - s2) * cfqd->cfq_back_penalty;
725 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200726 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Andreas Mohre8a99052006-03-28 08:59:49 +0200730 /*
731 * By doing switch() on the bit mask "wrap" we avoid having to
732 * check two variables for all permutations: --> faster!
733 */
734 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200735 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200736 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200737 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200738 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200739 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200740 else {
741 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200742 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200743 else
Jens Axboe5e705372006-07-13 12:39:25 +0200744 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200745 }
746
747 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200748 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200749 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200750 return rq2;
751 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200752 default:
753 /*
754 * Since both rqs are wrapped,
755 * start with the one that's further behind head
756 * (--> only *one* back seek required),
757 * since back seek takes more time than forward.
758 */
759 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200760 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 else
Jens Axboe5e705372006-07-13 12:39:25 +0200762 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764}
765
Jens Axboe498d3aa22007-04-26 12:54:48 +0200766/*
767 * The below is leftmost cache rbtree addon
768 */
Jens Axboe08717142008-01-28 11:38:15 +0100769static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200770{
Vivek Goyal615f0252009-12-03 12:59:39 -0500771 /* Service tree is empty */
772 if (!root->count)
773 return NULL;
774
Jens Axboecc09e292007-04-26 12:53:50 +0200775 if (!root->left)
776 root->left = rb_first(&root->rb);
777
Jens Axboe08717142008-01-28 11:38:15 +0100778 if (root->left)
779 return rb_entry(root->left, struct cfq_queue, rb_node);
780
781 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200782}
783
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500784static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
785{
786 if (!root->left)
787 root->left = rb_first(&root->rb);
788
789 if (root->left)
790 return rb_entry_cfqg(root->left);
791
792 return NULL;
793}
794
Jens Axboea36e71f2009-04-15 12:15:11 +0200795static void rb_erase_init(struct rb_node *n, struct rb_root *root)
796{
797 rb_erase(n, root);
798 RB_CLEAR_NODE(n);
799}
800
Jens Axboecc09e292007-04-26 12:53:50 +0200801static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
802{
803 if (root->left == n)
804 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200805 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100806 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
810 * would be nice to take fifo expire time into account as well
811 */
Jens Axboe5e705372006-07-13 12:39:25 +0200812static struct request *
813cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
814 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jens Axboe21183b02006-07-13 12:33:14 +0200816 struct rb_node *rbnext = rb_next(&last->rb_node);
817 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200818 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Jens Axboe21183b02006-07-13 12:33:14 +0200820 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200823 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200826 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200827 else {
828 rbnext = rb_first(&cfqq->sort_list);
829 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200830 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100833 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Jens Axboed9e76202007-04-20 14:27:50 +0200836static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
837 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Jens Axboed9e76202007-04-20 14:27:50 +0200839 /*
840 * just an approximation, should be ok.
841 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500842 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100843 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500846static inline s64
847cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
848{
849 return cfqg->vdisktime - st->min_vdisktime;
850}
851
852static void
853__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
854{
855 struct rb_node **node = &st->rb.rb_node;
856 struct rb_node *parent = NULL;
857 struct cfq_group *__cfqg;
858 s64 key = cfqg_key(st, cfqg);
859 int left = 1;
860
861 while (*node != NULL) {
862 parent = *node;
863 __cfqg = rb_entry_cfqg(parent);
864
865 if (key < cfqg_key(st, __cfqg))
866 node = &parent->rb_left;
867 else {
868 node = &parent->rb_right;
869 left = 0;
870 }
871 }
872
873 if (left)
874 st->left = &cfqg->rb_node;
875
876 rb_link_node(&cfqg->rb_node, parent, node);
877 rb_insert_color(&cfqg->rb_node, &st->rb);
878}
879
880static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100881cfq_update_group_weight(struct cfq_group *cfqg)
882{
883 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
884 if (cfqg->needs_update) {
885 cfqg->weight = cfqg->new_weight;
886 cfqg->needs_update = false;
887 }
888}
889
890static void
891cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
892{
893 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
894
895 cfq_update_group_weight(cfqg);
896 __cfq_group_service_tree_add(st, cfqg);
897 st->total_weight += cfqg->weight;
898}
899
900static void
901cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500902{
903 struct cfq_rb_root *st = &cfqd->grp_service_tree;
904 struct cfq_group *__cfqg;
905 struct rb_node *n;
906
907 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +0100908 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500909 return;
910
911 /*
912 * Currently put the group at the end. Later implement something
913 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300914 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500915 */
916 n = rb_last(&st->rb);
917 if (n) {
918 __cfqg = rb_entry_cfqg(n);
919 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
920 } else
921 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +0100922 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500923}
924
925static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100926cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
927{
928 st->total_weight -= cfqg->weight;
929 if (!RB_EMPTY_NODE(&cfqg->rb_node))
930 cfq_rb_erase(&cfqg->rb_node, st);
931}
932
933static void
934cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500935{
936 struct cfq_rb_root *st = &cfqd->grp_service_tree;
937
938 BUG_ON(cfqg->nr_cfqq < 1);
939 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500940
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500941 /* If there are other cfq queues under this group, don't delete it */
942 if (cfqg->nr_cfqq)
943 return;
944
Vivek Goyal2868ef72009-12-03 12:59:48 -0500945 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +0100946 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500947 cfqg->saved_workload_slice = 0;
Tejun Heo03814112012-03-05 13:15:14 -0800948 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg), 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500949}
950
Justin TerAvest167400d2011-03-12 16:54:00 +0100951static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
952 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500953{
Vivek Goyalf75edf22009-12-03 12:59:53 -0500954 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500955
956 /*
957 * Queue got expired before even a single request completed or
958 * got expired immediately after first request completion.
959 */
960 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
961 /*
962 * Also charge the seek time incurred to the group, otherwise
963 * if there are mutiple queues in the group, each can dispatch
964 * a single request on seeky media and cause lots of seek time
965 * and group will never know it.
966 */
967 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
968 1);
969 } else {
970 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +0100971 if (slice_used > cfqq->allocated_slice) {
972 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500973 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +0100974 }
975 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
976 *unaccounted_time += cfqq->slice_start -
977 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500978 }
979
Vivek Goyaldae739e2009-12-03 12:59:45 -0500980 return slice_used;
981}
982
983static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +0200984 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500985{
986 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +0100987 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500988 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
989 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500990
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500991 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +0100992 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500993
Vivek Goyal02b35082010-08-23 12:23:53 +0200994 if (iops_mode(cfqd))
995 charge = cfqq->slice_dispatch;
996 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
997 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500998
999 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001000 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001001 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001002 /* If a new weight was requested, update now, off tree */
1003 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001004
1005 /* This group is being expired. Save the context */
1006 if (time_after(cfqd->workload_expires, jiffies)) {
1007 cfqg->saved_workload_slice = cfqd->workload_expires
1008 - jiffies;
1009 cfqg->saved_workload = cfqd->serving_type;
1010 cfqg->saved_serving_prio = cfqd->serving_prio;
1011 } else
1012 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001013
1014 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1015 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001016 cfq_log_cfqq(cfqq->cfqd, cfqq,
1017 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1018 used_sl, cfqq->slice_dispatch, charge,
1019 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo03814112012-03-05 13:15:14 -08001020 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), used_sl,
Justin TerAvest167400d2011-03-12 16:54:00 +01001021 unaccounted_sl);
Tejun Heo03814112012-03-05 13:15:14 -08001022 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg));
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001023}
1024
Tejun Heof51b8022012-03-05 13:15:05 -08001025/**
1026 * cfq_init_cfqg_base - initialize base part of a cfq_group
1027 * @cfqg: cfq_group to initialize
1028 *
1029 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1030 * is enabled or not.
1031 */
1032static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1033{
1034 struct cfq_rb_root *st;
1035 int i, j;
1036
1037 for_each_cfqg_st(cfqg, i, j, st)
1038 *st = CFQ_RB_ROOT;
1039 RB_CLEAR_NODE(&cfqg->rb_node);
1040
1041 cfqg->ttime.last_end_request = jiffies;
1042}
1043
Vivek Goyal25fb5162009-12-03 12:59:46 -05001044#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoca32aef2012-03-05 13:15:03 -08001045static void cfq_update_blkio_group_weight(struct request_queue *q,
1046 struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001047 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001048{
Tejun Heo03814112012-03-05 13:15:14 -08001049 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1050
Justin TerAvest8184f932011-03-17 16:12:36 +01001051 cfqg->new_weight = weight;
1052 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001053}
1054
Tejun Heocd1604f2012-03-05 13:15:06 -08001055static void cfq_link_blkio_group(struct request_queue *q,
1056 struct blkio_group *blkg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001057{
Tejun Heocd1604f2012-03-05 13:15:06 -08001058 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heo03814112012-03-05 13:15:14 -08001059 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001060
1061 cfqd->nr_blkcg_linked_grps++;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001062
1063 /* Add group on cfqd list */
1064 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
1065}
1066
Tejun Heo03814112012-03-05 13:15:14 -08001067static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001068{
Tejun Heo03814112012-03-05 13:15:14 -08001069 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001070
Tejun Heof51b8022012-03-05 13:15:05 -08001071 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001072 cfqg->weight = blkg->blkcg->weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001073}
1074
1075/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001076 * Search for the cfq group current task belongs to. request_queue lock must
1077 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001078 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001079static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1080 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001081{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001082 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001083 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001084
Tejun Heocd1604f2012-03-05 13:15:06 -08001085 /* avoid lookup for the common case where there's no blkio cgroup */
1086 if (blkcg == &blkio_root_cgroup) {
1087 cfqg = cfqd->root_group;
1088 } else {
1089 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001090
Tejun Heocd1604f2012-03-05 13:15:06 -08001091 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_PROP, false);
1092 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001093 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001094 }
1095
Vivek Goyal25fb5162009-12-03 12:59:46 -05001096 return cfqg;
1097}
1098
1099static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1100{
1101 /* Currently, all async queues are mapped to root group */
1102 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001103 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001104
1105 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001106 /* cfqq reference on cfqg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -08001107 blkg_get(cfqg_to_blkg(cfqg));
Vivek Goyalb1c35762009-12-03 12:59:47 -05001108}
1109
1110static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1111{
1112 /* Something wrong if we are trying to remove same group twice */
1113 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1114
1115 hlist_del_init(&cfqg->cfqd_node);
1116
Vivek Goyala5395b82011-08-02 09:24:09 +02001117 BUG_ON(cfqd->nr_blkcg_linked_grps <= 0);
1118 cfqd->nr_blkcg_linked_grps--;
1119
Vivek Goyalb1c35762009-12-03 12:59:47 -05001120 /*
1121 * Put the reference taken at the time of creation so that when all
1122 * queues are gone, group can be destroyed.
1123 */
Tejun Heo1adaf3d2012-03-05 13:15:15 -08001124 blkg_put(cfqg_to_blkg(cfqg));
Vivek Goyalb1c35762009-12-03 12:59:47 -05001125}
1126
Tejun Heo72e06c22012-03-05 13:15:00 -08001127static bool cfq_release_cfq_groups(struct cfq_data *cfqd)
Vivek Goyalb1c35762009-12-03 12:59:47 -05001128{
1129 struct hlist_node *pos, *n;
1130 struct cfq_group *cfqg;
Tejun Heo72e06c22012-03-05 13:15:00 -08001131 bool empty = true;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001132
1133 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1134 /*
1135 * If cgroup removal path got to blk_group first and removed
1136 * it from cgroup list, then it will take care of destroying
1137 * cfqg also.
1138 */
Tejun Heo03814112012-03-05 13:15:14 -08001139 if (!cfq_blkiocg_del_blkio_group(cfqg_to_blkg(cfqg)))
Vivek Goyalb1c35762009-12-03 12:59:47 -05001140 cfq_destroy_cfqg(cfqd, cfqg);
Tejun Heo72e06c22012-03-05 13:15:00 -08001141 else
1142 empty = false;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001143 }
Tejun Heo72e06c22012-03-05 13:15:00 -08001144 return empty;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001145}
1146
1147/*
1148 * Blk cgroup controller notification saying that blkio_group object is being
1149 * delinked as associated cgroup object is going away. That also means that
1150 * no new IO will come in this group. So get rid of this group as soon as
1151 * any pending IO in the group is finished.
1152 *
1153 * This function is called under rcu_read_lock(). key is the rcu protected
Tejun Heoca32aef2012-03-05 13:15:03 -08001154 * pointer. That means @q is a valid request_queue pointer as long as we
1155 * are rcu read lock.
Vivek Goyalb1c35762009-12-03 12:59:47 -05001156 *
Tejun Heoca32aef2012-03-05 13:15:03 -08001157 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
Vivek Goyalb1c35762009-12-03 12:59:47 -05001158 * it should not be NULL as even if elevator was exiting, cgroup deltion
1159 * path got to it first.
1160 */
Tejun Heoca32aef2012-03-05 13:15:03 -08001161static void cfq_unlink_blkio_group(struct request_queue *q,
1162 struct blkio_group *blkg)
Vivek Goyalb1c35762009-12-03 12:59:47 -05001163{
Tejun Heoca32aef2012-03-05 13:15:03 -08001164 struct cfq_data *cfqd = q->elevator->elevator_data;
1165 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001166
Tejun Heoca32aef2012-03-05 13:15:03 -08001167 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo03814112012-03-05 13:15:14 -08001168 cfq_destroy_cfqg(cfqd, blkg_to_cfqg(blkg));
Tejun Heoca32aef2012-03-05 13:15:03 -08001169 spin_unlock_irqrestore(q->queue_lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001170}
1171
Tejun Heo72e06c22012-03-05 13:15:00 -08001172static struct elevator_type iosched_cfq;
1173
1174static bool cfq_clear_queue(struct request_queue *q)
1175{
1176 lockdep_assert_held(q->queue_lock);
1177
1178 /* shoot down blkgs iff the current elevator is cfq */
1179 if (!q->elevator || q->elevator->type != &iosched_cfq)
1180 return true;
1181
1182 return cfq_release_cfq_groups(q->elevator->elevator_data);
1183}
1184
Vivek Goyal25fb5162009-12-03 12:59:46 -05001185#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001186static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1187 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001188{
Tejun Heof51b8022012-03-05 13:15:05 -08001189 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001190}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001191
Vivek Goyal25fb5162009-12-03 12:59:46 -05001192static inline void
1193cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1194 cfqq->cfqg = cfqg;
1195}
1196
Vivek Goyalb1c35762009-12-03 12:59:47 -05001197static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
Vivek Goyalb1c35762009-12-03 12:59:47 -05001198
Vivek Goyal25fb5162009-12-03 12:59:46 -05001199#endif /* GROUP_IOSCHED */
1200
Jens Axboe498d3aa22007-04-26 12:54:48 +02001201/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001202 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001203 * requests waiting to be processed. It is sorted in the order that
1204 * we will service the queues.
1205 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001206static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001207 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001208{
Jens Axboe08717142008-01-28 11:38:15 +01001209 struct rb_node **p, *parent;
1210 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001211 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001212 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001213 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001214 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001215
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001216 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001217 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001218 if (cfq_class_idle(cfqq)) {
1219 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001220 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001221 if (parent && parent != &cfqq->rb_node) {
1222 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1223 rb_key += __cfqq->rb_key;
1224 } else
1225 rb_key += jiffies;
1226 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001227 /*
1228 * Get our rb key offset. Subtract any residual slice
1229 * value carried from last service. A negative resid
1230 * count indicates slice overrun, and this should position
1231 * the next service time further away in the tree.
1232 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001233 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001234 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001235 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001236 } else {
1237 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001238 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001239 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1240 }
Jens Axboed9e76202007-04-20 14:27:50 +02001241
1242 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001243 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001244 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001245 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001246 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001247 if (rb_key == cfqq->rb_key &&
1248 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001249 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001250
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001251 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1252 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001253 }
Jens Axboed9e76202007-04-20 14:27:50 +02001254
Jens Axboe498d3aa22007-04-26 12:54:48 +02001255 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001256 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001257 cfqq->service_tree = service_tree;
1258 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001259 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001260 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001261
Jens Axboed9e76202007-04-20 14:27:50 +02001262 parent = *p;
1263 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1264
Jens Axboe0c534e02007-04-18 20:01:57 +02001265 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001266 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001267 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001268 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001269 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001270 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001271 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001272 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001273 }
Jens Axboe67060e32007-04-18 20:13:32 +02001274
1275 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001276 }
1277
Jens Axboecc09e292007-04-26 12:53:50 +02001278 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001279 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001280
Jens Axboed9e76202007-04-20 14:27:50 +02001281 cfqq->rb_key = rb_key;
1282 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001283 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1284 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001285 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001286 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001287 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Jens Axboea36e71f2009-04-15 12:15:11 +02001290static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001291cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1292 sector_t sector, struct rb_node **ret_parent,
1293 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001294{
Jens Axboea36e71f2009-04-15 12:15:11 +02001295 struct rb_node **p, *parent;
1296 struct cfq_queue *cfqq = NULL;
1297
1298 parent = NULL;
1299 p = &root->rb_node;
1300 while (*p) {
1301 struct rb_node **n;
1302
1303 parent = *p;
1304 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1305
1306 /*
1307 * Sort strictly based on sector. Smallest to the left,
1308 * largest to the right.
1309 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001310 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001311 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001312 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001313 n = &(*p)->rb_left;
1314 else
1315 break;
1316 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001317 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001318 }
1319
1320 *ret_parent = parent;
1321 if (rb_link)
1322 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001323 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001324}
1325
1326static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1327{
Jens Axboea36e71f2009-04-15 12:15:11 +02001328 struct rb_node **p, *parent;
1329 struct cfq_queue *__cfqq;
1330
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001331 if (cfqq->p_root) {
1332 rb_erase(&cfqq->p_node, cfqq->p_root);
1333 cfqq->p_root = NULL;
1334 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001335
1336 if (cfq_class_idle(cfqq))
1337 return;
1338 if (!cfqq->next_rq)
1339 return;
1340
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001341 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001342 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1343 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001344 if (!__cfqq) {
1345 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001346 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1347 } else
1348 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001349}
1350
Jens Axboe498d3aa22007-04-26 12:54:48 +02001351/*
1352 * Update cfqq's position in the service tree.
1353 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001354static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001355{
Jens Axboe6d048f52007-04-25 12:44:27 +02001356 /*
1357 * Resorting requires the cfqq to be on the RR list already.
1358 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001359 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001360 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001361 cfq_prio_tree_add(cfqd, cfqq);
1362 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*
1366 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001367 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Jens Axboefebffd62008-01-28 13:19:43 +01001369static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Jens Axboe7b679132008-05-30 12:23:07 +02001371 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001372 BUG_ON(cfq_cfqq_on_rr(cfqq));
1373 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001375 if (cfq_cfqq_sync(cfqq))
1376 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Jens Axboeedd75ff2007-04-19 12:03:34 +02001378 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Jens Axboe498d3aa22007-04-26 12:54:48 +02001381/*
1382 * Called when the cfqq no longer has requests pending, remove it from
1383 * the service tree.
1384 */
Jens Axboefebffd62008-01-28 13:19:43 +01001385static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Jens Axboe7b679132008-05-30 12:23:07 +02001387 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001388 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1389 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001391 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1392 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1393 cfqq->service_tree = NULL;
1394 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001395 if (cfqq->p_root) {
1396 rb_erase(&cfqq->p_node, cfqq->p_root);
1397 cfqq->p_root = NULL;
1398 }
Jens Axboed9e76202007-04-20 14:27:50 +02001399
Justin TerAvest8184f932011-03-17 16:12:36 +01001400 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 BUG_ON(!cfqd->busy_queues);
1402 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001403 if (cfq_cfqq_sync(cfqq))
1404 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
1407/*
1408 * rb tree support functions
1409 */
Jens Axboefebffd62008-01-28 13:19:43 +01001410static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Jens Axboe5e705372006-07-13 12:39:25 +02001412 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001413 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Jens Axboeb4878f22005-10-20 16:42:29 +02001415 BUG_ON(!cfqq->queued[sync]);
1416 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Jens Axboe5e705372006-07-13 12:39:25 +02001418 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Vivek Goyalf04a6422009-12-03 12:59:40 -05001420 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1421 /*
1422 * Queue will be deleted from service tree when we actually
1423 * expire it later. Right now just remove it from prio tree
1424 * as it is empty.
1425 */
1426 if (cfqq->p_root) {
1427 rb_erase(&cfqq->p_node, cfqq->p_root);
1428 cfqq->p_root = NULL;
1429 }
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Jens Axboe5e705372006-07-13 12:39:25 +02001433static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Jens Axboe5e705372006-07-13 12:39:25 +02001435 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001437 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jens Axboe5380a102006-07-13 12:37:56 +02001439 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jeff Moyer796d5112011-06-02 21:19:05 +02001441 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001442
1443 if (!cfq_cfqq_on_rr(cfqq))
1444 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001445
1446 /*
1447 * check if this request is a better next-serve candidate
1448 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001449 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001450 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001451
1452 /*
1453 * adjust priority tree position, if ->next_rq changes
1454 */
1455 if (prev != cfqq->next_rq)
1456 cfq_prio_tree_add(cfqd, cfqq);
1457
Jens Axboe5044eed2007-04-25 11:53:48 +02001458 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Jens Axboefebffd62008-01-28 13:19:43 +01001461static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Jens Axboe5380a102006-07-13 12:37:56 +02001463 elv_rb_del(&cfqq->sort_list, rq);
1464 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001465 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001466 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001467 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001468 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1469 cfqg_to_blkg(cfqq->cfqd->serving_group),
1470 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Jens Axboe206dc692006-03-28 13:03:44 +02001473static struct request *
1474cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Jens Axboe206dc692006-03-28 13:03:44 +02001476 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001477 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001478 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jens Axboe4ac845a2008-01-24 08:44:49 +01001480 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001481 if (!cic)
1482 return NULL;
1483
1484 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001485 if (cfqq) {
1486 sector_t sector = bio->bi_sector + bio_sectors(bio);
1487
Jens Axboe21183b02006-07-13 12:33:14 +02001488 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return NULL;
1492}
1493
Jens Axboe165125e2007-07-24 09:28:11 +02001494static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001495{
1496 struct cfq_data *cfqd = q->elevator->elevator_data;
1497
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001498 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001499 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001500 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001501
Tejun Heo5b936292009-05-07 22:24:38 +09001502 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001503}
1504
Jens Axboe165125e2007-07-24 09:28:11 +02001505static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Jens Axboe22e2c502005-06-27 10:55:12 +02001507 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001509 WARN_ON(!cfqd->rq_in_driver);
1510 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001511 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001512 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Jens Axboeb4878f22005-10-20 16:42:29 +02001515static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jens Axboe5e705372006-07-13 12:39:25 +02001517 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001518
Jens Axboe5e705372006-07-13 12:39:25 +02001519 if (cfqq->next_rq == rq)
1520 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jens Axboeb4878f22005-10-20 16:42:29 +02001522 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001523 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001524
Aaron Carroll45333d52008-08-26 15:52:36 +02001525 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001526 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001527 rq_data_dir(rq), rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001528 if (rq->cmd_flags & REQ_PRIO) {
1529 WARN_ON(!cfqq->prio_pending);
1530 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Jens Axboe165125e2007-07-24 09:28:11 +02001534static int cfq_merge(struct request_queue *q, struct request **req,
1535 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct cfq_data *cfqd = q->elevator->elevator_data;
1538 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Jens Axboe206dc692006-03-28 13:03:44 +02001540 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001541 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001542 *req = __rq;
1543 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Jens Axboe165125e2007-07-24 09:28:11 +02001549static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001550 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Jens Axboe21183b02006-07-13 12:33:14 +02001552 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001553 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Jens Axboe5e705372006-07-13 12:39:25 +02001555 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Divyesh Shah812d4022010-04-08 21:14:23 -07001559static void cfq_bio_merged(struct request_queue *q, struct request *req,
1560 struct bio *bio)
1561{
Tejun Heo03814112012-03-05 13:15:14 -08001562 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001563 bio_data_dir(bio), cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566static void
Jens Axboe165125e2007-07-24 09:28:11 +02001567cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 struct request *next)
1569{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001570 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001571 struct cfq_data *cfqd = q->elevator->elevator_data;
1572
Jens Axboe22e2c502005-06-27 10:55:12 +02001573 /*
1574 * reposition in fifo if next is older than rq
1575 */
1576 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001577 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001578 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001579 rq_set_fifo_time(rq, rq_fifo_time(next));
1580 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001581
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001582 if (cfqq->next_rq == next)
1583 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001584 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08001585 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001586 rq_data_dir(next), rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001587
1588 cfqq = RQ_CFQQ(next);
1589 /*
1590 * all requests of this queue are merged to other queues, delete it
1591 * from the service tree. If it's the active_queue,
1592 * cfq_dispatch_requests() will choose to expire it or do idle
1593 */
1594 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1595 cfqq != cfqd->active_queue)
1596 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001597}
1598
Jens Axboe165125e2007-07-24 09:28:11 +02001599static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001600 struct bio *bio)
1601{
1602 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01001603 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001604 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001605
1606 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001607 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001608 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001609 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001610 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001611
1612 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01001613 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01001614 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001615 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01001616 cic = cfq_cic_lookup(cfqd, current->io_context);
1617 if (!cic)
1618 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001619
Vasily Tarasov91fac312007-04-25 12:29:51 +02001620 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001621 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001622}
1623
Divyesh Shah812df482010-04-08 21:15:35 -07001624static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1625{
1626 del_timer(&cfqd->idle_slice_timer);
Tejun Heo03814112012-03-05 13:15:14 -08001627 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
Divyesh Shah812df482010-04-08 21:15:35 -07001628}
1629
Jens Axboefebffd62008-01-28 13:19:43 +01001630static void __cfq_set_active_queue(struct cfq_data *cfqd,
1631 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001632{
1633 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001634 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1635 cfqd->serving_prio, cfqd->serving_type);
Tejun Heo03814112012-03-05 13:15:14 -08001636 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg));
Justin TerAvest62a37f62011-03-23 08:25:44 +01001637 cfqq->slice_start = 0;
1638 cfqq->dispatch_start = jiffies;
1639 cfqq->allocated_slice = 0;
1640 cfqq->slice_end = 0;
1641 cfqq->slice_dispatch = 0;
1642 cfqq->nr_sectors = 0;
1643
1644 cfq_clear_cfqq_wait_request(cfqq);
1645 cfq_clear_cfqq_must_dispatch(cfqq);
1646 cfq_clear_cfqq_must_alloc_slice(cfqq);
1647 cfq_clear_cfqq_fifo_expire(cfqq);
1648 cfq_mark_cfqq_slice_new(cfqq);
1649
1650 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001651 }
1652
1653 cfqd->active_queue = cfqq;
1654}
1655
1656/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001657 * current cfqq expired its slice (or was too idle), select new one
1658 */
1659static void
1660__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001661 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001662{
Jens Axboe7b679132008-05-30 12:23:07 +02001663 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1664
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001665 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07001666 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001667
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001668 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001669 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001670
1671 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01001672 * If this cfqq is shared between multiple processes, check to
1673 * make sure that those processes are still issuing I/Os within
1674 * the mean seek distance. If not, it may be time to break the
1675 * queues apart again.
1676 */
1677 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1678 cfq_mark_cfqq_split_coop(cfqq);
1679
1680 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001681 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001682 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01001683 if (timed_out) {
1684 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07001685 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001686 else
1687 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001688 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1689 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001690
Vivek Goyale5ff0822010-04-26 19:25:11 +02001691 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001692
Vivek Goyalf04a6422009-12-03 12:59:40 -05001693 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1694 cfq_del_cfqq_rr(cfqd, cfqq);
1695
Jens Axboeedd75ff2007-04-19 12:03:34 +02001696 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001697
1698 if (cfqq == cfqd->active_queue)
1699 cfqd->active_queue = NULL;
1700
1701 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01001702 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001703 cfqd->active_cic = NULL;
1704 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001705}
1706
Vivek Goyale5ff0822010-04-26 19:25:11 +02001707static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001708{
1709 struct cfq_queue *cfqq = cfqd->active_queue;
1710
1711 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02001712 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001713}
1714
Jens Axboe498d3aa22007-04-26 12:54:48 +02001715/*
1716 * Get next queue for service. Unless we have a queue preemption,
1717 * we'll simply select the first cfqq in the service tree.
1718 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001719static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001720{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001721 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001722 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05001723 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001724
Vivek Goyalf04a6422009-12-03 12:59:40 -05001725 if (!cfqd->rq_queued)
1726 return NULL;
1727
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001728 /* There is nothing to dispatch */
1729 if (!service_tree)
1730 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001731 if (RB_EMPTY_ROOT(&service_tree->rb))
1732 return NULL;
1733 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001734}
1735
Vivek Goyalf04a6422009-12-03 12:59:40 -05001736static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1737{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001738 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001739 struct cfq_queue *cfqq;
1740 int i, j;
1741 struct cfq_rb_root *st;
1742
1743 if (!cfqd->rq_queued)
1744 return NULL;
1745
Vivek Goyal25fb5162009-12-03 12:59:46 -05001746 cfqg = cfq_get_next_cfqg(cfqd);
1747 if (!cfqg)
1748 return NULL;
1749
Vivek Goyalf04a6422009-12-03 12:59:40 -05001750 for_each_cfqg_st(cfqg, i, j, st)
1751 if ((cfqq = cfq_rb_first(st)) != NULL)
1752 return cfqq;
1753 return NULL;
1754}
1755
Jens Axboe498d3aa22007-04-26 12:54:48 +02001756/*
1757 * Get and set a new active queue for service.
1758 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001759static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1760 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001761{
Jens Axboee00ef792009-11-04 08:54:55 +01001762 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001763 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001764
Jens Axboe22e2c502005-06-27 10:55:12 +02001765 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001766 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001767}
1768
Jens Axboed9e76202007-04-20 14:27:50 +02001769static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1770 struct request *rq)
1771{
Tejun Heo83096eb2009-05-07 22:24:39 +09001772 if (blk_rq_pos(rq) >= cfqd->last_position)
1773 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001774 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001775 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001776}
1777
Jeff Moyerb2c18e1e2009-10-23 17:14:49 -04001778static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01001779 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001780{
Shaohua Lie9ce3352010-03-19 08:03:04 +01001781 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001782}
1783
Jens Axboea36e71f2009-04-15 12:15:11 +02001784static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1785 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001786{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001787 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001788 struct rb_node *parent, *node;
1789 struct cfq_queue *__cfqq;
1790 sector_t sector = cfqd->last_position;
1791
1792 if (RB_EMPTY_ROOT(root))
1793 return NULL;
1794
1795 /*
1796 * First, if we find a request starting at the end of the last
1797 * request, choose it.
1798 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001799 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001800 if (__cfqq)
1801 return __cfqq;
1802
1803 /*
1804 * If the exact sector wasn't found, the parent of the NULL leaf
1805 * will contain the closest sector.
1806 */
1807 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001808 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001809 return __cfqq;
1810
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001811 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001812 node = rb_next(&__cfqq->p_node);
1813 else
1814 node = rb_prev(&__cfqq->p_node);
1815 if (!node)
1816 return NULL;
1817
1818 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001819 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001820 return __cfqq;
1821
1822 return NULL;
1823}
1824
1825/*
1826 * cfqd - obvious
1827 * cur_cfqq - passed in so that we don't decide that the current queue is
1828 * closely cooperating with itself.
1829 *
1830 * So, basically we're assuming that that cur_cfqq has dispatched at least
1831 * one request, and that cfqd->last_position reflects a position on the disk
1832 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1833 * assumption.
1834 */
1835static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001836 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001837{
1838 struct cfq_queue *cfqq;
1839
Divyesh Shah39c01b22010-03-25 15:45:57 +01001840 if (cfq_class_idle(cur_cfqq))
1841 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001842 if (!cfq_cfqq_sync(cur_cfqq))
1843 return NULL;
1844 if (CFQQ_SEEKY(cur_cfqq))
1845 return NULL;
1846
Jens Axboea36e71f2009-04-15 12:15:11 +02001847 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01001848 * Don't search priority tree if it's the only queue in the group.
1849 */
1850 if (cur_cfqq->cfqg->nr_cfqq == 1)
1851 return NULL;
1852
1853 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001854 * We should notice if some of the queues are cooperating, eg
1855 * working closely on the same area of the disk. In that case,
1856 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001857 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001858 cfqq = cfqq_close(cfqd, cur_cfqq);
1859 if (!cfqq)
1860 return NULL;
1861
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001862 /* If new queue belongs to different cfq_group, don't choose it */
1863 if (cur_cfqq->cfqg != cfqq->cfqg)
1864 return NULL;
1865
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001866 /*
1867 * It only makes sense to merge sync queues.
1868 */
1869 if (!cfq_cfqq_sync(cfqq))
1870 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001871 if (CFQQ_SEEKY(cfqq))
1872 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001873
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001874 /*
1875 * Do not merge queues of different priority classes
1876 */
1877 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1878 return NULL;
1879
Jens Axboea36e71f2009-04-15 12:15:11 +02001880 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001881}
1882
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001883/*
1884 * Determine whether we should enforce idle window for this queue.
1885 */
1886
1887static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1888{
1889 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001890 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001891
Vivek Goyalf04a6422009-12-03 12:59:40 -05001892 BUG_ON(!service_tree);
1893 BUG_ON(!service_tree->count);
1894
Vivek Goyalb6508c12010-08-23 12:23:33 +02001895 if (!cfqd->cfq_slice_idle)
1896 return false;
1897
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001898 /* We never do for idle class queues. */
1899 if (prio == IDLE_WORKLOAD)
1900 return false;
1901
1902 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01001903 if (cfq_cfqq_idle_window(cfqq) &&
1904 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001905 return true;
1906
1907 /*
1908 * Otherwise, we do only if they are the last ones
1909 * in their service tree.
1910 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02001911 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
1912 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01001913 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001914 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1915 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01001916 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001917}
1918
Jens Axboe6d048f52007-04-25 12:44:27 +02001919static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001920{
Jens Axboe17926692007-01-19 11:59:30 +11001921 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01001922 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001923 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001924
Jens Axboea68bbddba2008-09-24 13:03:33 +02001925 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001926 * SSD device without seek penalty, disable idling. But only do so
1927 * for devices that support queuing, otherwise we still have a problem
1928 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02001929 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001930 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbddba2008-09-24 13:03:33 +02001931 return;
1932
Jens Axboedd67d052006-06-21 09:36:18 +02001933 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001934 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001935
1936 /*
1937 * idle is disabled, either manually or by past process history
1938 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001939 if (!cfq_should_idle(cfqd, cfqq)) {
1940 /* no queue idling. Check for group idling */
1941 if (cfqd->cfq_group_idle)
1942 group_idle = cfqd->cfq_group_idle;
1943 else
1944 return;
1945 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001946
Jens Axboe22e2c502005-06-27 10:55:12 +02001947 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001948 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001949 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001950 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001951 return;
1952
1953 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001954 * task has exited, don't wait
1955 */
Jens Axboe206dc692006-03-28 13:03:44 +02001956 cic = cfqd->active_cic;
Tejun Heoc5869802011-12-14 00:33:41 +01001957 if (!cic || !atomic_read(&cic->icq.ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001958 return;
1959
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001960 /*
1961 * If our average think time is larger than the remaining time
1962 * slice, then don't idle. This avoids overrunning the allotted
1963 * time slice.
1964 */
Shaohua Li383cd722011-07-12 14:24:35 +02001965 if (sample_valid(cic->ttime.ttime_samples) &&
1966 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02001967 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02001968 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001969 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001970 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001971
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001972 /* There are other queues in the group, don't do group idle */
1973 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
1974 return;
1975
Jens Axboe3b181522005-06-27 10:56:24 +02001976 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001977
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001978 if (group_idle)
1979 sl = cfqd->cfq_group_idle;
1980 else
1981 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001982
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001983 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo03814112012-03-05 13:15:14 -08001984 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001985 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
1986 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Jens Axboe498d3aa22007-04-26 12:54:48 +02001989/*
1990 * Move request from internal lists to the request queue dispatch list.
1991 */
Jens Axboe165125e2007-07-24 09:28:11 +02001992static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001994 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001995 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001996
Jens Axboe7b679132008-05-30 12:23:07 +02001997 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1998
Jeff Moyer06d21882009-09-11 17:08:59 +02001999 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002000 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002001 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002002 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002003 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002004
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002005 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002006 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08002007 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
2008 blk_rq_bytes(rq), rq_data_dir(rq),
2009 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012/*
2013 * return expired entry, or NULL to just start from scratch in rbtree
2014 */
Jens Axboefebffd62008-01-28 13:19:43 +01002015static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Jens Axboe30996f42009-10-05 11:03:39 +02002017 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Jens Axboe3b181522005-06-27 10:56:24 +02002019 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002021
2022 cfq_mark_cfqq_fifo_expire(cfqq);
2023
Jens Axboe89850f72006-07-22 16:48:31 +02002024 if (list_empty(&cfqq->fifo))
2025 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Jens Axboe89850f72006-07-22 16:48:31 +02002027 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002028 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002029 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Jens Axboe30996f42009-10-05 11:03:39 +02002031 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002032 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Jens Axboe22e2c502005-06-27 10:55:12 +02002035static inline int
2036cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2037{
2038 const int base_rq = cfqd->cfq_slice_async_rq;
2039
2040 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2041
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002042 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002043}
2044
2045/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002046 * Must be called with the queue_lock held.
2047 */
2048static int cfqq_process_refs(struct cfq_queue *cfqq)
2049{
2050 int process_refs, io_refs;
2051
2052 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002053 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002054 BUG_ON(process_refs < 0);
2055 return process_refs;
2056}
2057
2058static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2059{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002060 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002061 struct cfq_queue *__cfqq;
2062
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002063 /*
2064 * If there are no process references on the new_cfqq, then it is
2065 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2066 * chain may have dropped their last reference (not just their
2067 * last process reference).
2068 */
2069 if (!cfqq_process_refs(new_cfqq))
2070 return;
2071
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002072 /* Avoid a circular list and skip interim queue merges */
2073 while ((__cfqq = new_cfqq->new_cfqq)) {
2074 if (__cfqq == cfqq)
2075 return;
2076 new_cfqq = __cfqq;
2077 }
2078
2079 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002080 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002081 /*
2082 * If the process for the cfqq has gone away, there is no
2083 * sense in merging the queues.
2084 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002085 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002086 return;
2087
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002088 /*
2089 * Merge in the direction of the lesser amount of work.
2090 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002091 if (new_process_refs >= process_refs) {
2092 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002093 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002094 } else {
2095 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002096 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002097 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002098}
2099
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002100static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002101 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002102{
2103 struct cfq_queue *queue;
2104 int i;
2105 bool key_valid = false;
2106 unsigned long lowest_key = 0;
2107 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2108
Vivek Goyal65b32a52009-12-16 17:52:59 -05002109 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2110 /* select the one with lowest rb_key */
2111 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002112 if (queue &&
2113 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2114 lowest_key = queue->rb_key;
2115 cur_best = i;
2116 key_valid = true;
2117 }
2118 }
2119
2120 return cur_best;
2121}
2122
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002123static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002124{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002125 unsigned slice;
2126 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002127 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002128 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002129 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002130
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002131 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002132 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002133 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002134 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002135 cfqd->serving_prio = BE_WORKLOAD;
2136 else {
2137 cfqd->serving_prio = IDLE_WORKLOAD;
2138 cfqd->workload_expires = jiffies + 1;
2139 return;
2140 }
2141
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002142 if (original_prio != cfqd->serving_prio)
2143 goto new_workload;
2144
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002145 /*
2146 * For RT and BE, we have to choose also the type
2147 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2148 * expiration time
2149 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002150 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002151 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002152
2153 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002154 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002155 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002156 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002157 return;
2158
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002159new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002160 /* otherwise select new workload type */
2161 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002162 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2163 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002164 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002165
2166 /*
2167 * the workload slice is computed as a fraction of target latency
2168 * proportional to the number of queues in that workload, over
2169 * all the queues in the same priority class
2170 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002171 group_slice = cfq_group_slice(cfqd, cfqg);
2172
2173 slice = group_slice * count /
2174 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2175 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002176
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002177 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2178 unsigned int tmp;
2179
2180 /*
2181 * Async queues are currently system wide. Just taking
2182 * proportion of queues with-in same group will lead to higher
2183 * async ratio system wide as generally root group is going
2184 * to have higher weight. A more accurate thing would be to
2185 * calculate system wide asnc/sync ratio.
2186 */
2187 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2188 tmp = tmp/cfqd->busy_queues;
2189 slice = min_t(unsigned, slice, tmp);
2190
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002191 /* async workload slice is scaled down according to
2192 * the sync/async slice ratio. */
2193 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002194 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002195 /* sync workload slice is at least 2 * cfq_slice_idle */
2196 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2197
2198 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002199 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002200 cfqd->workload_expires = jiffies + slice;
2201}
2202
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002203static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2204{
2205 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002206 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002207
2208 if (RB_EMPTY_ROOT(&st->rb))
2209 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002210 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002211 update_min_vdisktime(st);
2212 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002213}
2214
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002215static void cfq_choose_cfqg(struct cfq_data *cfqd)
2216{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002217 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2218
2219 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002220
2221 /* Restore the workload type data */
2222 if (cfqg->saved_workload_slice) {
2223 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2224 cfqd->serving_type = cfqg->saved_workload;
2225 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002226 } else
2227 cfqd->workload_expires = jiffies - 1;
2228
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002229 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002230}
2231
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002232/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002233 * Select a queue for service. If we have a current active queue,
2234 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002235 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002236static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002237{
Jens Axboea36e71f2009-04-15 12:15:11 +02002238 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002239
2240 cfqq = cfqd->active_queue;
2241 if (!cfqq)
2242 goto new_queue;
2243
Vivek Goyalf04a6422009-12-03 12:59:40 -05002244 if (!cfqd->rq_queued)
2245 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002246
2247 /*
2248 * We were waiting for group to get backlogged. Expire the queue
2249 */
2250 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2251 goto expire;
2252
Jens Axboe22e2c502005-06-27 10:55:12 +02002253 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002254 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002255 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002256 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2257 /*
2258 * If slice had not expired at the completion of last request
2259 * we might not have turned on wait_busy flag. Don't expire
2260 * the queue yet. Allow the group to get backlogged.
2261 *
2262 * The very fact that we have used the slice, that means we
2263 * have been idling all along on this queue and it should be
2264 * ok to wait for this request to complete.
2265 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002266 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2267 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2268 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002269 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002270 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002271 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002272 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002273
2274 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002275 * The active queue has requests and isn't expired, allow it to
2276 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002277 */
Jens Axboedd67d052006-06-21 09:36:18 +02002278 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002279 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002280
2281 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002282 * If another queue has a request waiting within our mean seek
2283 * distance, let it run. The expire code will check for close
2284 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002285 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002286 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002287 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002288 if (new_cfqq) {
2289 if (!cfqq->new_cfqq)
2290 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002291 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002292 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002293
2294 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002295 * No requests pending. If the active queue still has requests in
2296 * flight or is idling for a new request, allow either of these
2297 * conditions to happen (or time out) before selecting a new queue.
2298 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002299 if (timer_pending(&cfqd->idle_slice_timer)) {
2300 cfqq = NULL;
2301 goto keep_queue;
2302 }
2303
Shaohua Li8e1ac662010-11-08 15:01:04 +01002304 /*
2305 * This is a deep seek queue, but the device is much faster than
2306 * the queue can deliver, don't idle
2307 **/
2308 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2309 (cfq_cfqq_slice_new(cfqq) ||
2310 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2311 cfq_clear_cfqq_deep(cfqq);
2312 cfq_clear_cfqq_idle_window(cfqq);
2313 }
2314
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002315 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2316 cfqq = NULL;
2317 goto keep_queue;
2318 }
2319
2320 /*
2321 * If group idle is enabled and there are requests dispatched from
2322 * this group, wait for requests to complete.
2323 */
2324check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002325 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2326 cfqq->cfqg->dispatched &&
2327 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002328 cfqq = NULL;
2329 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002330 }
2331
Jens Axboe3b181522005-06-27 10:56:24 +02002332expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002333 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002334new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002335 /*
2336 * Current queue expired. Check if we have to switch to a new
2337 * service tree
2338 */
2339 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002340 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002341
Jens Axboea36e71f2009-04-15 12:15:11 +02002342 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002343keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002344 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002345}
2346
Jens Axboefebffd62008-01-28 13:19:43 +01002347static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002348{
2349 int dispatched = 0;
2350
2351 while (cfqq->next_rq) {
2352 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2353 dispatched++;
2354 }
2355
2356 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002357
2358 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002359 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002360 return dispatched;
2361}
2362
Jens Axboe498d3aa22007-04-26 12:54:48 +02002363/*
2364 * Drain our current requests. Used for barriers and when switching
2365 * io schedulers on-the-fly.
2366 */
Jens Axboed9e76202007-04-20 14:27:50 +02002367static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002368{
Jens Axboe08717142008-01-28 11:38:15 +01002369 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002370 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002371
Divyesh Shah3440c492010-04-09 09:29:57 +02002372 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002373 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002374 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2375 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002376 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002377 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002378
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002379 BUG_ON(cfqd->busy_queues);
2380
Jeff Moyer69237152009-06-12 15:29:30 +02002381 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002382 return dispatched;
2383}
2384
Shaohua Liabc3c742010-03-01 09:20:54 +01002385static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2386 struct cfq_queue *cfqq)
2387{
2388 /* the queue hasn't finished any request, can't estimate */
2389 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002390 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002391 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2392 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002393 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002394
Shaohua Lic1e44752010-11-08 15:01:02 +01002395 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002396}
2397
Jens Axboe0b182d62009-10-06 20:49:37 +02002398static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002399{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002400 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Jens Axboe2f5cb732009-04-07 08:51:19 +02002402 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002403 * Drain async requests before we start sync IO
2404 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002405 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002406 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002407
2408 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002409 * If this is an async queue and we have sync IO in flight, let it wait
2410 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002411 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002412 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002413
Shaohua Liabc3c742010-03-01 09:20:54 +01002414 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002415 if (cfq_class_idle(cfqq))
2416 max_dispatch = 1;
2417
2418 /*
2419 * Does this cfqq already have too much IO in flight?
2420 */
2421 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002422 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002423 /*
2424 * idle queue must always only have a single IO in flight
2425 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002426 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002427 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002428
Jens Axboe2f5cb732009-04-07 08:51:19 +02002429 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002430 * If there is only one sync queue
2431 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002432 * queue no dispatch limit. The reason is a sync queue can
2433 * preempt async queue, limiting the sync queue doesn't make
2434 * sense. This is useful for aiostress test.
2435 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002436 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2437 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002438
2439 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002440 * We have other queues, don't allow more IO from this one
2441 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002442 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2443 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002444 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002445
Jens Axboe2f5cb732009-04-07 08:51:19 +02002446 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002447 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002448 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002449 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002450 max_dispatch = -1;
2451 else
2452 /*
2453 * Normally we start throttling cfqq when cfq_quantum/2
2454 * requests have been dispatched. But we can drive
2455 * deeper queue depths at the beginning of slice
2456 * subjected to upper limit of cfq_quantum.
2457 * */
2458 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002459 }
2460
2461 /*
2462 * Async queues must wait a bit before being allowed dispatch.
2463 * We also ramp up the dispatch depth gradually for async IO,
2464 * based on the last sync IO we serviced
2465 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002466 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002467 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002468 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002469
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002470 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002471 if (!depth && !cfqq->dispatched)
2472 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002473 if (depth < max_dispatch)
2474 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Jens Axboe0b182d62009-10-06 20:49:37 +02002477 /*
2478 * If we're below the current max, allow a dispatch
2479 */
2480 return cfqq->dispatched < max_dispatch;
2481}
2482
2483/*
2484 * Dispatch a request from cfqq, moving them to the request queue
2485 * dispatch list.
2486 */
2487static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2488{
2489 struct request *rq;
2490
2491 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2492
2493 if (!cfq_may_dispatch(cfqd, cfqq))
2494 return false;
2495
2496 /*
2497 * follow expired path, else get first next available
2498 */
2499 rq = cfq_check_fifo(cfqq);
2500 if (!rq)
2501 rq = cfqq->next_rq;
2502
2503 /*
2504 * insert request into driver dispatch list
2505 */
2506 cfq_dispatch_insert(cfqd->queue, rq);
2507
2508 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002509 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002510
Tejun Heoc5869802011-12-14 00:33:41 +01002511 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002512 cfqd->active_cic = cic;
2513 }
2514
2515 return true;
2516}
2517
2518/*
2519 * Find the cfqq that we need to service and move a request from that to the
2520 * dispatch list
2521 */
2522static int cfq_dispatch_requests(struct request_queue *q, int force)
2523{
2524 struct cfq_data *cfqd = q->elevator->elevator_data;
2525 struct cfq_queue *cfqq;
2526
2527 if (!cfqd->busy_queues)
2528 return 0;
2529
2530 if (unlikely(force))
2531 return cfq_forced_dispatch(cfqd);
2532
2533 cfqq = cfq_select_queue(cfqd);
2534 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002535 return 0;
2536
Jens Axboe2f5cb732009-04-07 08:51:19 +02002537 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002538 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002539 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002540 if (!cfq_dispatch_request(cfqd, cfqq))
2541 return 0;
2542
Jens Axboe2f5cb732009-04-07 08:51:19 +02002543 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002544 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002545
2546 /*
2547 * expire an async queue immediately if it has used up its slice. idle
2548 * queue always expire after 1 dispatch round.
2549 */
2550 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2551 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2552 cfq_class_idle(cfqq))) {
2553 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002554 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002555 }
2556
Shan Weib217a902009-09-01 10:06:42 +02002557 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002558 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561/*
Jens Axboe5e705372006-07-13 12:39:25 +02002562 * task holds one reference to the queue, dropped when task exits. each rq
2563 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002565 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 * queue lock must be held here.
2567 */
2568static void cfq_put_queue(struct cfq_queue *cfqq)
2569{
Jens Axboe22e2c502005-06-27 10:55:12 +02002570 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002571 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002572
Shaohua Li30d7b942011-01-07 08:46:59 +01002573 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Shaohua Li30d7b942011-01-07 08:46:59 +01002575 cfqq->ref--;
2576 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return;
2578
Jens Axboe7b679132008-05-30 12:23:07 +02002579 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002581 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002582 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002584 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002585 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002586 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002587 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002588
Vivek Goyalf04a6422009-12-03 12:59:40 -05002589 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 kmem_cache_free(cfq_pool, cfqq);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08002591 blkg_put(cfqg_to_blkg(cfqg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Shaohua Lid02a2c02010-05-25 10:16:53 +02002594static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002595{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002596 struct cfq_queue *__cfqq, *next;
2597
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002598 /*
2599 * If this queue was scheduled to merge with another queue, be
2600 * sure to drop the reference taken on that queue (and others in
2601 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2602 */
2603 __cfqq = cfqq->new_cfqq;
2604 while (__cfqq) {
2605 if (__cfqq == cfqq) {
2606 WARN(1, "cfqq->new_cfqq loop detected\n");
2607 break;
2608 }
2609 next = __cfqq->new_cfqq;
2610 cfq_put_queue(__cfqq);
2611 __cfqq = next;
2612 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02002613}
2614
2615static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2616{
2617 if (unlikely(cfqq == cfqd->active_queue)) {
2618 __cfq_slice_expired(cfqd, cfqq, 0);
2619 cfq_schedule_dispatch(cfqd);
2620 }
2621
2622 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002623
Jens Axboe89850f72006-07-22 16:48:31 +02002624 cfq_put_queue(cfqq);
2625}
2626
Tejun Heo9b84cac2011-12-14 00:33:42 +01002627static void cfq_init_icq(struct io_cq *icq)
2628{
2629 struct cfq_io_cq *cic = icq_to_cic(icq);
2630
2631 cic->ttime.last_end_request = jiffies;
2632}
2633
Tejun Heoc5869802011-12-14 00:33:41 +01002634static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02002635{
Tejun Heoc5869802011-12-14 00:33:41 +01002636 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01002637 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002638
Jens Axboeff6657c2009-04-08 10:58:57 +02002639 if (cic->cfqq[BLK_RW_ASYNC]) {
2640 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2641 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002642 }
2643
Jens Axboeff6657c2009-04-08 10:58:57 +02002644 if (cic->cfqq[BLK_RW_SYNC]) {
2645 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2646 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002647 }
Jens Axboe89850f72006-07-22 16:48:31 +02002648}
2649
Jens Axboefd0928d2008-01-24 08:52:45 +01002650static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002651{
2652 struct task_struct *tsk = current;
2653 int ioprio_class;
2654
Jens Axboe3b181522005-06-27 10:56:24 +02002655 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002656 return;
2657
Jens Axboefd0928d2008-01-24 08:52:45 +01002658 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002659 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002660 default:
2661 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2662 case IOPRIO_CLASS_NONE:
2663 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002664 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002665 */
2666 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002667 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002668 break;
2669 case IOPRIO_CLASS_RT:
2670 cfqq->ioprio = task_ioprio(ioc);
2671 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2672 break;
2673 case IOPRIO_CLASS_BE:
2674 cfqq->ioprio = task_ioprio(ioc);
2675 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2676 break;
2677 case IOPRIO_CLASS_IDLE:
2678 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2679 cfqq->ioprio = 7;
2680 cfq_clear_cfqq_idle_window(cfqq);
2681 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002682 }
2683
2684 /*
2685 * keep track of original prio settings in case we have to temporarily
2686 * elevate the priority of this queue
2687 */
2688 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02002689 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002690}
2691
Tejun Heoc5869802011-12-14 00:33:41 +01002692static void changed_ioprio(struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002693{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002694 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05002695 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02002696
Jens Axboecaaa5f92006-06-16 11:23:00 +02002697 if (unlikely(!cfqd))
2698 return;
2699
Jens Axboeff6657c2009-04-08 10:58:57 +02002700 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002701 if (cfqq) {
2702 struct cfq_queue *new_cfqq;
Tejun Heoc5869802011-12-14 00:33:41 +01002703 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->icq.ioc,
Jens Axboeff6657c2009-04-08 10:58:57 +02002704 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002705 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002706 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002707 cfq_put_queue(cfqq);
2708 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002709 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002710
Jens Axboeff6657c2009-04-08 10:58:57 +02002711 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002712 if (cfqq)
2713 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002714}
2715
Jens Axboed5036d72009-06-26 10:44:34 +02002716static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002717 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002718{
2719 RB_CLEAR_NODE(&cfqq->rb_node);
2720 RB_CLEAR_NODE(&cfqq->p_node);
2721 INIT_LIST_HEAD(&cfqq->fifo);
2722
Shaohua Li30d7b942011-01-07 08:46:59 +01002723 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02002724 cfqq->cfqd = cfqd;
2725
2726 cfq_mark_cfqq_prio_changed(cfqq);
2727
2728 if (is_sync) {
2729 if (!cfq_class_idle(cfqq))
2730 cfq_mark_cfqq_idle_window(cfqq);
2731 cfq_mark_cfqq_sync(cfqq);
2732 }
2733 cfqq->pid = pid;
2734}
2735
Vivek Goyal246103332009-12-03 12:59:51 -05002736#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoc5869802011-12-14 00:33:41 +01002737static void changed_cgroup(struct cfq_io_cq *cic)
Vivek Goyal246103332009-12-03 12:59:51 -05002738{
2739 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002740 struct cfq_data *cfqd = cic_to_cfqd(cic);
Vivek Goyal246103332009-12-03 12:59:51 -05002741 struct request_queue *q;
2742
2743 if (unlikely(!cfqd))
2744 return;
2745
2746 q = cfqd->queue;
2747
Vivek Goyal246103332009-12-03 12:59:51 -05002748 if (sync_cfqq) {
2749 /*
2750 * Drop reference to sync queue. A new sync queue will be
2751 * assigned in new group upon arrival of a fresh request.
2752 */
2753 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2754 cic_set_cfqq(cic, NULL, 1);
2755 cfq_put_queue(sync_cfqq);
2756 }
Vivek Goyal246103332009-12-03 12:59:51 -05002757}
Vivek Goyal246103332009-12-03 12:59:51 -05002758#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002761cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002762 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002764 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 struct cfq_queue *cfqq, *new_cfqq = NULL;
Tejun Heoc5869802011-12-14 00:33:41 +01002766 struct cfq_io_cq *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002767 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08002770 rcu_read_lock();
2771
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002772 blkcg = task_blkio_cgroup(current);
2773
Tejun Heocd1604f2012-03-05 13:15:06 -08002774 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
2775
Jens Axboe4ac845a2008-01-24 08:44:49 +01002776 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002777 /* cic always exists here */
2778 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Jens Axboe6118b702009-06-30 09:34:12 +02002780 /*
2781 * Always try a new alloc if we fell back to the OOM cfqq
2782 * originally, since it should just be a temporary situation.
2783 */
2784 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2785 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (new_cfqq) {
2787 cfqq = new_cfqq;
2788 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002789 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08002790 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002792 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002793 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002794 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002796 if (new_cfqq)
2797 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002798 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002799 cfqq = kmem_cache_alloc_node(cfq_pool,
2800 gfp_mask | __GFP_ZERO,
2801 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Jens Axboe6118b702009-06-30 09:34:12 +02002804 if (cfqq) {
2805 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2806 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002807 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002808 cfq_log_cfqq(cfqd, cfqq, "alloced");
2809 } else
2810 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
2812
2813 if (new_cfqq)
2814 kmem_cache_free(cfq_pool, new_cfqq);
2815
Tejun Heo2a7f1242012-03-05 13:15:01 -08002816 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return cfqq;
2818}
2819
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002820static struct cfq_queue **
2821cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2822{
Jens Axboefe094d92008-01-31 13:08:54 +01002823 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002824 case IOPRIO_CLASS_RT:
2825 return &cfqd->async_cfqq[0][ioprio];
2826 case IOPRIO_CLASS_BE:
2827 return &cfqd->async_cfqq[1][ioprio];
2828 case IOPRIO_CLASS_IDLE:
2829 return &cfqd->async_idle_cfqq;
2830 default:
2831 BUG();
2832 }
2833}
2834
Jens Axboe15c31be2007-07-10 13:43:25 +02002835static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002836cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002837 gfp_t gfp_mask)
2838{
Jens Axboefd0928d2008-01-24 08:52:45 +01002839 const int ioprio = task_ioprio(ioc);
2840 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002841 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002842 struct cfq_queue *cfqq = NULL;
2843
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002844 if (!is_sync) {
2845 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2846 cfqq = *async_cfqq;
2847 }
2848
Jens Axboe6118b702009-06-30 09:34:12 +02002849 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002850 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002851
2852 /*
2853 * pin the queue now that it's allocated, scheduler exit will prune it
2854 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002855 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01002856 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002857 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002858 }
2859
Shaohua Li30d7b942011-01-07 08:46:59 +01002860 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02002861 return cfqq;
2862}
2863
Jens Axboe22e2c502005-06-27 10:55:12 +02002864static void
Shaohua Li383cd722011-07-12 14:24:35 +02002865__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002866{
Shaohua Li383cd722011-07-12 14:24:35 +02002867 unsigned long elapsed = jiffies - ttime->last_end_request;
2868 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002869
Shaohua Li383cd722011-07-12 14:24:35 +02002870 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
2871 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
2872 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
2873}
2874
2875static void
2876cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002877 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02002878{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002879 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02002880 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002881 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
2882 cfqd->cfq_slice_idle);
2883 }
Shaohua Li7700fc42011-07-12 14:24:56 +02002884#ifdef CONFIG_CFQ_GROUP_IOSCHED
2885 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
2886#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02002887}
2888
Jens Axboe206dc692006-03-28 13:03:44 +02002889static void
Jeff Moyerb2c18e1e2009-10-23 17:14:49 -04002890cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002891 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002892{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002893 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002894 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002895 if (cfqq->last_request_pos) {
2896 if (cfqq->last_request_pos < blk_rq_pos(rq))
2897 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2898 else
2899 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2900 }
Jens Axboe206dc692006-03-28 13:03:44 +02002901
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002902 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002903 if (blk_queue_nonrot(cfqd->queue))
2904 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
2905 else
2906 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02002907}
Jens Axboe22e2c502005-06-27 10:55:12 +02002908
2909/*
2910 * Disable idle window if the process thinks too long or seeks so much that
2911 * it doesn't matter
2912 */
2913static void
2914cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002915 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002916{
Jens Axboe7b679132008-05-30 12:23:07 +02002917 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02002918
Jens Axboe08717142008-01-28 11:38:15 +01002919 /*
2920 * Don't idle for async or idle io prio class
2921 */
2922 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02002923 return;
2924
Jens Axboec265a7f2008-06-26 13:49:33 +02002925 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002926
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002927 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2928 cfq_mark_cfqq_deep(cfqq);
2929
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02002930 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
2931 enable_idle = 0;
Tejun Heoc5869802011-12-14 00:33:41 +01002932 else if (!atomic_read(&cic->icq.ioc->nr_tasks) ||
2933 !cfqd->cfq_slice_idle ||
2934 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002935 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02002936 else if (sample_valid(cic->ttime.ttime_samples)) {
2937 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002938 enable_idle = 0;
2939 else
2940 enable_idle = 1;
2941 }
2942
Jens Axboe7b679132008-05-30 12:23:07 +02002943 if (old_idle != enable_idle) {
2944 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2945 if (enable_idle)
2946 cfq_mark_cfqq_idle_window(cfqq);
2947 else
2948 cfq_clear_cfqq_idle_window(cfqq);
2949 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002950}
2951
Jens Axboe22e2c502005-06-27 10:55:12 +02002952/*
2953 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2954 * no or if we aren't sure, a 1 will cause a preempt.
2955 */
Jens Axboea6151c32009-10-07 20:02:57 +02002956static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002957cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002958 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002959{
Jens Axboe6d048f52007-04-25 12:44:27 +02002960 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002961
Jens Axboe6d048f52007-04-25 12:44:27 +02002962 cfqq = cfqd->active_queue;
2963 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002964 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002965
Jens Axboe6d048f52007-04-25 12:44:27 +02002966 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002967 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002968
2969 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002970 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002971
Jens Axboe22e2c502005-06-27 10:55:12 +02002972 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08002973 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
2974 */
2975 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
2976 return false;
2977
2978 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002979 * if the new request is sync, but the currently running queue is
2980 * not, let the sync request have priority.
2981 */
Jens Axboe5e705372006-07-13 12:39:25 +02002982 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002983 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002984
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002985 if (new_cfqq->cfqg != cfqq->cfqg)
2986 return false;
2987
2988 if (cfq_slice_used(cfqq))
2989 return true;
2990
2991 /* Allow preemption only if we are idling on sync-noidle tree */
2992 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2993 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
2994 new_cfqq->service_tree->count == 2 &&
2995 RB_EMPTY_ROOT(&cfqq->sort_list))
2996 return true;
2997
Jens Axboe374f84a2006-07-23 01:42:19 +02002998 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002999 * So both queues are sync. Let the new request get disk time if
3000 * it's a metadata request and the current queue is doing regular IO.
3001 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003002 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003003 return true;
3004
3005 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003006 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3007 */
3008 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003009 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003010
Shaohua Lid2d59e12010-11-08 15:01:03 +01003011 /* An idle queue should not be idle now for some reason */
3012 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3013 return true;
3014
Jens Axboe1e3335d2007-02-14 19:59:49 +01003015 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003016 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003017
3018 /*
3019 * if this request is as-good as one we would expect from the
3020 * current cfqq, let it preempt
3021 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003022 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003023 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003024
Jens Axboea6151c32009-10-07 20:02:57 +02003025 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003026}
3027
3028/*
3029 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3030 * let it have half of its nominal slice.
3031 */
3032static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3033{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003034 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3035
Jens Axboe7b679132008-05-30 12:23:07 +02003036 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003037 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003038
Jens Axboebf572252006-07-19 20:29:12 +02003039 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003040 * workload type is changed, don't save slice, otherwise preempt
3041 * doesn't happen
3042 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003043 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003044 cfqq->cfqg->saved_workload_slice = 0;
3045
3046 /*
Jens Axboebf572252006-07-19 20:29:12 +02003047 * Put the new queue at the front of the of the current list,
3048 * so we know that it will be selected next.
3049 */
3050 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003051
3052 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003053
Justin TerAvest62a37f62011-03-23 08:25:44 +01003054 cfqq->slice_end = 0;
3055 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003056}
3057
3058/*
Jens Axboe5e705372006-07-13 12:39:25 +02003059 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003060 * something we should do about it
3061 */
3062static void
Jens Axboe5e705372006-07-13 12:39:25 +02003063cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3064 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003065{
Tejun Heoc5869802011-12-14 00:33:41 +01003066 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003067
Aaron Carroll45333d52008-08-26 15:52:36 +02003068 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003069 if (rq->cmd_flags & REQ_PRIO)
3070 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003071
Shaohua Li383cd722011-07-12 14:24:35 +02003072 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e1e2009-10-23 17:14:49 -04003073 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003074 cfq_update_idle_window(cfqd, cfqq, cic);
3075
Jeff Moyerb2c18e1e2009-10-23 17:14:49 -04003076 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003077
3078 if (cfqq == cfqd->active_queue) {
3079 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003080 * Remember that we saw a request from this process, but
3081 * don't start queuing just yet. Otherwise we risk seeing lots
3082 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003083 * and merging. If the request is already larger than a single
3084 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003085 * merging is already done. Ditto for a busy system that
3086 * has other work pending, don't risk delaying until the
3087 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003088 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003089 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003090 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3091 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003092 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003093 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003094 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003095 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003096 cfq_blkiocg_update_idle_time_stats(
Tejun Heo03814112012-03-05 13:15:14 -08003097 cfqg_to_blkg(cfqq->cfqg));
Vivek Goyalbf7919372009-12-03 12:59:37 -05003098 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003099 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003100 }
Jens Axboe5e705372006-07-13 12:39:25 +02003101 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003102 /*
3103 * not the active queue - expire current slice if it is
3104 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003105 * has some old slice time left and is of higher priority or
3106 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003107 */
3108 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003109 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003110 }
3111}
3112
Jens Axboe165125e2007-07-24 09:28:11 +02003113static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003114{
Jens Axboeb4878f22005-10-20 16:42:29 +02003115 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003116 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003117
Jens Axboe7b679132008-05-30 12:23:07 +02003118 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoc5869802011-12-14 00:33:41 +01003119 cfq_init_prio_data(cfqq, RQ_CIC(rq)->icq.ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Jens Axboe30996f42009-10-05 11:03:39 +02003121 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003122 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003123 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003124 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
3125 cfqg_to_blkg(cfqd->serving_group),
3126 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003127 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
Aaron Carroll45333d52008-08-26 15:52:36 +02003130/*
3131 * Update hw_tag based on peak queue depth over 50 samples under
3132 * sufficient load.
3133 */
3134static void cfq_update_hw_tag(struct cfq_data *cfqd)
3135{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003136 struct cfq_queue *cfqq = cfqd->active_queue;
3137
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003138 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3139 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003140
3141 if (cfqd->hw_tag == 1)
3142 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003143
3144 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003145 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003146 return;
3147
Shaohua Li1a1238a2009-10-27 08:46:23 +01003148 /*
3149 * If active queue hasn't enough requests and can idle, cfq might not
3150 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3151 * case
3152 */
3153 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3154 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003155 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003156 return;
3157
Aaron Carroll45333d52008-08-26 15:52:36 +02003158 if (cfqd->hw_tag_samples++ < 50)
3159 return;
3160
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003161 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003162 cfqd->hw_tag = 1;
3163 else
3164 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003165}
3166
Vivek Goyal7667aa02009-12-08 17:52:58 -05003167static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3168{
Tejun Heoc5869802011-12-14 00:33:41 +01003169 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003170
Justin TerAvest02a8f012011-02-09 14:20:03 +01003171 /* If the queue already has requests, don't wait */
3172 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3173 return false;
3174
Vivek Goyal7667aa02009-12-08 17:52:58 -05003175 /* If there are other queues in the group, don't wait */
3176 if (cfqq->cfqg->nr_cfqq > 1)
3177 return false;
3178
Shaohua Li7700fc42011-07-12 14:24:56 +02003179 /* the only queue in the group, but think time is big */
3180 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3181 return false;
3182
Vivek Goyal7667aa02009-12-08 17:52:58 -05003183 if (cfq_slice_used(cfqq))
3184 return true;
3185
3186 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003187 if (cic && sample_valid(cic->ttime.ttime_samples)
3188 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003189 return true;
3190
3191 /*
3192 * If think times is less than a jiffy than ttime_mean=0 and above
3193 * will not be true. It might happen that slice has not expired yet
3194 * but will expire soon (4-5 ns) during select_queue(). To cover the
3195 * case where think time is less than a jiffy, mark the queue wait
3196 * busy if only 1 jiffy is left in the slice.
3197 */
3198 if (cfqq->slice_end - jiffies == 1)
3199 return true;
3200
3201 return false;
3202}
3203
Jens Axboe165125e2007-07-24 09:28:11 +02003204static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Jens Axboe5e705372006-07-13 12:39:25 +02003206 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003207 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003208 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003209 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Jens Axboeb4878f22005-10-20 16:42:29 +02003211 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003212 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3213 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Aaron Carroll45333d52008-08-26 15:52:36 +02003215 cfq_update_hw_tag(cfqd);
3216
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003217 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003218 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003219 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003220 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003221 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003222 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Vivek Goyale98ef892010-06-18 10:39:47 -04003223 rq_start_time_ns(rq), rq_io_start_time_ns(rq),
3224 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003226 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003227
Vivek Goyal365722b2009-10-03 15:21:27 +02003228 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003229 struct cfq_rb_root *service_tree;
3230
Shaohua Li383cd722011-07-12 14:24:35 +02003231 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003232
3233 if (cfq_cfqq_on_rr(cfqq))
3234 service_tree = cfqq->service_tree;
3235 else
3236 service_tree = service_tree_for(cfqq->cfqg,
3237 cfqq_prio(cfqq), cfqq_type(cfqq));
3238 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003239 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3240 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003241 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003242
Shaohua Li7700fc42011-07-12 14:24:56 +02003243#ifdef CONFIG_CFQ_GROUP_IOSCHED
3244 cfqq->cfqg->ttime.last_end_request = now;
3245#endif
3246
Jens Axboecaaa5f92006-06-16 11:23:00 +02003247 /*
3248 * If this is the active queue, check if it needs to be expired,
3249 * or if we want to idle in case it has no pending requests.
3250 */
3251 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003252 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3253
Jens Axboe44f7c162007-01-19 11:51:58 +11003254 if (cfq_cfqq_slice_new(cfqq)) {
3255 cfq_set_prio_slice(cfqd, cfqq);
3256 cfq_clear_cfqq_slice_new(cfqq);
3257 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003258
3259 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003260 * Should we wait for next request to come in before we expire
3261 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003262 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003263 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003264 unsigned long extend_sl = cfqd->cfq_slice_idle;
3265 if (!cfqd->cfq_slice_idle)
3266 extend_sl = cfqd->cfq_group_idle;
3267 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003268 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003269 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003270 }
3271
Jens Axboea36e71f2009-04-15 12:15:11 +02003272 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003273 * Idling is not enabled on:
3274 * - expired queues
3275 * - idle-priority queues
3276 * - async queues
3277 * - queues with still some requests queued
3278 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003279 */
Jens Axboe08717142008-01-28 11:38:15 +01003280 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003281 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003282 else if (sync && cfqq_empty &&
3283 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003284 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003285 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003286 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003287
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003288 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003289 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Jens Axboe89850f72006-07-22 16:48:31 +02003292static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003293{
Jens Axboe1b379d82009-08-11 08:26:11 +02003294 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003295 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003296 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003297 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003298
3299 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003300}
3301
Jens Axboe165125e2007-07-24 09:28:11 +02003302static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003303{
3304 struct cfq_data *cfqd = q->elevator->elevator_data;
3305 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003306 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003307 struct cfq_queue *cfqq;
3308
3309 /*
3310 * don't force setup of a queue from here, as a call to may_queue
3311 * does not necessarily imply that a request actually will be queued.
3312 * so just lookup a possibly existing queue, or return 'may queue'
3313 * if that fails
3314 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003315 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003316 if (!cic)
3317 return ELV_MQUEUE_MAY;
3318
Jens Axboeb0b78f82009-04-08 10:56:08 +02003319 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003320 if (cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003321 cfq_init_prio_data(cfqq, cic->icq.ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003322
Jens Axboe89850f72006-07-22 16:48:31 +02003323 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003324 }
3325
3326 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
3330 * queue lock held here
3331 */
Jens Axboebb37b942006-12-01 10:42:33 +01003332static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Jens Axboe5e705372006-07-13 12:39:25 +02003334 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Jens Axboe5e705372006-07-13 12:39:25 +02003336 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003337 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Jens Axboe22e2c502005-06-27 10:55:12 +02003339 BUG_ON(!cfqq->allocated[rw]);
3340 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003342 /* Put down rq reference on cfqg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003343 blkg_put(cfqg_to_blkg(RQ_CFQG(rq)));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003344 rq->elv.priv[0] = NULL;
3345 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 cfq_put_queue(cfqq);
3348 }
3349}
3350
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003351static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003352cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003353 struct cfq_queue *cfqq)
3354{
3355 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3356 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003357 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003358 cfq_put_queue(cfqq);
3359 return cic_to_cfqq(cic, 1);
3360}
3361
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003362/*
3363 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3364 * was the last process referring to said cfqq.
3365 */
3366static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003367split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003368{
3369 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003370 cfqq->pid = current->pid;
3371 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003372 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003373 return cfqq;
3374 }
3375
3376 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003377
3378 cfq_put_cooperator(cfqq);
3379
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003380 cfq_put_queue(cfqq);
3381 return NULL;
3382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003384 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003386static int
Jens Axboe165125e2007-07-24 09:28:11 +02003387cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
3389 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003390 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003392 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003393 struct cfq_queue *cfqq;
Tejun Heod705ae62012-02-15 09:45:49 +01003394 unsigned int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
3396 might_sleep_if(gfp_mask & __GFP_WAIT);
3397
Tejun Heo216284c2011-12-14 00:33:38 +01003398 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003399
3400 /* handle changed notifications */
Tejun Heod705ae62012-02-15 09:45:49 +01003401 changed = icq_get_changed(&cic->icq);
3402 if (unlikely(changed & ICQ_IOPRIO_CHANGED))
3403 changed_ioprio(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003404#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heod705ae62012-02-15 09:45:49 +01003405 if (unlikely(changed & ICQ_CGROUP_CHANGED))
3406 changed_cgroup(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003409new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003410 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003411 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003412 cfqq = cfq_get_queue(cfqd, is_sync, cic->icq.ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003413 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003414 } else {
3415 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003416 * If the queue was seeky for too long, break it apart.
3417 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003418 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003419 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3420 cfqq = split_cfqq(cic, cfqq);
3421 if (!cfqq)
3422 goto new_queue;
3423 }
3424
3425 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003426 * Check to see if this queue is scheduled to merge with
3427 * another, closely cooperating queue. The merging of
3428 * queues happens here as it must be done in process context.
3429 * The reference on new_cfqq was taken in merge_cfqqs.
3430 */
3431 if (cfqq->new_cfqq)
3432 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003436
Jens Axboe6fae9c22011-03-01 15:04:39 -05003437 cfqq->ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003438 blkg_get(cfqg_to_blkg(cfqq->cfqg));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003439 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003440 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01003441 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
David Howells65f27f32006-11-22 14:55:48 +00003445static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003446{
David Howells65f27f32006-11-22 14:55:48 +00003447 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003448 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003449 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003450
Jens Axboe40bb54d2009-04-15 12:11:10 +02003451 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003452 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003453 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003454}
3455
3456/*
3457 * Timer running if the active_queue is currently idling inside its time slice
3458 */
3459static void cfq_idle_slice_timer(unsigned long data)
3460{
3461 struct cfq_data *cfqd = (struct cfq_data *) data;
3462 struct cfq_queue *cfqq;
3463 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003464 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003465
Jens Axboe7b679132008-05-30 12:23:07 +02003466 cfq_log(cfqd, "idle timer fired");
3467
Jens Axboe22e2c502005-06-27 10:55:12 +02003468 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3469
Jens Axboefe094d92008-01-31 13:08:54 +01003470 cfqq = cfqd->active_queue;
3471 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003472 timed_out = 0;
3473
Jens Axboe22e2c502005-06-27 10:55:12 +02003474 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003475 * We saw a request before the queue expired, let it through
3476 */
3477 if (cfq_cfqq_must_dispatch(cfqq))
3478 goto out_kick;
3479
3480 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003481 * expired
3482 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003483 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003484 goto expire;
3485
3486 /*
3487 * only expire and reinvoke request handler, if there are
3488 * other queues with pending requests
3489 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003490 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003491 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003492
3493 /*
3494 * not expired and it has a request pending, let it dispatch
3495 */
Jens Axboe75e50982009-04-07 08:56:14 +02003496 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003497 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003498
3499 /*
3500 * Queue depth flag is reset only when the idle didn't succeed
3501 */
3502 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003503 }
3504expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003505 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003506out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003507 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003508out_cont:
3509 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3510}
3511
Jens Axboe3b181522005-06-27 10:56:24 +02003512static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3513{
3514 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003515 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003516}
Jens Axboe22e2c502005-06-27 10:55:12 +02003517
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003518static void cfq_put_async_queues(struct cfq_data *cfqd)
3519{
3520 int i;
3521
3522 for (i = 0; i < IOPRIO_BE_NR; i++) {
3523 if (cfqd->async_cfqq[0][i])
3524 cfq_put_queue(cfqd->async_cfqq[0][i]);
3525 if (cfqd->async_cfqq[1][i])
3526 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003527 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003528
3529 if (cfqd->async_idle_cfqq)
3530 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003531}
3532
Jens Axboeb374d182008-10-31 10:05:07 +01003533static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Jens Axboe22e2c502005-06-27 10:55:12 +02003535 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003536 struct request_queue *q = cfqd->queue;
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003537 bool wait = false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003538
Jens Axboe3b181522005-06-27 10:56:24 +02003539 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003540
Al Virod9ff4182006-03-18 13:51:22 -05003541 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003542
Al Virod9ff4182006-03-18 13:51:22 -05003543 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003544 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003545
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003546 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003547 cfq_release_cfq_groups(cfqd);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003548
3549 /*
3550 * If there are groups which we could not unlink from blkcg list,
3551 * wait for a rcu period for them to be freed.
3552 */
3553 if (cfqd->nr_blkcg_linked_grps)
3554 wait = true;
Jens Axboe15c31be2007-07-10 13:43:25 +02003555
Al Virod9ff4182006-03-18 13:51:22 -05003556 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003557
3558 cfq_shutdown_timer_wq(cfqd);
3559
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003560 /*
3561 * Wait for cfqg->blkg->key accessors to exit their grace periods.
3562 * Do this wait only if there are other unlinked groups out
3563 * there. This can happen if cgroup deletion path claimed the
3564 * responsibility of cleaning up a group before queue cleanup code
3565 * get to the group.
3566 *
3567 * Do not call synchronize_rcu() unconditionally as there are drivers
3568 * which create/delete request queue hundreds of times during scan/boot
3569 * and synchronize_rcu() can take significant time and slow down boot.
3570 */
3571 if (wait)
3572 synchronize_rcu();
Vivek Goyal2abae552011-05-23 10:02:19 +02003573
Tejun Heof51b8022012-03-05 13:15:05 -08003574#ifndef CONFIG_CFQ_GROUP_IOSCHED
3575 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003576#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003577 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Tejun Heob2fab5a2012-03-05 13:14:57 -08003580static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
3582 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003583 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003584 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Christoph Lameter94f60302007-07-17 04:03:29 -07003586 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003587 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003588 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003589
Tejun Heof51b8022012-03-05 13:15:05 -08003590 cfqd->queue = q;
3591 q->elevator->elevator_data = cfqd;
3592
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003593 /* Init root service tree */
3594 cfqd->grp_service_tree = CFQ_RB_ROOT;
3595
Tejun Heof51b8022012-03-05 13:15:05 -08003596 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05003597#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08003598 rcu_read_lock();
3599 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003600
Tejun Heocd1604f2012-03-05 13:15:06 -08003601 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_PROP,
3602 true);
3603 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08003604 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08003605
3606 spin_unlock_irq(q->queue_lock);
3607 rcu_read_unlock();
3608#else
3609 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
3610 GFP_KERNEL, cfqd->queue->node);
3611 if (cfqd->root_group)
3612 cfq_init_cfqg_base(cfqd->root_group);
3613#endif
3614 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003615 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08003616 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003617 }
3618
Tejun Heof51b8022012-03-05 13:15:05 -08003619 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003620
Jens Axboe26a2ac02009-04-23 12:13:27 +02003621 /*
3622 * Not strictly needed (since RB_ROOT just clears the node and we
3623 * zeroed cfqd on alloc), but better be safe in case someone decides
3624 * to add magic to the rb code
3625 */
3626 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3627 cfqd->prio_trees[i] = RB_ROOT;
3628
Jens Axboe6118b702009-06-30 09:34:12 +02003629 /*
3630 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3631 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08003632 * will not attempt to free it. oom_cfqq is linked to root_group
3633 * but shouldn't hold a reference as it'll never be unlinked. Lose
3634 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02003635 */
3636 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01003637 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003638
3639 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08003640 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003641 blkg_put(cfqg_to_blkg(cfqd->root_group));
3642 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Jens Axboe22e2c502005-06-27 10:55:12 +02003644 init_timer(&cfqd->idle_slice_timer);
3645 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3646 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3647
Jens Axboe23e018a2009-10-05 08:52:35 +02003648 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003651 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3652 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 cfqd->cfq_back_max = cfq_back_max;
3654 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003655 cfqd->cfq_slice[0] = cfq_slice_async;
3656 cfqd->cfq_slice[1] = cfq_slice_sync;
3657 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3658 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003659 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003660 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003661 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01003662 /*
3663 * we optimistically start assuming sync ops weren't delayed in last
3664 * second, in order to have larger depth for async operations.
3665 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003666 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08003667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670/*
3671 * sysfs parts below -->
3672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673static ssize_t
3674cfq_var_show(unsigned int var, char *page)
3675{
3676 return sprintf(page, "%d\n", var);
3677}
3678
3679static ssize_t
3680cfq_var_store(unsigned int *var, const char *page, size_t count)
3681{
3682 char *p = (char *) page;
3683
3684 *var = simple_strtoul(p, &p, 10);
3685 return count;
3686}
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003689static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003691 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 unsigned int __data = __VAR; \
3693 if (__CONV) \
3694 __data = jiffies_to_msecs(__data); \
3695 return cfq_var_show(__data, (page)); \
3696}
3697SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003698SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3699SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003700SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3701SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003702SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003703SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003704SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3705SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3706SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003707SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708#undef SHOW_FUNCTION
3709
3710#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003711static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003713 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 unsigned int __data; \
3715 int ret = cfq_var_store(&__data, (page), count); \
3716 if (__data < (MIN)) \
3717 __data = (MIN); \
3718 else if (__data > (MAX)) \
3719 __data = (MAX); \
3720 if (__CONV) \
3721 *(__PTR) = msecs_to_jiffies(__data); \
3722 else \
3723 *(__PTR) = __data; \
3724 return ret; \
3725}
3726STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003727STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3728 UINT_MAX, 1);
3729STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3730 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003731STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003732STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3733 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003734STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003735STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003736STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3737STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003738STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3739 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003740STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741#undef STORE_FUNCTION
3742
Al Viroe572ec72006-03-18 22:27:18 -05003743#define CFQ_ATTR(name) \
3744 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003745
Al Viroe572ec72006-03-18 22:27:18 -05003746static struct elv_fs_entry cfq_attrs[] = {
3747 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003748 CFQ_ATTR(fifo_expire_sync),
3749 CFQ_ATTR(fifo_expire_async),
3750 CFQ_ATTR(back_seek_max),
3751 CFQ_ATTR(back_seek_penalty),
3752 CFQ_ATTR(slice_sync),
3753 CFQ_ATTR(slice_async),
3754 CFQ_ATTR(slice_async_rq),
3755 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003756 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003757 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003758 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759};
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761static struct elevator_type iosched_cfq = {
3762 .ops = {
3763 .elevator_merge_fn = cfq_merge,
3764 .elevator_merged_fn = cfq_merged_request,
3765 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003766 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07003767 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02003768 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003770 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003773 .elevator_former_req_fn = elv_rb_former_request,
3774 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01003775 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01003776 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 .elevator_set_req_fn = cfq_set_request,
3778 .elevator_put_req_fn = cfq_put_request,
3779 .elevator_may_queue_fn = cfq_may_queue,
3780 .elevator_init_fn = cfq_init_queue,
3781 .elevator_exit_fn = cfq_exit_queue,
3782 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01003783 .icq_size = sizeof(struct cfq_io_cq),
3784 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05003785 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01003786 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 .elevator_owner = THIS_MODULE,
3788};
3789
Vivek Goyal3e252062009-12-04 10:36:42 -05003790#ifdef CONFIG_CFQ_GROUP_IOSCHED
3791static struct blkio_policy_type blkio_policy_cfq = {
3792 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08003793 .blkio_init_group_fn = cfq_init_blkio_group,
Tejun Heocd1604f2012-03-05 13:15:06 -08003794 .blkio_link_group_fn = cfq_link_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05003795 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
Tejun Heo72e06c22012-03-05 13:15:00 -08003796 .blkio_clear_queue_fn = cfq_clear_queue,
Vivek Goyal3e252062009-12-04 10:36:42 -05003797 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3798 },
Vivek Goyal062a6442010-09-15 17:06:33 -04003799 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08003800 .pdata_size = sizeof(struct cfq_group),
Vivek Goyal3e252062009-12-04 10:36:42 -05003801};
Vivek Goyal3e252062009-12-04 10:36:42 -05003802#endif
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804static int __init cfq_init(void)
3805{
Tejun Heo3d3c2372011-12-14 00:33:42 +01003806 int ret;
3807
Jens Axboe22e2c502005-06-27 10:55:12 +02003808 /*
3809 * could be 0 on HZ < 1000 setups
3810 */
3811 if (!cfq_slice_async)
3812 cfq_slice_async = 1;
3813 if (!cfq_slice_idle)
3814 cfq_slice_idle = 1;
3815
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003816#ifdef CONFIG_CFQ_GROUP_IOSCHED
3817 if (!cfq_group_idle)
3818 cfq_group_idle = 1;
3819#else
3820 cfq_group_idle = 0;
3821#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01003822 cfq_pool = KMEM_CACHE(cfq_queue, 0);
3823 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return -ENOMEM;
3825
Tejun Heo3d3c2372011-12-14 00:33:42 +01003826 ret = elv_register(&iosched_cfq);
3827 if (ret) {
3828 kmem_cache_destroy(cfq_pool);
3829 return ret;
3830 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01003831
Tejun Heob95ada52012-03-05 13:14:55 -08003832#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003833 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003834#endif
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{
Tejun Heob95ada52012-03-05 13:14:55 -08003840#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003841 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003842#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01003844 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
3847module_init(cfq_init);
3848module_exit(cfq_exit);
3849
3850MODULE_AUTHOR("Jens Axboe");
3851MODULE_LICENSE("GPL");
3852MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");