blob: c7449db52a863c020925a5f91b0376814bae46e2 [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 Moyerb2c18e12009-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;
Shaohua Li329a6782011-01-07 08:48:28 +0100213 int ref;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500214#endif
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200215 /* number of requests that are on the dispatch list or inside driver */
216 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200217 struct cfq_ttime ttime;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500218};
219
Tejun Heoc5869802011-12-14 00:33:41 +0100220struct cfq_io_cq {
221 struct io_cq icq; /* must be the first member */
222 struct cfq_queue *cfqq[2];
223 struct cfq_ttime ttime;
224};
225
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500226/*
227 * Per block device queue structure
228 */
229struct cfq_data {
230 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500231 /* Root service tree for cfq_groups */
232 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800233 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500234
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100235 /*
236 * The priority currently being served
237 */
238 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100239 enum wl_type_t serving_type;
240 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500241 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200242
243 /*
244 * Each priority tree is sorted by next_request position. These
245 * trees are used when determining if two or more queues are
246 * interleaving requests (see cfq_close_cooperator).
247 */
248 struct rb_root prio_trees[CFQ_PRIO_LISTS];
249
Jens Axboe22e2c502005-06-27 10:55:12 +0200250 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100251 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200252
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100253 int rq_in_driver;
254 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200255
256 /*
257 * queue-depth detection
258 */
259 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200260 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100261 /*
262 * hw_tag can be
263 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
264 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
265 * 0 => no NCQ
266 */
267 int hw_tag_est_depth;
268 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200269
270 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200271 * idle window management
272 */
273 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200274 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200275
276 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100277 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200278
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200279 /*
280 * async queue for each priority case
281 */
282 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
283 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200284
Jens Axboe6d048f52007-04-25 12:44:27 +0200285 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * tunables, see top of file
289 */
290 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200291 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 unsigned int cfq_back_penalty;
293 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200294 unsigned int cfq_slice[2];
295 unsigned int cfq_slice_async_rq;
296 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200297 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200298 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500299
Jens Axboe6118b702009-06-30 09:34:12 +0200300 /*
301 * Fallback dummy cfqq for extreme OOM conditions
302 */
303 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200304
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100305 unsigned long last_delayed_sync;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500306
307 /* List of cfq groups being managed on this device*/
308 struct hlist_head cfqg_list;
Vivek Goyal56edf7d2011-05-19 15:38:22 -0400309
310 /* Number of groups which are on blkcg->blkg_list */
311 unsigned int nr_blkcg_linked_grps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
Tejun Heo03814112012-03-05 13:15:14 -0800314static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
315{
316 return blkg_to_pdata(blkg, &blkio_policy_cfq);
317}
318
319static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
320{
321 return pdata_to_blkg(cfqg, &blkio_policy_cfq);
322}
323
Vivek Goyal25fb5162009-12-03 12:59:46 -0500324static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
325
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500326static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
327 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500328 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100329{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500330 if (!cfqg)
331 return NULL;
332
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100333 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500334 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100335
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500336 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100337}
338
Jens Axboe3b181522005-06-27 10:56:24 +0200339enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100340 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
341 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200342 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100343 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100344 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
345 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
346 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100347 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200348 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400349 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100350 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100351 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500352 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200353};
354
355#define CFQ_CFQQ_FNS(name) \
356static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
357{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100358 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200359} \
360static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
361{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100362 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200363} \
364static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
365{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100366 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200367}
368
369CFQ_CFQQ_FNS(on_rr);
370CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200371CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200372CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200373CFQ_CFQQ_FNS(fifo_expire);
374CFQ_CFQQ_FNS(idle_window);
375CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100376CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200377CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200378CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100379CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100380CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500381CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200382#undef CFQ_CFQQ_FNS
383
Vivek Goyalafc24d42010-04-26 19:27:56 +0200384#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal2868ef72009-12-03 12:59:48 -0500385#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
386 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
387 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
Tejun Heo03814112012-03-05 13:15:14 -0800388 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500389
390#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
391 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800392 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
Vivek Goyal2868ef72009-12-03 12:59:48 -0500393
394#else
Jens Axboe7b679132008-05-30 12:23:07 +0200395#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
396 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200397#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500398#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200399#define cfq_log(cfqd, fmt, args...) \
400 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
401
Vivek Goyal615f0252009-12-03 12:59:39 -0500402/* Traverses through cfq group service trees */
403#define for_each_cfqg_st(cfqg, i, j, st) \
404 for (i = 0; i <= IDLE_WORKLOAD; i++) \
405 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
406 : &cfqg->service_tree_idle; \
407 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
408 (i == IDLE_WORKLOAD && j == 0); \
409 j++, st = i < IDLE_WORKLOAD ? \
410 &cfqg->service_trees[i][j]: NULL) \
411
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200412static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
413 struct cfq_ttime *ttime, bool group_idle)
414{
415 unsigned long slice;
416 if (!sample_valid(ttime->ttime_samples))
417 return false;
418 if (group_idle)
419 slice = cfqd->cfq_group_idle;
420 else
421 slice = cfqd->cfq_slice_idle;
422 return ttime->ttime_mean > slice;
423}
Vivek Goyal615f0252009-12-03 12:59:39 -0500424
Vivek Goyal02b35082010-08-23 12:23:53 +0200425static inline bool iops_mode(struct cfq_data *cfqd)
426{
427 /*
428 * If we are not idling on queues and it is a NCQ drive, parallel
429 * execution of requests is on and measuring time is not possible
430 * in most of the cases until and unless we drive shallower queue
431 * depths and that becomes a performance bottleneck. In such cases
432 * switch to start providing fairness in terms of number of IOs.
433 */
434 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
435 return true;
436 else
437 return false;
438}
439
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100440static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
441{
442 if (cfq_class_idle(cfqq))
443 return IDLE_WORKLOAD;
444 if (cfq_class_rt(cfqq))
445 return RT_WORKLOAD;
446 return BE_WORKLOAD;
447}
448
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100449
450static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
451{
452 if (!cfq_cfqq_sync(cfqq))
453 return ASYNC_WORKLOAD;
454 if (!cfq_cfqq_idle_window(cfqq))
455 return SYNC_NOIDLE_WORKLOAD;
456 return SYNC_WORKLOAD;
457}
458
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500459static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
460 struct cfq_data *cfqd,
461 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100462{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500463 if (wl == IDLE_WORKLOAD)
464 return cfqg->service_tree_idle.count;
465
466 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
467 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
468 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100469}
470
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500471static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
472 struct cfq_group *cfqg)
473{
474 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
475 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
476}
477
Jens Axboe165125e2007-07-24 09:28:11 +0200478static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200479static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100480 struct io_context *, gfp_t);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200481
Tejun Heoc5869802011-12-14 00:33:41 +0100482static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
483{
484 /* cic->icq is the first member, %NULL will convert to %NULL */
485 return container_of(icq, struct cfq_io_cq, icq);
486}
487
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100488static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
489 struct io_context *ioc)
490{
491 if (ioc)
492 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
493 return NULL;
494}
495
Tejun Heoc5869802011-12-14 00:33:41 +0100496static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200497{
Jens Axboea6151c32009-10-07 20:02:57 +0200498 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200499}
500
Tejun Heoc5869802011-12-14 00:33:41 +0100501static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
502 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200503{
Jens Axboea6151c32009-10-07 20:02:57 +0200504 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200505}
506
Tejun Heoc5869802011-12-14 00:33:41 +0100507static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400508{
Tejun Heoc5869802011-12-14 00:33:41 +0100509 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400510}
511
Vasily Tarasov91fac312007-04-25 12:29:51 +0200512/*
513 * We regard a request as SYNC, if it's either a read or has the SYNC bit
514 * set (in which case it could also be direct WRITE).
515 */
Jens Axboea6151c32009-10-07 20:02:57 +0200516static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200517{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200518 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200519}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700522 * scheduler run of queue, if there are requests pending and no one in the
523 * driver that will restart queueing
524 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200525static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700526{
Jens Axboe7b679132008-05-30 12:23:07 +0200527 if (cfqd->busy_queues) {
528 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200529 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200530 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100534 * Scale schedule slice based on io priority. Use the sync time slice only
535 * if a queue is marked sync and has sync io queued. A sync queue with async
536 * io only, should not get full sync slice length.
537 */
Jens Axboea6151c32009-10-07 20:02:57 +0200538static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200539 unsigned short prio)
540{
541 const int base_slice = cfqd->cfq_slice[sync];
542
543 WARN_ON(prio >= IOPRIO_BE_NR);
544
545 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
546}
547
Jens Axboe44f7c162007-01-19 11:51:58 +1100548static inline int
549cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
550{
Jens Axboed9e76202007-04-20 14:27:50 +0200551 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100552}
553
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500554static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
555{
556 u64 d = delta << CFQ_SERVICE_SHIFT;
557
558 d = d * BLKIO_WEIGHT_DEFAULT;
559 do_div(d, cfqg->weight);
560 return d;
561}
562
563static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
564{
565 s64 delta = (s64)(vdisktime - min_vdisktime);
566 if (delta > 0)
567 min_vdisktime = vdisktime;
568
569 return min_vdisktime;
570}
571
572static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
573{
574 s64 delta = (s64)(vdisktime - min_vdisktime);
575 if (delta < 0)
576 min_vdisktime = vdisktime;
577
578 return min_vdisktime;
579}
580
581static void update_min_vdisktime(struct cfq_rb_root *st)
582{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500583 struct cfq_group *cfqg;
584
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500585 if (st->left) {
586 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100587 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
588 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500589 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500590}
591
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100592/*
593 * get averaged number of queues of RT/BE priority.
594 * average is updated, with a formula that gives more weight to higher numbers,
595 * to quickly follows sudden increases and decrease slowly
596 */
597
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500598static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
599 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100600{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100601 unsigned min_q, max_q;
602 unsigned mult = cfq_hist_divisor - 1;
603 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500604 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100605
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500606 min_q = min(cfqg->busy_queues_avg[rt], busy);
607 max_q = max(cfqg->busy_queues_avg[rt], busy);
608 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100609 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500610 return cfqg->busy_queues_avg[rt];
611}
612
613static inline unsigned
614cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
615{
616 struct cfq_rb_root *st = &cfqd->grp_service_tree;
617
618 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100619}
620
Shaohua Lic553f8e2011-01-14 08:41:03 +0100621static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700622cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100623{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100624 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
625 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500626 /*
627 * interested queues (we consider only the ones with the same
628 * priority class in the cfq group)
629 */
630 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
631 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100632 unsigned sync_slice = cfqd->cfq_slice[1];
633 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500634 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
635
636 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100637 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
638 /* scale low_slice according to IO priority
639 * and sync vs async */
640 unsigned low_slice =
641 min(slice, base_low_slice * slice / sync_slice);
642 /* the adapted slice value is scaled to fit all iqs
643 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500644 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100645 low_slice);
646 }
647 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100648 return slice;
649}
650
651static inline void
652cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
653{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700654 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100655
Vivek Goyaldae739e2009-12-03 12:59:45 -0500656 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100657 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500658 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200659 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100660}
661
662/*
663 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
664 * isn't valid until the first request from the dispatch is activated
665 * and the slice time set.
666 */
Jens Axboea6151c32009-10-07 20:02:57 +0200667static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100668{
669 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100670 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100671 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100672 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100673
Shaohua Lic1e44752010-11-08 15:01:02 +0100674 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100675}
676
677/*
Jens Axboe5e705372006-07-13 12:39:25 +0200678 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200680 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Jens Axboe5e705372006-07-13 12:39:25 +0200682static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100683cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100685 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200687#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
688#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
689 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jens Axboe5e705372006-07-13 12:39:25 +0200691 if (rq1 == NULL || rq1 == rq2)
692 return rq2;
693 if (rq2 == NULL)
694 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200695
Namhyung Kim229836b2011-05-24 10:23:21 +0200696 if (rq_is_sync(rq1) != rq_is_sync(rq2))
697 return rq_is_sync(rq1) ? rq1 : rq2;
698
Christoph Hellwig65299a32011-08-23 14:50:29 +0200699 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
700 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +0200701
Tejun Heo83096eb2009-05-07 22:24:39 +0900702 s1 = blk_rq_pos(rq1);
703 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * by definition, 1KiB is 2 sectors
707 */
708 back_max = cfqd->cfq_back_max * 2;
709
710 /*
711 * Strict one way elevator _except_ in the case where we allow
712 * short backward seeks which are biased as twice the cost of a
713 * similar forward seek.
714 */
715 if (s1 >= last)
716 d1 = s1 - last;
717 else if (s1 + back_max >= last)
718 d1 = (last - s1) * cfqd->cfq_back_penalty;
719 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200720 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (s2 >= last)
723 d2 = s2 - last;
724 else if (s2 + back_max >= last)
725 d2 = (last - s2) * cfqd->cfq_back_penalty;
726 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200727 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Andreas Mohre8a99052006-03-28 08:59:49 +0200731 /*
732 * By doing switch() on the bit mask "wrap" we avoid having to
733 * check two variables for all permutations: --> faster!
734 */
735 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200736 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200737 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200738 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200739 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200740 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200741 else {
742 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200743 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200744 else
Jens Axboe5e705372006-07-13 12:39:25 +0200745 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200746 }
747
748 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200749 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200750 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200751 return rq2;
752 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200753 default:
754 /*
755 * Since both rqs are wrapped,
756 * start with the one that's further behind head
757 * (--> only *one* back seek required),
758 * since back seek takes more time than forward.
759 */
760 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200761 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else
Jens Axboe5e705372006-07-13 12:39:25 +0200763 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765}
766
Jens Axboe498d3aa22007-04-26 12:54:48 +0200767/*
768 * The below is leftmost cache rbtree addon
769 */
Jens Axboe08717142008-01-28 11:38:15 +0100770static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200771{
Vivek Goyal615f0252009-12-03 12:59:39 -0500772 /* Service tree is empty */
773 if (!root->count)
774 return NULL;
775
Jens Axboecc09e292007-04-26 12:53:50 +0200776 if (!root->left)
777 root->left = rb_first(&root->rb);
778
Jens Axboe08717142008-01-28 11:38:15 +0100779 if (root->left)
780 return rb_entry(root->left, struct cfq_queue, rb_node);
781
782 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200783}
784
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500785static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
786{
787 if (!root->left)
788 root->left = rb_first(&root->rb);
789
790 if (root->left)
791 return rb_entry_cfqg(root->left);
792
793 return NULL;
794}
795
Jens Axboea36e71f2009-04-15 12:15:11 +0200796static void rb_erase_init(struct rb_node *n, struct rb_root *root)
797{
798 rb_erase(n, root);
799 RB_CLEAR_NODE(n);
800}
801
Jens Axboecc09e292007-04-26 12:53:50 +0200802static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
803{
804 if (root->left == n)
805 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200806 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100807 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * would be nice to take fifo expire time into account as well
812 */
Jens Axboe5e705372006-07-13 12:39:25 +0200813static struct request *
814cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
815 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jens Axboe21183b02006-07-13 12:33:14 +0200817 struct rb_node *rbnext = rb_next(&last->rb_node);
818 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200819 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Jens Axboe21183b02006-07-13 12:33:14 +0200821 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200824 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200827 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200828 else {
829 rbnext = rb_first(&cfqq->sort_list);
830 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200831 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100834 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Jens Axboed9e76202007-04-20 14:27:50 +0200837static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
838 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Jens Axboed9e76202007-04-20 14:27:50 +0200840 /*
841 * just an approximation, should be ok.
842 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500843 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100844 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500847static inline s64
848cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
849{
850 return cfqg->vdisktime - st->min_vdisktime;
851}
852
853static void
854__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
855{
856 struct rb_node **node = &st->rb.rb_node;
857 struct rb_node *parent = NULL;
858 struct cfq_group *__cfqg;
859 s64 key = cfqg_key(st, cfqg);
860 int left = 1;
861
862 while (*node != NULL) {
863 parent = *node;
864 __cfqg = rb_entry_cfqg(parent);
865
866 if (key < cfqg_key(st, __cfqg))
867 node = &parent->rb_left;
868 else {
869 node = &parent->rb_right;
870 left = 0;
871 }
872 }
873
874 if (left)
875 st->left = &cfqg->rb_node;
876
877 rb_link_node(&cfqg->rb_node, parent, node);
878 rb_insert_color(&cfqg->rb_node, &st->rb);
879}
880
881static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100882cfq_update_group_weight(struct cfq_group *cfqg)
883{
884 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
885 if (cfqg->needs_update) {
886 cfqg->weight = cfqg->new_weight;
887 cfqg->needs_update = false;
888 }
889}
890
891static void
892cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
893{
894 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
895
896 cfq_update_group_weight(cfqg);
897 __cfq_group_service_tree_add(st, cfqg);
898 st->total_weight += cfqg->weight;
899}
900
901static void
902cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500903{
904 struct cfq_rb_root *st = &cfqd->grp_service_tree;
905 struct cfq_group *__cfqg;
906 struct rb_node *n;
907
908 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +0100909 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500910 return;
911
912 /*
913 * Currently put the group at the end. Later implement something
914 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300915 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500916 */
917 n = rb_last(&st->rb);
918 if (n) {
919 __cfqg = rb_entry_cfqg(n);
920 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
921 } else
922 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +0100923 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500924}
925
926static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100927cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
928{
929 st->total_weight -= cfqg->weight;
930 if (!RB_EMPTY_NODE(&cfqg->rb_node))
931 cfq_rb_erase(&cfqg->rb_node, st);
932}
933
934static void
935cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500936{
937 struct cfq_rb_root *st = &cfqd->grp_service_tree;
938
939 BUG_ON(cfqg->nr_cfqq < 1);
940 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500941
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500942 /* If there are other cfq queues under this group, don't delete it */
943 if (cfqg->nr_cfqq)
944 return;
945
Vivek Goyal2868ef72009-12-03 12:59:48 -0500946 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +0100947 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500948 cfqg->saved_workload_slice = 0;
Tejun Heo03814112012-03-05 13:15:14 -0800949 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg), 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500950}
951
Justin TerAvest167400d2011-03-12 16:54:00 +0100952static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
953 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500954{
Vivek Goyalf75edf22009-12-03 12:59:53 -0500955 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500956
957 /*
958 * Queue got expired before even a single request completed or
959 * got expired immediately after first request completion.
960 */
961 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
962 /*
963 * Also charge the seek time incurred to the group, otherwise
964 * if there are mutiple queues in the group, each can dispatch
965 * a single request on seeky media and cause lots of seek time
966 * and group will never know it.
967 */
968 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
969 1);
970 } else {
971 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +0100972 if (slice_used > cfqq->allocated_slice) {
973 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500974 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +0100975 }
976 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
977 *unaccounted_time += cfqq->slice_start -
978 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500979 }
980
Vivek Goyaldae739e2009-12-03 12:59:45 -0500981 return slice_used;
982}
983
984static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +0200985 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500986{
987 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +0100988 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500989 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
990 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500991
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500992 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +0100993 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500994
Vivek Goyal02b35082010-08-23 12:23:53 +0200995 if (iops_mode(cfqd))
996 charge = cfqq->slice_dispatch;
997 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
998 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500999
1000 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001001 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001002 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001003 /* If a new weight was requested, update now, off tree */
1004 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001005
1006 /* This group is being expired. Save the context */
1007 if (time_after(cfqd->workload_expires, jiffies)) {
1008 cfqg->saved_workload_slice = cfqd->workload_expires
1009 - jiffies;
1010 cfqg->saved_workload = cfqd->serving_type;
1011 cfqg->saved_serving_prio = cfqd->serving_prio;
1012 } else
1013 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001014
1015 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1016 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001017 cfq_log_cfqq(cfqq->cfqd, cfqq,
1018 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1019 used_sl, cfqq->slice_dispatch, charge,
1020 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo03814112012-03-05 13:15:14 -08001021 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), used_sl,
Justin TerAvest167400d2011-03-12 16:54:00 +01001022 unaccounted_sl);
Tejun Heo03814112012-03-05 13:15:14 -08001023 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg));
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001024}
1025
Tejun Heof51b8022012-03-05 13:15:05 -08001026/**
1027 * cfq_init_cfqg_base - initialize base part of a cfq_group
1028 * @cfqg: cfq_group to initialize
1029 *
1030 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1031 * is enabled or not.
1032 */
1033static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1034{
1035 struct cfq_rb_root *st;
1036 int i, j;
1037
1038 for_each_cfqg_st(cfqg, i, j, st)
1039 *st = CFQ_RB_ROOT;
1040 RB_CLEAR_NODE(&cfqg->rb_node);
1041
1042 cfqg->ttime.last_end_request = jiffies;
1043}
1044
Vivek Goyal25fb5162009-12-03 12:59:46 -05001045#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoca32aef2012-03-05 13:15:03 -08001046static void cfq_update_blkio_group_weight(struct request_queue *q,
1047 struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001048 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001049{
Tejun Heo03814112012-03-05 13:15:14 -08001050 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1051
Justin TerAvest8184f932011-03-17 16:12:36 +01001052 cfqg->new_weight = weight;
1053 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001054}
1055
Tejun Heocd1604f2012-03-05 13:15:06 -08001056static void cfq_link_blkio_group(struct request_queue *q,
1057 struct blkio_group *blkg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001058{
Tejun Heocd1604f2012-03-05 13:15:06 -08001059 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heo03814112012-03-05 13:15:14 -08001060 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001061
1062 cfqd->nr_blkcg_linked_grps++;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001063
1064 /* Add group on cfqd list */
1065 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
1066}
1067
Tejun Heo03814112012-03-05 13:15:14 -08001068static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001069{
Tejun Heo03814112012-03-05 13:15:14 -08001070 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001071
Tejun Heof51b8022012-03-05 13:15:05 -08001072 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001073 cfqg->weight = blkg->blkcg->weight;
Shaohua Li7700fc42011-07-12 14:24:56 +02001074
Vivek Goyalb1c35762009-12-03 12:59:47 -05001075 /*
1076 * Take the initial reference that will be released on destroy
1077 * This can be thought of a joint reference by cgroup and
1078 * elevator which will be dropped by either elevator exit
1079 * or cgroup deletion path depending on who is exiting first.
1080 */
Shaohua Li329a6782011-01-07 08:48:28 +01001081 cfqg->ref = 1;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001082}
1083
1084/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001085 * Search for the cfq group current task belongs to. request_queue lock must
1086 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001087 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001088static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1089 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001090{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001091 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001092 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001093
Tejun Heocd1604f2012-03-05 13:15:06 -08001094 /* avoid lookup for the common case where there's no blkio cgroup */
1095 if (blkcg == &blkio_root_cgroup) {
1096 cfqg = cfqd->root_group;
1097 } else {
1098 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001099
Tejun Heocd1604f2012-03-05 13:15:06 -08001100 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_PROP, false);
1101 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001102 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001103 }
1104
Vivek Goyal25fb5162009-12-03 12:59:46 -05001105 return cfqg;
1106}
1107
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001108static inline struct cfq_group *cfq_ref_get_cfqg(struct cfq_group *cfqg)
1109{
Shaohua Li329a6782011-01-07 08:48:28 +01001110 cfqg->ref++;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001111 return cfqg;
1112}
1113
Vivek Goyal25fb5162009-12-03 12:59:46 -05001114static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1115{
1116 /* Currently, all async queues are mapped to root group */
1117 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001118 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001119
1120 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001121 /* cfqq reference on cfqg */
Shaohua Li329a6782011-01-07 08:48:28 +01001122 cfqq->cfqg->ref++;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001123}
Vivek Goyalb1c35762009-12-03 12:59:47 -05001124
1125static void cfq_put_cfqg(struct cfq_group *cfqg)
1126{
Tejun Heo03814112012-03-05 13:15:14 -08001127 struct blkio_group *blkg = cfqg_to_blkg(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001128 struct cfq_rb_root *st;
1129 int i, j;
1130
Shaohua Li329a6782011-01-07 08:48:28 +01001131 BUG_ON(cfqg->ref <= 0);
1132 cfqg->ref--;
1133 if (cfqg->ref)
Vivek Goyalb1c35762009-12-03 12:59:47 -05001134 return;
Tejun Heo7ee9c562012-03-05 13:15:11 -08001135
1136 /* release the extra blkcg reference this blkg has been holding */
Tejun Heo03814112012-03-05 13:15:14 -08001137 css_put(&blkg->blkcg->css);
Tejun Heo7ee9c562012-03-05 13:15:11 -08001138
Vivek Goyalb1c35762009-12-03 12:59:47 -05001139 for_each_cfqg_st(cfqg, i, j, st)
Gui Jianfengb54ce602010-11-30 20:52:46 +01001140 BUG_ON(!RB_EMPTY_ROOT(&st->rb));
Tejun Heo03814112012-03-05 13:15:14 -08001141 free_percpu(blkg->stats_cpu);
1142 kfree(blkg->pd);
1143 kfree(blkg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001144}
1145
1146static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1147{
1148 /* Something wrong if we are trying to remove same group twice */
1149 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1150
1151 hlist_del_init(&cfqg->cfqd_node);
1152
Vivek Goyala5395b82011-08-02 09:24:09 +02001153 BUG_ON(cfqd->nr_blkcg_linked_grps <= 0);
1154 cfqd->nr_blkcg_linked_grps--;
1155
Vivek Goyalb1c35762009-12-03 12:59:47 -05001156 /*
1157 * Put the reference taken at the time of creation so that when all
1158 * queues are gone, group can be destroyed.
1159 */
1160 cfq_put_cfqg(cfqg);
1161}
1162
Tejun Heo72e06c22012-03-05 13:15:00 -08001163static bool cfq_release_cfq_groups(struct cfq_data *cfqd)
Vivek Goyalb1c35762009-12-03 12:59:47 -05001164{
1165 struct hlist_node *pos, *n;
1166 struct cfq_group *cfqg;
Tejun Heo72e06c22012-03-05 13:15:00 -08001167 bool empty = true;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001168
1169 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1170 /*
1171 * If cgroup removal path got to blk_group first and removed
1172 * it from cgroup list, then it will take care of destroying
1173 * cfqg also.
1174 */
Tejun Heo03814112012-03-05 13:15:14 -08001175 if (!cfq_blkiocg_del_blkio_group(cfqg_to_blkg(cfqg)))
Vivek Goyalb1c35762009-12-03 12:59:47 -05001176 cfq_destroy_cfqg(cfqd, cfqg);
Tejun Heo72e06c22012-03-05 13:15:00 -08001177 else
1178 empty = false;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001179 }
Tejun Heo72e06c22012-03-05 13:15:00 -08001180 return empty;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001181}
1182
1183/*
1184 * Blk cgroup controller notification saying that blkio_group object is being
1185 * delinked as associated cgroup object is going away. That also means that
1186 * no new IO will come in this group. So get rid of this group as soon as
1187 * any pending IO in the group is finished.
1188 *
1189 * This function is called under rcu_read_lock(). key is the rcu protected
Tejun Heoca32aef2012-03-05 13:15:03 -08001190 * pointer. That means @q is a valid request_queue pointer as long as we
1191 * are rcu read lock.
Vivek Goyalb1c35762009-12-03 12:59:47 -05001192 *
Tejun Heoca32aef2012-03-05 13:15:03 -08001193 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
Vivek Goyalb1c35762009-12-03 12:59:47 -05001194 * it should not be NULL as even if elevator was exiting, cgroup deltion
1195 * path got to it first.
1196 */
Tejun Heoca32aef2012-03-05 13:15:03 -08001197static void cfq_unlink_blkio_group(struct request_queue *q,
1198 struct blkio_group *blkg)
Vivek Goyalb1c35762009-12-03 12:59:47 -05001199{
Tejun Heoca32aef2012-03-05 13:15:03 -08001200 struct cfq_data *cfqd = q->elevator->elevator_data;
1201 unsigned long flags;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001202
Tejun Heoca32aef2012-03-05 13:15:03 -08001203 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo03814112012-03-05 13:15:14 -08001204 cfq_destroy_cfqg(cfqd, blkg_to_cfqg(blkg));
Tejun Heoca32aef2012-03-05 13:15:03 -08001205 spin_unlock_irqrestore(q->queue_lock, flags);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001206}
1207
Tejun Heo72e06c22012-03-05 13:15:00 -08001208static struct elevator_type iosched_cfq;
1209
1210static bool cfq_clear_queue(struct request_queue *q)
1211{
1212 lockdep_assert_held(q->queue_lock);
1213
1214 /* shoot down blkgs iff the current elevator is cfq */
1215 if (!q->elevator || q->elevator->type != &iosched_cfq)
1216 return true;
1217
1218 return cfq_release_cfq_groups(q->elevator->elevator_data);
1219}
1220
Vivek Goyal25fb5162009-12-03 12:59:46 -05001221#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001222static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1223 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001224{
Tejun Heof51b8022012-03-05 13:15:05 -08001225 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001226}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001227
1228static inline struct cfq_group *cfq_ref_get_cfqg(struct cfq_group *cfqg)
1229{
Dmitry Monakhov50eaeb32010-04-28 19:50:33 +02001230 return cfqg;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001231}
1232
Vivek Goyal25fb5162009-12-03 12:59:46 -05001233static inline void
1234cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1235 cfqq->cfqg = cfqg;
1236}
1237
Vivek Goyalb1c35762009-12-03 12:59:47 -05001238static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1239static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1240
Vivek Goyal25fb5162009-12-03 12:59:46 -05001241#endif /* GROUP_IOSCHED */
1242
Jens Axboe498d3aa22007-04-26 12:54:48 +02001243/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001244 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001245 * requests waiting to be processed. It is sorted in the order that
1246 * we will service the queues.
1247 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001248static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001249 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001250{
Jens Axboe08717142008-01-28 11:38:15 +01001251 struct rb_node **p, *parent;
1252 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001253 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001254 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001255 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001256 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001257
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001258 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001259 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001260 if (cfq_class_idle(cfqq)) {
1261 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001262 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001263 if (parent && parent != &cfqq->rb_node) {
1264 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1265 rb_key += __cfqq->rb_key;
1266 } else
1267 rb_key += jiffies;
1268 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001269 /*
1270 * Get our rb key offset. Subtract any residual slice
1271 * value carried from last service. A negative resid
1272 * count indicates slice overrun, and this should position
1273 * the next service time further away in the tree.
1274 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001275 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001276 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001277 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001278 } else {
1279 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001280 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001281 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1282 }
Jens Axboed9e76202007-04-20 14:27:50 +02001283
1284 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001285 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001286 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001287 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001288 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001289 if (rb_key == cfqq->rb_key &&
1290 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001291 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001292
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001293 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1294 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001295 }
Jens Axboed9e76202007-04-20 14:27:50 +02001296
Jens Axboe498d3aa22007-04-26 12:54:48 +02001297 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001298 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001299 cfqq->service_tree = service_tree;
1300 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001301 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001302 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001303
Jens Axboed9e76202007-04-20 14:27:50 +02001304 parent = *p;
1305 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1306
Jens Axboe0c534e02007-04-18 20:01:57 +02001307 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001308 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001309 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001310 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001311 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001312 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001313 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001314 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001315 }
Jens Axboe67060e32007-04-18 20:13:32 +02001316
1317 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001318 }
1319
Jens Axboecc09e292007-04-26 12:53:50 +02001320 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001321 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001322
Jens Axboed9e76202007-04-20 14:27:50 +02001323 cfqq->rb_key = rb_key;
1324 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001325 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1326 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001327 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001328 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001329 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Jens Axboea36e71f2009-04-15 12:15:11 +02001332static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001333cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1334 sector_t sector, struct rb_node **ret_parent,
1335 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001336{
Jens Axboea36e71f2009-04-15 12:15:11 +02001337 struct rb_node **p, *parent;
1338 struct cfq_queue *cfqq = NULL;
1339
1340 parent = NULL;
1341 p = &root->rb_node;
1342 while (*p) {
1343 struct rb_node **n;
1344
1345 parent = *p;
1346 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1347
1348 /*
1349 * Sort strictly based on sector. Smallest to the left,
1350 * largest to the right.
1351 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001352 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001353 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001354 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001355 n = &(*p)->rb_left;
1356 else
1357 break;
1358 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001359 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001360 }
1361
1362 *ret_parent = parent;
1363 if (rb_link)
1364 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001365 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001366}
1367
1368static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1369{
Jens Axboea36e71f2009-04-15 12:15:11 +02001370 struct rb_node **p, *parent;
1371 struct cfq_queue *__cfqq;
1372
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001373 if (cfqq->p_root) {
1374 rb_erase(&cfqq->p_node, cfqq->p_root);
1375 cfqq->p_root = NULL;
1376 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001377
1378 if (cfq_class_idle(cfqq))
1379 return;
1380 if (!cfqq->next_rq)
1381 return;
1382
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001383 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001384 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1385 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001386 if (!__cfqq) {
1387 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001388 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1389 } else
1390 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001391}
1392
Jens Axboe498d3aa22007-04-26 12:54:48 +02001393/*
1394 * Update cfqq's position in the service tree.
1395 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001396static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001397{
Jens Axboe6d048f52007-04-25 12:44:27 +02001398 /*
1399 * Resorting requires the cfqq to be on the RR list already.
1400 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001401 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001402 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001403 cfq_prio_tree_add(cfqd, cfqq);
1404 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/*
1408 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001409 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
Jens Axboefebffd62008-01-28 13:19:43 +01001411static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Jens Axboe7b679132008-05-30 12:23:07 +02001413 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001414 BUG_ON(cfq_cfqq_on_rr(cfqq));
1415 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001417 if (cfq_cfqq_sync(cfqq))
1418 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jens Axboeedd75ff2007-04-19 12:03:34 +02001420 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Jens Axboe498d3aa22007-04-26 12:54:48 +02001423/*
1424 * Called when the cfqq no longer has requests pending, remove it from
1425 * the service tree.
1426 */
Jens Axboefebffd62008-01-28 13:19:43 +01001427static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jens Axboe7b679132008-05-30 12:23:07 +02001429 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001430 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1431 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001433 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1434 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1435 cfqq->service_tree = NULL;
1436 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001437 if (cfqq->p_root) {
1438 rb_erase(&cfqq->p_node, cfqq->p_root);
1439 cfqq->p_root = NULL;
1440 }
Jens Axboed9e76202007-04-20 14:27:50 +02001441
Justin TerAvest8184f932011-03-17 16:12:36 +01001442 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 BUG_ON(!cfqd->busy_queues);
1444 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001445 if (cfq_cfqq_sync(cfqq))
1446 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449/*
1450 * rb tree support functions
1451 */
Jens Axboefebffd62008-01-28 13:19:43 +01001452static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Jens Axboe5e705372006-07-13 12:39:25 +02001454 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001455 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jens Axboeb4878f22005-10-20 16:42:29 +02001457 BUG_ON(!cfqq->queued[sync]);
1458 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jens Axboe5e705372006-07-13 12:39:25 +02001460 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Vivek Goyalf04a6422009-12-03 12:59:40 -05001462 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1463 /*
1464 * Queue will be deleted from service tree when we actually
1465 * expire it later. Right now just remove it from prio tree
1466 * as it is empty.
1467 */
1468 if (cfqq->p_root) {
1469 rb_erase(&cfqq->p_node, cfqq->p_root);
1470 cfqq->p_root = NULL;
1471 }
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Jens Axboe5e705372006-07-13 12:39:25 +02001475static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Jens Axboe5e705372006-07-13 12:39:25 +02001477 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001479 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jens Axboe5380a102006-07-13 12:37:56 +02001481 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jeff Moyer796d5112011-06-02 21:19:05 +02001483 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001484
1485 if (!cfq_cfqq_on_rr(cfqq))
1486 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001487
1488 /*
1489 * check if this request is a better next-serve candidate
1490 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001491 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001492 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001493
1494 /*
1495 * adjust priority tree position, if ->next_rq changes
1496 */
1497 if (prev != cfqq->next_rq)
1498 cfq_prio_tree_add(cfqd, cfqq);
1499
Jens Axboe5044eed2007-04-25 11:53:48 +02001500 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Jens Axboefebffd62008-01-28 13:19:43 +01001503static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Jens Axboe5380a102006-07-13 12:37:56 +02001505 elv_rb_del(&cfqq->sort_list, rq);
1506 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001507 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001508 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001509 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001510 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
1511 cfqg_to_blkg(cfqq->cfqd->serving_group),
1512 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Jens Axboe206dc692006-03-28 13:03:44 +02001515static struct request *
1516cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jens Axboe206dc692006-03-28 13:03:44 +02001518 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001519 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001520 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jens Axboe4ac845a2008-01-24 08:44:49 +01001522 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001523 if (!cic)
1524 return NULL;
1525
1526 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001527 if (cfqq) {
1528 sector_t sector = bio->bi_sector + bio_sectors(bio);
1529
Jens Axboe21183b02006-07-13 12:33:14 +02001530 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return NULL;
1534}
1535
Jens Axboe165125e2007-07-24 09:28:11 +02001536static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001537{
1538 struct cfq_data *cfqd = q->elevator->elevator_data;
1539
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001540 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001541 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001542 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001543
Tejun Heo5b936292009-05-07 22:24:38 +09001544 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001545}
1546
Jens Axboe165125e2007-07-24 09:28:11 +02001547static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Jens Axboe22e2c502005-06-27 10:55:12 +02001549 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001551 WARN_ON(!cfqd->rq_in_driver);
1552 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001553 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001554 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Jens Axboeb4878f22005-10-20 16:42:29 +02001557static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Jens Axboe5e705372006-07-13 12:39:25 +02001559 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001560
Jens Axboe5e705372006-07-13 12:39:25 +02001561 if (cfqq->next_rq == rq)
1562 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Jens Axboeb4878f22005-10-20 16:42:29 +02001564 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001565 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001566
Aaron Carroll45333d52008-08-26 15:52:36 +02001567 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001568 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001569 rq_data_dir(rq), rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001570 if (rq->cmd_flags & REQ_PRIO) {
1571 WARN_ON(!cfqq->prio_pending);
1572 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Jens Axboe165125e2007-07-24 09:28:11 +02001576static int cfq_merge(struct request_queue *q, struct request **req,
1577 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct cfq_data *cfqd = q->elevator->elevator_data;
1580 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jens Axboe206dc692006-03-28 13:03:44 +02001582 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001583 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001584 *req = __rq;
1585 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Jens Axboe165125e2007-07-24 09:28:11 +02001591static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001592 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Jens Axboe21183b02006-07-13 12:33:14 +02001594 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001595 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Jens Axboe5e705372006-07-13 12:39:25 +02001597 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Divyesh Shah812d4022010-04-08 21:14:23 -07001601static void cfq_bio_merged(struct request_queue *q, struct request *req,
1602 struct bio *bio)
1603{
Tejun Heo03814112012-03-05 13:15:14 -08001604 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001605 bio_data_dir(bio), cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608static void
Jens Axboe165125e2007-07-24 09:28:11 +02001609cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct request *next)
1611{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001612 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001613 struct cfq_data *cfqd = q->elevator->elevator_data;
1614
Jens Axboe22e2c502005-06-27 10:55:12 +02001615 /*
1616 * reposition in fifo if next is older than rq
1617 */
1618 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001619 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001620 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001621 rq_set_fifo_time(rq, rq_fifo_time(next));
1622 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001623
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001624 if (cfqq->next_rq == next)
1625 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001626 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08001627 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Vivek Goyale98ef892010-06-18 10:39:47 -04001628 rq_data_dir(next), rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001629
1630 cfqq = RQ_CFQQ(next);
1631 /*
1632 * all requests of this queue are merged to other queues, delete it
1633 * from the service tree. If it's the active_queue,
1634 * cfq_dispatch_requests() will choose to expire it or do idle
1635 */
1636 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1637 cfqq != cfqd->active_queue)
1638 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001639}
1640
Jens Axboe165125e2007-07-24 09:28:11 +02001641static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001642 struct bio *bio)
1643{
1644 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01001645 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001646 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001647
1648 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001649 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001650 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001651 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001652 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001653
1654 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01001655 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01001656 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001657 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01001658 cic = cfq_cic_lookup(cfqd, current->io_context);
1659 if (!cic)
1660 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001661
Vasily Tarasov91fac312007-04-25 12:29:51 +02001662 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001663 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001664}
1665
Divyesh Shah812df482010-04-08 21:15:35 -07001666static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1667{
1668 del_timer(&cfqd->idle_slice_timer);
Tejun Heo03814112012-03-05 13:15:14 -08001669 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
Divyesh Shah812df482010-04-08 21:15:35 -07001670}
1671
Jens Axboefebffd62008-01-28 13:19:43 +01001672static void __cfq_set_active_queue(struct cfq_data *cfqd,
1673 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001674{
1675 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001676 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1677 cfqd->serving_prio, cfqd->serving_type);
Tejun Heo03814112012-03-05 13:15:14 -08001678 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg));
Justin TerAvest62a37f62011-03-23 08:25:44 +01001679 cfqq->slice_start = 0;
1680 cfqq->dispatch_start = jiffies;
1681 cfqq->allocated_slice = 0;
1682 cfqq->slice_end = 0;
1683 cfqq->slice_dispatch = 0;
1684 cfqq->nr_sectors = 0;
1685
1686 cfq_clear_cfqq_wait_request(cfqq);
1687 cfq_clear_cfqq_must_dispatch(cfqq);
1688 cfq_clear_cfqq_must_alloc_slice(cfqq);
1689 cfq_clear_cfqq_fifo_expire(cfqq);
1690 cfq_mark_cfqq_slice_new(cfqq);
1691
1692 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001693 }
1694
1695 cfqd->active_queue = cfqq;
1696}
1697
1698/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001699 * current cfqq expired its slice (or was too idle), select new one
1700 */
1701static void
1702__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001703 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001704{
Jens Axboe7b679132008-05-30 12:23:07 +02001705 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1706
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001707 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07001708 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001709
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001710 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001711 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001712
1713 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01001714 * If this cfqq is shared between multiple processes, check to
1715 * make sure that those processes are still issuing I/Os within
1716 * the mean seek distance. If not, it may be time to break the
1717 * queues apart again.
1718 */
1719 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1720 cfq_mark_cfqq_split_coop(cfqq);
1721
1722 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001723 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001724 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01001725 if (timed_out) {
1726 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07001727 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001728 else
1729 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001730 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1731 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001732
Vivek Goyale5ff0822010-04-26 19:25:11 +02001733 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001734
Vivek Goyalf04a6422009-12-03 12:59:40 -05001735 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1736 cfq_del_cfqq_rr(cfqd, cfqq);
1737
Jens Axboeedd75ff2007-04-19 12:03:34 +02001738 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001739
1740 if (cfqq == cfqd->active_queue)
1741 cfqd->active_queue = NULL;
1742
1743 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01001744 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001745 cfqd->active_cic = NULL;
1746 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001747}
1748
Vivek Goyale5ff0822010-04-26 19:25:11 +02001749static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001750{
1751 struct cfq_queue *cfqq = cfqd->active_queue;
1752
1753 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02001754 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001755}
1756
Jens Axboe498d3aa22007-04-26 12:54:48 +02001757/*
1758 * Get next queue for service. Unless we have a queue preemption,
1759 * we'll simply select the first cfqq in the service tree.
1760 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001761static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001762{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001763 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001764 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05001765 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001766
Vivek Goyalf04a6422009-12-03 12:59:40 -05001767 if (!cfqd->rq_queued)
1768 return NULL;
1769
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001770 /* There is nothing to dispatch */
1771 if (!service_tree)
1772 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001773 if (RB_EMPTY_ROOT(&service_tree->rb))
1774 return NULL;
1775 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001776}
1777
Vivek Goyalf04a6422009-12-03 12:59:40 -05001778static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1779{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001780 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001781 struct cfq_queue *cfqq;
1782 int i, j;
1783 struct cfq_rb_root *st;
1784
1785 if (!cfqd->rq_queued)
1786 return NULL;
1787
Vivek Goyal25fb5162009-12-03 12:59:46 -05001788 cfqg = cfq_get_next_cfqg(cfqd);
1789 if (!cfqg)
1790 return NULL;
1791
Vivek Goyalf04a6422009-12-03 12:59:40 -05001792 for_each_cfqg_st(cfqg, i, j, st)
1793 if ((cfqq = cfq_rb_first(st)) != NULL)
1794 return cfqq;
1795 return NULL;
1796}
1797
Jens Axboe498d3aa22007-04-26 12:54:48 +02001798/*
1799 * Get and set a new active queue for service.
1800 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001801static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1802 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001803{
Jens Axboee00ef792009-11-04 08:54:55 +01001804 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001805 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001806
Jens Axboe22e2c502005-06-27 10:55:12 +02001807 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001808 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001809}
1810
Jens Axboed9e76202007-04-20 14:27:50 +02001811static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1812 struct request *rq)
1813{
Tejun Heo83096eb2009-05-07 22:24:39 +09001814 if (blk_rq_pos(rq) >= cfqd->last_position)
1815 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001816 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001817 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001818}
1819
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001820static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01001821 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001822{
Shaohua Lie9ce3352010-03-19 08:03:04 +01001823 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001824}
1825
Jens Axboea36e71f2009-04-15 12:15:11 +02001826static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1827 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001828{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001829 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001830 struct rb_node *parent, *node;
1831 struct cfq_queue *__cfqq;
1832 sector_t sector = cfqd->last_position;
1833
1834 if (RB_EMPTY_ROOT(root))
1835 return NULL;
1836
1837 /*
1838 * First, if we find a request starting at the end of the last
1839 * request, choose it.
1840 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001841 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001842 if (__cfqq)
1843 return __cfqq;
1844
1845 /*
1846 * If the exact sector wasn't found, the parent of the NULL leaf
1847 * will contain the closest sector.
1848 */
1849 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001850 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001851 return __cfqq;
1852
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001853 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001854 node = rb_next(&__cfqq->p_node);
1855 else
1856 node = rb_prev(&__cfqq->p_node);
1857 if (!node)
1858 return NULL;
1859
1860 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001861 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001862 return __cfqq;
1863
1864 return NULL;
1865}
1866
1867/*
1868 * cfqd - obvious
1869 * cur_cfqq - passed in so that we don't decide that the current queue is
1870 * closely cooperating with itself.
1871 *
1872 * So, basically we're assuming that that cur_cfqq has dispatched at least
1873 * one request, and that cfqd->last_position reflects a position on the disk
1874 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1875 * assumption.
1876 */
1877static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001878 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001879{
1880 struct cfq_queue *cfqq;
1881
Divyesh Shah39c01b22010-03-25 15:45:57 +01001882 if (cfq_class_idle(cur_cfqq))
1883 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001884 if (!cfq_cfqq_sync(cur_cfqq))
1885 return NULL;
1886 if (CFQQ_SEEKY(cur_cfqq))
1887 return NULL;
1888
Jens Axboea36e71f2009-04-15 12:15:11 +02001889 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01001890 * Don't search priority tree if it's the only queue in the group.
1891 */
1892 if (cur_cfqq->cfqg->nr_cfqq == 1)
1893 return NULL;
1894
1895 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001896 * We should notice if some of the queues are cooperating, eg
1897 * working closely on the same area of the disk. In that case,
1898 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001899 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001900 cfqq = cfqq_close(cfqd, cur_cfqq);
1901 if (!cfqq)
1902 return NULL;
1903
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001904 /* If new queue belongs to different cfq_group, don't choose it */
1905 if (cur_cfqq->cfqg != cfqq->cfqg)
1906 return NULL;
1907
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001908 /*
1909 * It only makes sense to merge sync queues.
1910 */
1911 if (!cfq_cfqq_sync(cfqq))
1912 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001913 if (CFQQ_SEEKY(cfqq))
1914 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001915
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001916 /*
1917 * Do not merge queues of different priority classes
1918 */
1919 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1920 return NULL;
1921
Jens Axboea36e71f2009-04-15 12:15:11 +02001922 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001923}
1924
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001925/*
1926 * Determine whether we should enforce idle window for this queue.
1927 */
1928
1929static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1930{
1931 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001932 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001933
Vivek Goyalf04a6422009-12-03 12:59:40 -05001934 BUG_ON(!service_tree);
1935 BUG_ON(!service_tree->count);
1936
Vivek Goyalb6508c12010-08-23 12:23:33 +02001937 if (!cfqd->cfq_slice_idle)
1938 return false;
1939
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001940 /* We never do for idle class queues. */
1941 if (prio == IDLE_WORKLOAD)
1942 return false;
1943
1944 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01001945 if (cfq_cfqq_idle_window(cfqq) &&
1946 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001947 return true;
1948
1949 /*
1950 * Otherwise, we do only if they are the last ones
1951 * in their service tree.
1952 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02001953 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
1954 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01001955 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001956 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1957 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01001958 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001959}
1960
Jens Axboe6d048f52007-04-25 12:44:27 +02001961static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001962{
Jens Axboe17926692007-01-19 11:59:30 +11001963 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01001964 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001965 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001966
Jens Axboea68bbdd2008-09-24 13:03:33 +02001967 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001968 * SSD device without seek penalty, disable idling. But only do so
1969 * for devices that support queuing, otherwise we still have a problem
1970 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001971 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001972 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001973 return;
1974
Jens Axboedd67d052006-06-21 09:36:18 +02001975 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001976 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001977
1978 /*
1979 * idle is disabled, either manually or by past process history
1980 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001981 if (!cfq_should_idle(cfqd, cfqq)) {
1982 /* no queue idling. Check for group idling */
1983 if (cfqd->cfq_group_idle)
1984 group_idle = cfqd->cfq_group_idle;
1985 else
1986 return;
1987 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001988
Jens Axboe22e2c502005-06-27 10:55:12 +02001989 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001990 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001991 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001992 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001993 return;
1994
1995 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001996 * task has exited, don't wait
1997 */
Jens Axboe206dc692006-03-28 13:03:44 +02001998 cic = cfqd->active_cic;
Tejun Heoc5869802011-12-14 00:33:41 +01001999 if (!cic || !atomic_read(&cic->icq.ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02002000 return;
2001
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002002 /*
2003 * If our average think time is larger than the remaining time
2004 * slice, then don't idle. This avoids overrunning the allotted
2005 * time slice.
2006 */
Shaohua Li383cd722011-07-12 14:24:35 +02002007 if (sample_valid(cic->ttime.ttime_samples) &&
2008 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002009 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002010 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002011 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002012 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002013
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002014 /* There are other queues in the group, don't do group idle */
2015 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2016 return;
2017
Jens Axboe3b181522005-06-27 10:56:24 +02002018 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002019
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002020 if (group_idle)
2021 sl = cfqd->cfq_group_idle;
2022 else
2023 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002024
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002025 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo03814112012-03-05 13:15:14 -08002026 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg));
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002027 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2028 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Jens Axboe498d3aa22007-04-26 12:54:48 +02002031/*
2032 * Move request from internal lists to the request queue dispatch list.
2033 */
Jens Axboe165125e2007-07-24 09:28:11 +02002034static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002036 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002037 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002038
Jens Axboe7b679132008-05-30 12:23:07 +02002039 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2040
Jeff Moyer06d21882009-09-11 17:08:59 +02002041 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002042 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002043 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002044 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002045 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002046
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002047 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002048 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08002049 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
2050 blk_rq_bytes(rq), rq_data_dir(rq),
2051 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054/*
2055 * return expired entry, or NULL to just start from scratch in rbtree
2056 */
Jens Axboefebffd62008-01-28 13:19:43 +01002057static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Jens Axboe30996f42009-10-05 11:03:39 +02002059 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Jens Axboe3b181522005-06-27 10:56:24 +02002061 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002063
2064 cfq_mark_cfqq_fifo_expire(cfqq);
2065
Jens Axboe89850f72006-07-22 16:48:31 +02002066 if (list_empty(&cfqq->fifo))
2067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Jens Axboe89850f72006-07-22 16:48:31 +02002069 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002070 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002071 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jens Axboe30996f42009-10-05 11:03:39 +02002073 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002074 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Jens Axboe22e2c502005-06-27 10:55:12 +02002077static inline int
2078cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2079{
2080 const int base_rq = cfqd->cfq_slice_async_rq;
2081
2082 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2083
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002084 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002085}
2086
2087/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002088 * Must be called with the queue_lock held.
2089 */
2090static int cfqq_process_refs(struct cfq_queue *cfqq)
2091{
2092 int process_refs, io_refs;
2093
2094 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002095 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002096 BUG_ON(process_refs < 0);
2097 return process_refs;
2098}
2099
2100static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2101{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002102 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002103 struct cfq_queue *__cfqq;
2104
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002105 /*
2106 * If there are no process references on the new_cfqq, then it is
2107 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2108 * chain may have dropped their last reference (not just their
2109 * last process reference).
2110 */
2111 if (!cfqq_process_refs(new_cfqq))
2112 return;
2113
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002114 /* Avoid a circular list and skip interim queue merges */
2115 while ((__cfqq = new_cfqq->new_cfqq)) {
2116 if (__cfqq == cfqq)
2117 return;
2118 new_cfqq = __cfqq;
2119 }
2120
2121 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002122 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002123 /*
2124 * If the process for the cfqq has gone away, there is no
2125 * sense in merging the queues.
2126 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002127 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002128 return;
2129
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002130 /*
2131 * Merge in the direction of the lesser amount of work.
2132 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002133 if (new_process_refs >= process_refs) {
2134 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002135 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002136 } else {
2137 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002138 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002139 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002140}
2141
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002142static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002143 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002144{
2145 struct cfq_queue *queue;
2146 int i;
2147 bool key_valid = false;
2148 unsigned long lowest_key = 0;
2149 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2150
Vivek Goyal65b32a52009-12-16 17:52:59 -05002151 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2152 /* select the one with lowest rb_key */
2153 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002154 if (queue &&
2155 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2156 lowest_key = queue->rb_key;
2157 cur_best = i;
2158 key_valid = true;
2159 }
2160 }
2161
2162 return cur_best;
2163}
2164
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002165static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002166{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002167 unsigned slice;
2168 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002169 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002170 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002171 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002172
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002173 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002174 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002175 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002176 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002177 cfqd->serving_prio = BE_WORKLOAD;
2178 else {
2179 cfqd->serving_prio = IDLE_WORKLOAD;
2180 cfqd->workload_expires = jiffies + 1;
2181 return;
2182 }
2183
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002184 if (original_prio != cfqd->serving_prio)
2185 goto new_workload;
2186
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002187 /*
2188 * For RT and BE, we have to choose also the type
2189 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2190 * expiration time
2191 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002192 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002193 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002194
2195 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002196 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002197 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002198 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002199 return;
2200
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002201new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002202 /* otherwise select new workload type */
2203 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002204 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2205 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002206 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002207
2208 /*
2209 * the workload slice is computed as a fraction of target latency
2210 * proportional to the number of queues in that workload, over
2211 * all the queues in the same priority class
2212 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002213 group_slice = cfq_group_slice(cfqd, cfqg);
2214
2215 slice = group_slice * count /
2216 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2217 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002218
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002219 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2220 unsigned int tmp;
2221
2222 /*
2223 * Async queues are currently system wide. Just taking
2224 * proportion of queues with-in same group will lead to higher
2225 * async ratio system wide as generally root group is going
2226 * to have higher weight. A more accurate thing would be to
2227 * calculate system wide asnc/sync ratio.
2228 */
2229 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2230 tmp = tmp/cfqd->busy_queues;
2231 slice = min_t(unsigned, slice, tmp);
2232
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002233 /* async workload slice is scaled down according to
2234 * the sync/async slice ratio. */
2235 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002236 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002237 /* sync workload slice is at least 2 * cfq_slice_idle */
2238 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2239
2240 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002241 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002242 cfqd->workload_expires = jiffies + slice;
2243}
2244
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002245static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2246{
2247 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002248 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002249
2250 if (RB_EMPTY_ROOT(&st->rb))
2251 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002252 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002253 update_min_vdisktime(st);
2254 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002255}
2256
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002257static void cfq_choose_cfqg(struct cfq_data *cfqd)
2258{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002259 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2260
2261 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002262
2263 /* Restore the workload type data */
2264 if (cfqg->saved_workload_slice) {
2265 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2266 cfqd->serving_type = cfqg->saved_workload;
2267 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002268 } else
2269 cfqd->workload_expires = jiffies - 1;
2270
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002271 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002272}
2273
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002274/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002275 * Select a queue for service. If we have a current active queue,
2276 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002277 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002278static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002279{
Jens Axboea36e71f2009-04-15 12:15:11 +02002280 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002281
2282 cfqq = cfqd->active_queue;
2283 if (!cfqq)
2284 goto new_queue;
2285
Vivek Goyalf04a6422009-12-03 12:59:40 -05002286 if (!cfqd->rq_queued)
2287 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002288
2289 /*
2290 * We were waiting for group to get backlogged. Expire the queue
2291 */
2292 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2293 goto expire;
2294
Jens Axboe22e2c502005-06-27 10:55:12 +02002295 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002296 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002297 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002298 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2299 /*
2300 * If slice had not expired at the completion of last request
2301 * we might not have turned on wait_busy flag. Don't expire
2302 * the queue yet. Allow the group to get backlogged.
2303 *
2304 * The very fact that we have used the slice, that means we
2305 * have been idling all along on this queue and it should be
2306 * ok to wait for this request to complete.
2307 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002308 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2309 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2310 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002311 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002312 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002313 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002314 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002315
2316 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002317 * The active queue has requests and isn't expired, allow it to
2318 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002319 */
Jens Axboedd67d052006-06-21 09:36:18 +02002320 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002321 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002322
2323 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002324 * If another queue has a request waiting within our mean seek
2325 * distance, let it run. The expire code will check for close
2326 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002327 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002328 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002329 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002330 if (new_cfqq) {
2331 if (!cfqq->new_cfqq)
2332 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002333 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002334 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002335
2336 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002337 * No requests pending. If the active queue still has requests in
2338 * flight or is idling for a new request, allow either of these
2339 * conditions to happen (or time out) before selecting a new queue.
2340 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002341 if (timer_pending(&cfqd->idle_slice_timer)) {
2342 cfqq = NULL;
2343 goto keep_queue;
2344 }
2345
Shaohua Li8e1ac662010-11-08 15:01:04 +01002346 /*
2347 * This is a deep seek queue, but the device is much faster than
2348 * the queue can deliver, don't idle
2349 **/
2350 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2351 (cfq_cfqq_slice_new(cfqq) ||
2352 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2353 cfq_clear_cfqq_deep(cfqq);
2354 cfq_clear_cfqq_idle_window(cfqq);
2355 }
2356
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002357 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2358 cfqq = NULL;
2359 goto keep_queue;
2360 }
2361
2362 /*
2363 * If group idle is enabled and there are requests dispatched from
2364 * this group, wait for requests to complete.
2365 */
2366check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002367 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2368 cfqq->cfqg->dispatched &&
2369 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002370 cfqq = NULL;
2371 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002372 }
2373
Jens Axboe3b181522005-06-27 10:56:24 +02002374expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002375 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002376new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002377 /*
2378 * Current queue expired. Check if we have to switch to a new
2379 * service tree
2380 */
2381 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002382 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002383
Jens Axboea36e71f2009-04-15 12:15:11 +02002384 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002385keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002386 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002387}
2388
Jens Axboefebffd62008-01-28 13:19:43 +01002389static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002390{
2391 int dispatched = 0;
2392
2393 while (cfqq->next_rq) {
2394 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2395 dispatched++;
2396 }
2397
2398 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002399
2400 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002401 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002402 return dispatched;
2403}
2404
Jens Axboe498d3aa22007-04-26 12:54:48 +02002405/*
2406 * Drain our current requests. Used for barriers and when switching
2407 * io schedulers on-the-fly.
2408 */
Jens Axboed9e76202007-04-20 14:27:50 +02002409static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002410{
Jens Axboe08717142008-01-28 11:38:15 +01002411 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002412 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002413
Divyesh Shah3440c492010-04-09 09:29:57 +02002414 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002415 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002416 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2417 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002418 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002419 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002420
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002421 BUG_ON(cfqd->busy_queues);
2422
Jeff Moyer69237152009-06-12 15:29:30 +02002423 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002424 return dispatched;
2425}
2426
Shaohua Liabc3c742010-03-01 09:20:54 +01002427static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2428 struct cfq_queue *cfqq)
2429{
2430 /* the queue hasn't finished any request, can't estimate */
2431 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002432 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002433 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2434 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002435 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002436
Shaohua Lic1e44752010-11-08 15:01:02 +01002437 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002438}
2439
Jens Axboe0b182d62009-10-06 20:49:37 +02002440static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002441{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002442 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Jens Axboe2f5cb732009-04-07 08:51:19 +02002444 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002445 * Drain async requests before we start sync IO
2446 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002447 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002448 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002449
2450 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002451 * If this is an async queue and we have sync IO in flight, let it wait
2452 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002453 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002454 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002455
Shaohua Liabc3c742010-03-01 09:20:54 +01002456 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002457 if (cfq_class_idle(cfqq))
2458 max_dispatch = 1;
2459
2460 /*
2461 * Does this cfqq already have too much IO in flight?
2462 */
2463 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002464 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002465 /*
2466 * idle queue must always only have a single IO in flight
2467 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002468 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002469 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002470
Jens Axboe2f5cb732009-04-07 08:51:19 +02002471 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002472 * If there is only one sync queue
2473 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002474 * queue no dispatch limit. The reason is a sync queue can
2475 * preempt async queue, limiting the sync queue doesn't make
2476 * sense. This is useful for aiostress test.
2477 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002478 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2479 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002480
2481 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002482 * We have other queues, don't allow more IO from this one
2483 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002484 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2485 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002486 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002487
Jens Axboe2f5cb732009-04-07 08:51:19 +02002488 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002489 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002490 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002491 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002492 max_dispatch = -1;
2493 else
2494 /*
2495 * Normally we start throttling cfqq when cfq_quantum/2
2496 * requests have been dispatched. But we can drive
2497 * deeper queue depths at the beginning of slice
2498 * subjected to upper limit of cfq_quantum.
2499 * */
2500 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002501 }
2502
2503 /*
2504 * Async queues must wait a bit before being allowed dispatch.
2505 * We also ramp up the dispatch depth gradually for async IO,
2506 * based on the last sync IO we serviced
2507 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002508 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002509 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002510 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002511
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002512 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002513 if (!depth && !cfqq->dispatched)
2514 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002515 if (depth < max_dispatch)
2516 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518
Jens Axboe0b182d62009-10-06 20:49:37 +02002519 /*
2520 * If we're below the current max, allow a dispatch
2521 */
2522 return cfqq->dispatched < max_dispatch;
2523}
2524
2525/*
2526 * Dispatch a request from cfqq, moving them to the request queue
2527 * dispatch list.
2528 */
2529static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2530{
2531 struct request *rq;
2532
2533 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2534
2535 if (!cfq_may_dispatch(cfqd, cfqq))
2536 return false;
2537
2538 /*
2539 * follow expired path, else get first next available
2540 */
2541 rq = cfq_check_fifo(cfqq);
2542 if (!rq)
2543 rq = cfqq->next_rq;
2544
2545 /*
2546 * insert request into driver dispatch list
2547 */
2548 cfq_dispatch_insert(cfqd->queue, rq);
2549
2550 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002551 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002552
Tejun Heoc5869802011-12-14 00:33:41 +01002553 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002554 cfqd->active_cic = cic;
2555 }
2556
2557 return true;
2558}
2559
2560/*
2561 * Find the cfqq that we need to service and move a request from that to the
2562 * dispatch list
2563 */
2564static int cfq_dispatch_requests(struct request_queue *q, int force)
2565{
2566 struct cfq_data *cfqd = q->elevator->elevator_data;
2567 struct cfq_queue *cfqq;
2568
2569 if (!cfqd->busy_queues)
2570 return 0;
2571
2572 if (unlikely(force))
2573 return cfq_forced_dispatch(cfqd);
2574
2575 cfqq = cfq_select_queue(cfqd);
2576 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002577 return 0;
2578
Jens Axboe2f5cb732009-04-07 08:51:19 +02002579 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002580 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002581 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002582 if (!cfq_dispatch_request(cfqd, cfqq))
2583 return 0;
2584
Jens Axboe2f5cb732009-04-07 08:51:19 +02002585 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002586 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002587
2588 /*
2589 * expire an async queue immediately if it has used up its slice. idle
2590 * queue always expire after 1 dispatch round.
2591 */
2592 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2593 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2594 cfq_class_idle(cfqq))) {
2595 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002596 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002597 }
2598
Shan Weib217a902009-09-01 10:06:42 +02002599 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002600 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603/*
Jens Axboe5e705372006-07-13 12:39:25 +02002604 * task holds one reference to the queue, dropped when task exits. each rq
2605 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002607 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 * queue lock must be held here.
2609 */
2610static void cfq_put_queue(struct cfq_queue *cfqq)
2611{
Jens Axboe22e2c502005-06-27 10:55:12 +02002612 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002613 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002614
Shaohua Li30d7b942011-01-07 08:46:59 +01002615 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Shaohua Li30d7b942011-01-07 08:46:59 +01002617 cfqq->ref--;
2618 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return;
2620
Jens Axboe7b679132008-05-30 12:23:07 +02002621 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002623 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002624 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002626 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002627 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002628 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002629 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002630
Vivek Goyalf04a6422009-12-03 12:59:40 -05002631 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 kmem_cache_free(cfq_pool, cfqq);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002633 cfq_put_cfqg(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
Shaohua Lid02a2c02010-05-25 10:16:53 +02002636static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002637{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002638 struct cfq_queue *__cfqq, *next;
2639
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002640 /*
2641 * If this queue was scheduled to merge with another queue, be
2642 * sure to drop the reference taken on that queue (and others in
2643 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2644 */
2645 __cfqq = cfqq->new_cfqq;
2646 while (__cfqq) {
2647 if (__cfqq == cfqq) {
2648 WARN(1, "cfqq->new_cfqq loop detected\n");
2649 break;
2650 }
2651 next = __cfqq->new_cfqq;
2652 cfq_put_queue(__cfqq);
2653 __cfqq = next;
2654 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02002655}
2656
2657static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2658{
2659 if (unlikely(cfqq == cfqd->active_queue)) {
2660 __cfq_slice_expired(cfqd, cfqq, 0);
2661 cfq_schedule_dispatch(cfqd);
2662 }
2663
2664 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002665
Jens Axboe89850f72006-07-22 16:48:31 +02002666 cfq_put_queue(cfqq);
2667}
2668
Tejun Heo9b84cac2011-12-14 00:33:42 +01002669static void cfq_init_icq(struct io_cq *icq)
2670{
2671 struct cfq_io_cq *cic = icq_to_cic(icq);
2672
2673 cic->ttime.last_end_request = jiffies;
2674}
2675
Tejun Heoc5869802011-12-14 00:33:41 +01002676static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02002677{
Tejun Heoc5869802011-12-14 00:33:41 +01002678 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01002679 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002680
Jens Axboeff6657c2009-04-08 10:58:57 +02002681 if (cic->cfqq[BLK_RW_ASYNC]) {
2682 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2683 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002684 }
2685
Jens Axboeff6657c2009-04-08 10:58:57 +02002686 if (cic->cfqq[BLK_RW_SYNC]) {
2687 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2688 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002689 }
Jens Axboe89850f72006-07-22 16:48:31 +02002690}
2691
Jens Axboefd0928d2008-01-24 08:52:45 +01002692static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002693{
2694 struct task_struct *tsk = current;
2695 int ioprio_class;
2696
Jens Axboe3b181522005-06-27 10:56:24 +02002697 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002698 return;
2699
Jens Axboefd0928d2008-01-24 08:52:45 +01002700 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002701 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002702 default:
2703 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2704 case IOPRIO_CLASS_NONE:
2705 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002706 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002707 */
2708 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002709 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002710 break;
2711 case IOPRIO_CLASS_RT:
2712 cfqq->ioprio = task_ioprio(ioc);
2713 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2714 break;
2715 case IOPRIO_CLASS_BE:
2716 cfqq->ioprio = task_ioprio(ioc);
2717 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2718 break;
2719 case IOPRIO_CLASS_IDLE:
2720 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2721 cfqq->ioprio = 7;
2722 cfq_clear_cfqq_idle_window(cfqq);
2723 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002724 }
2725
2726 /*
2727 * keep track of original prio settings in case we have to temporarily
2728 * elevate the priority of this queue
2729 */
2730 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02002731 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002732}
2733
Tejun Heoc5869802011-12-14 00:33:41 +01002734static void changed_ioprio(struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002735{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002736 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05002737 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02002738
Jens Axboecaaa5f92006-06-16 11:23:00 +02002739 if (unlikely(!cfqd))
2740 return;
2741
Jens Axboeff6657c2009-04-08 10:58:57 +02002742 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002743 if (cfqq) {
2744 struct cfq_queue *new_cfqq;
Tejun Heoc5869802011-12-14 00:33:41 +01002745 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->icq.ioc,
Jens Axboeff6657c2009-04-08 10:58:57 +02002746 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002747 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002748 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002749 cfq_put_queue(cfqq);
2750 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002751 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002752
Jens Axboeff6657c2009-04-08 10:58:57 +02002753 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002754 if (cfqq)
2755 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002756}
2757
Jens Axboed5036d72009-06-26 10:44:34 +02002758static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002759 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002760{
2761 RB_CLEAR_NODE(&cfqq->rb_node);
2762 RB_CLEAR_NODE(&cfqq->p_node);
2763 INIT_LIST_HEAD(&cfqq->fifo);
2764
Shaohua Li30d7b942011-01-07 08:46:59 +01002765 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02002766 cfqq->cfqd = cfqd;
2767
2768 cfq_mark_cfqq_prio_changed(cfqq);
2769
2770 if (is_sync) {
2771 if (!cfq_class_idle(cfqq))
2772 cfq_mark_cfqq_idle_window(cfqq);
2773 cfq_mark_cfqq_sync(cfqq);
2774 }
2775 cfqq->pid = pid;
2776}
2777
Vivek Goyal246103332009-12-03 12:59:51 -05002778#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoc5869802011-12-14 00:33:41 +01002779static void changed_cgroup(struct cfq_io_cq *cic)
Vivek Goyal246103332009-12-03 12:59:51 -05002780{
2781 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002782 struct cfq_data *cfqd = cic_to_cfqd(cic);
Vivek Goyal246103332009-12-03 12:59:51 -05002783 struct request_queue *q;
2784
2785 if (unlikely(!cfqd))
2786 return;
2787
2788 q = cfqd->queue;
2789
Vivek Goyal246103332009-12-03 12:59:51 -05002790 if (sync_cfqq) {
2791 /*
2792 * Drop reference to sync queue. A new sync queue will be
2793 * assigned in new group upon arrival of a fresh request.
2794 */
2795 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2796 cic_set_cfqq(cic, NULL, 1);
2797 cfq_put_queue(sync_cfqq);
2798 }
Vivek Goyal246103332009-12-03 12:59:51 -05002799}
Vivek Goyal246103332009-12-03 12:59:51 -05002800#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002803cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002804 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002806 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 struct cfq_queue *cfqq, *new_cfqq = NULL;
Tejun Heoc5869802011-12-14 00:33:41 +01002808 struct cfq_io_cq *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002809 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08002812 rcu_read_lock();
2813
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002814 blkcg = task_blkio_cgroup(current);
2815
Tejun Heocd1604f2012-03-05 13:15:06 -08002816 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
2817
Jens Axboe4ac845a2008-01-24 08:44:49 +01002818 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002819 /* cic always exists here */
2820 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Jens Axboe6118b702009-06-30 09:34:12 +02002822 /*
2823 * Always try a new alloc if we fell back to the OOM cfqq
2824 * originally, since it should just be a temporary situation.
2825 */
2826 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2827 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (new_cfqq) {
2829 cfqq = new_cfqq;
2830 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002831 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08002832 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002834 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002835 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002836 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002838 if (new_cfqq)
2839 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002840 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002841 cfqq = kmem_cache_alloc_node(cfq_pool,
2842 gfp_mask | __GFP_ZERO,
2843 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Jens Axboe6118b702009-06-30 09:34:12 +02002846 if (cfqq) {
2847 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2848 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002849 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002850 cfq_log_cfqq(cfqd, cfqq, "alloced");
2851 } else
2852 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854
2855 if (new_cfqq)
2856 kmem_cache_free(cfq_pool, new_cfqq);
2857
Tejun Heo2a7f1242012-03-05 13:15:01 -08002858 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return cfqq;
2860}
2861
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002862static struct cfq_queue **
2863cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2864{
Jens Axboefe094d92008-01-31 13:08:54 +01002865 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002866 case IOPRIO_CLASS_RT:
2867 return &cfqd->async_cfqq[0][ioprio];
2868 case IOPRIO_CLASS_BE:
2869 return &cfqd->async_cfqq[1][ioprio];
2870 case IOPRIO_CLASS_IDLE:
2871 return &cfqd->async_idle_cfqq;
2872 default:
2873 BUG();
2874 }
2875}
2876
Jens Axboe15c31be2007-07-10 13:43:25 +02002877static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002878cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002879 gfp_t gfp_mask)
2880{
Jens Axboefd0928d2008-01-24 08:52:45 +01002881 const int ioprio = task_ioprio(ioc);
2882 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002883 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002884 struct cfq_queue *cfqq = NULL;
2885
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002886 if (!is_sync) {
2887 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2888 cfqq = *async_cfqq;
2889 }
2890
Jens Axboe6118b702009-06-30 09:34:12 +02002891 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002892 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002893
2894 /*
2895 * pin the queue now that it's allocated, scheduler exit will prune it
2896 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002897 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01002898 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002899 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002900 }
2901
Shaohua Li30d7b942011-01-07 08:46:59 +01002902 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02002903 return cfqq;
2904}
2905
Jens Axboe22e2c502005-06-27 10:55:12 +02002906static void
Shaohua Li383cd722011-07-12 14:24:35 +02002907__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002908{
Shaohua Li383cd722011-07-12 14:24:35 +02002909 unsigned long elapsed = jiffies - ttime->last_end_request;
2910 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002911
Shaohua Li383cd722011-07-12 14:24:35 +02002912 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
2913 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
2914 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
2915}
2916
2917static void
2918cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002919 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02002920{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002921 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02002922 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002923 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
2924 cfqd->cfq_slice_idle);
2925 }
Shaohua Li7700fc42011-07-12 14:24:56 +02002926#ifdef CONFIG_CFQ_GROUP_IOSCHED
2927 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
2928#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02002929}
2930
Jens Axboe206dc692006-03-28 13:03:44 +02002931static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002932cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002933 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002934{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002935 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002936 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002937 if (cfqq->last_request_pos) {
2938 if (cfqq->last_request_pos < blk_rq_pos(rq))
2939 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2940 else
2941 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2942 }
Jens Axboe206dc692006-03-28 13:03:44 +02002943
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002944 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002945 if (blk_queue_nonrot(cfqd->queue))
2946 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
2947 else
2948 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02002949}
Jens Axboe22e2c502005-06-27 10:55:12 +02002950
2951/*
2952 * Disable idle window if the process thinks too long or seeks so much that
2953 * it doesn't matter
2954 */
2955static void
2956cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002957 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002958{
Jens Axboe7b679132008-05-30 12:23:07 +02002959 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002960
Jens Axboe08717142008-01-28 11:38:15 +01002961 /*
2962 * Don't idle for async or idle io prio class
2963 */
2964 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002965 return;
2966
Jens Axboec265a7f2008-06-26 13:49:33 +02002967 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002968
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002969 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2970 cfq_mark_cfqq_deep(cfqq);
2971
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02002972 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
2973 enable_idle = 0;
Tejun Heoc5869802011-12-14 00:33:41 +01002974 else if (!atomic_read(&cic->icq.ioc->nr_tasks) ||
2975 !cfqd->cfq_slice_idle ||
2976 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002977 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02002978 else if (sample_valid(cic->ttime.ttime_samples)) {
2979 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002980 enable_idle = 0;
2981 else
2982 enable_idle = 1;
2983 }
2984
Jens Axboe7b679132008-05-30 12:23:07 +02002985 if (old_idle != enable_idle) {
2986 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2987 if (enable_idle)
2988 cfq_mark_cfqq_idle_window(cfqq);
2989 else
2990 cfq_clear_cfqq_idle_window(cfqq);
2991 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002992}
2993
Jens Axboe22e2c502005-06-27 10:55:12 +02002994/*
2995 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2996 * no or if we aren't sure, a 1 will cause a preempt.
2997 */
Jens Axboea6151c32009-10-07 20:02:57 +02002998static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002999cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003000 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003001{
Jens Axboe6d048f52007-04-25 12:44:27 +02003002 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003003
Jens Axboe6d048f52007-04-25 12:44:27 +02003004 cfqq = cfqd->active_queue;
3005 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003006 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003007
Jens Axboe6d048f52007-04-25 12:44:27 +02003008 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003009 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003010
3011 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003012 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003013
Jens Axboe22e2c502005-06-27 10:55:12 +02003014 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003015 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3016 */
3017 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3018 return false;
3019
3020 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003021 * if the new request is sync, but the currently running queue is
3022 * not, let the sync request have priority.
3023 */
Jens Axboe5e705372006-07-13 12:39:25 +02003024 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003025 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003026
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003027 if (new_cfqq->cfqg != cfqq->cfqg)
3028 return false;
3029
3030 if (cfq_slice_used(cfqq))
3031 return true;
3032
3033 /* Allow preemption only if we are idling on sync-noidle tree */
3034 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3035 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3036 new_cfqq->service_tree->count == 2 &&
3037 RB_EMPTY_ROOT(&cfqq->sort_list))
3038 return true;
3039
Jens Axboe374f84a2006-07-23 01:42:19 +02003040 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003041 * So both queues are sync. Let the new request get disk time if
3042 * it's a metadata request and the current queue is doing regular IO.
3043 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003044 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003045 return true;
3046
3047 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003048 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3049 */
3050 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003051 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003052
Shaohua Lid2d59e12010-11-08 15:01:03 +01003053 /* An idle queue should not be idle now for some reason */
3054 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3055 return true;
3056
Jens Axboe1e3335d2007-02-14 19:59:49 +01003057 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003058 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003059
3060 /*
3061 * if this request is as-good as one we would expect from the
3062 * current cfqq, let it preempt
3063 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003064 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003065 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003066
Jens Axboea6151c32009-10-07 20:02:57 +02003067 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003068}
3069
3070/*
3071 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3072 * let it have half of its nominal slice.
3073 */
3074static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3075{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003076 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3077
Jens Axboe7b679132008-05-30 12:23:07 +02003078 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003079 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003080
Jens Axboebf572252006-07-19 20:29:12 +02003081 /*
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003082 * workload type is changed, don't save slice, otherwise preempt
3083 * doesn't happen
3084 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003085 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003086 cfqq->cfqg->saved_workload_slice = 0;
3087
3088 /*
Jens Axboebf572252006-07-19 20:29:12 +02003089 * Put the new queue at the front of the of the current list,
3090 * so we know that it will be selected next.
3091 */
3092 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003093
3094 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003095
Justin TerAvest62a37f62011-03-23 08:25:44 +01003096 cfqq->slice_end = 0;
3097 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003098}
3099
3100/*
Jens Axboe5e705372006-07-13 12:39:25 +02003101 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003102 * something we should do about it
3103 */
3104static void
Jens Axboe5e705372006-07-13 12:39:25 +02003105cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3106 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003107{
Tejun Heoc5869802011-12-14 00:33:41 +01003108 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003109
Aaron Carroll45333d52008-08-26 15:52:36 +02003110 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003111 if (rq->cmd_flags & REQ_PRIO)
3112 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003113
Shaohua Li383cd722011-07-12 14:24:35 +02003114 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003115 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003116 cfq_update_idle_window(cfqd, cfqq, cic);
3117
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003118 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003119
3120 if (cfqq == cfqd->active_queue) {
3121 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003122 * Remember that we saw a request from this process, but
3123 * don't start queuing just yet. Otherwise we risk seeing lots
3124 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003125 * and merging. If the request is already larger than a single
3126 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003127 * merging is already done. Ditto for a busy system that
3128 * has other work pending, don't risk delaying until the
3129 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003130 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003131 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003132 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3133 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003134 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003135 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003136 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003137 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003138 cfq_blkiocg_update_idle_time_stats(
Tejun Heo03814112012-03-05 13:15:14 -08003139 cfqg_to_blkg(cfqq->cfqg));
Vivek Goyalbf7919372009-12-03 12:59:37 -05003140 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003141 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003142 }
Jens Axboe5e705372006-07-13 12:39:25 +02003143 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003144 /*
3145 * not the active queue - expire current slice if it is
3146 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003147 * has some old slice time left and is of higher priority or
3148 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003149 */
3150 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003151 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003152 }
3153}
3154
Jens Axboe165125e2007-07-24 09:28:11 +02003155static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003156{
Jens Axboeb4878f22005-10-20 16:42:29 +02003157 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003158 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003159
Jens Axboe7b679132008-05-30 12:23:07 +02003160 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoc5869802011-12-14 00:33:41 +01003161 cfq_init_prio_data(cfqq, RQ_CIC(rq)->icq.ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Jens Axboe30996f42009-10-05 11:03:39 +02003163 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003164 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003165 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003166 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
3167 cfqg_to_blkg(cfqd->serving_group),
3168 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003169 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Aaron Carroll45333d52008-08-26 15:52:36 +02003172/*
3173 * Update hw_tag based on peak queue depth over 50 samples under
3174 * sufficient load.
3175 */
3176static void cfq_update_hw_tag(struct cfq_data *cfqd)
3177{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003178 struct cfq_queue *cfqq = cfqd->active_queue;
3179
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003180 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3181 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003182
3183 if (cfqd->hw_tag == 1)
3184 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003185
3186 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003187 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003188 return;
3189
Shaohua Li1a1238a2009-10-27 08:46:23 +01003190 /*
3191 * If active queue hasn't enough requests and can idle, cfq might not
3192 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3193 * case
3194 */
3195 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3196 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003197 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003198 return;
3199
Aaron Carroll45333d52008-08-26 15:52:36 +02003200 if (cfqd->hw_tag_samples++ < 50)
3201 return;
3202
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003203 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003204 cfqd->hw_tag = 1;
3205 else
3206 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003207}
3208
Vivek Goyal7667aa02009-12-08 17:52:58 -05003209static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3210{
Tejun Heoc5869802011-12-14 00:33:41 +01003211 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003212
Justin TerAvest02a8f012011-02-09 14:20:03 +01003213 /* If the queue already has requests, don't wait */
3214 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3215 return false;
3216
Vivek Goyal7667aa02009-12-08 17:52:58 -05003217 /* If there are other queues in the group, don't wait */
3218 if (cfqq->cfqg->nr_cfqq > 1)
3219 return false;
3220
Shaohua Li7700fc42011-07-12 14:24:56 +02003221 /* the only queue in the group, but think time is big */
3222 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3223 return false;
3224
Vivek Goyal7667aa02009-12-08 17:52:58 -05003225 if (cfq_slice_used(cfqq))
3226 return true;
3227
3228 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003229 if (cic && sample_valid(cic->ttime.ttime_samples)
3230 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003231 return true;
3232
3233 /*
3234 * If think times is less than a jiffy than ttime_mean=0 and above
3235 * will not be true. It might happen that slice has not expired yet
3236 * but will expire soon (4-5 ns) during select_queue(). To cover the
3237 * case where think time is less than a jiffy, mark the queue wait
3238 * busy if only 1 jiffy is left in the slice.
3239 */
3240 if (cfqq->slice_end - jiffies == 1)
3241 return true;
3242
3243 return false;
3244}
3245
Jens Axboe165125e2007-07-24 09:28:11 +02003246static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Jens Axboe5e705372006-07-13 12:39:25 +02003248 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003249 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003250 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003251 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Jens Axboeb4878f22005-10-20 16:42:29 +02003253 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003254 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3255 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Aaron Carroll45333d52008-08-26 15:52:36 +02003257 cfq_update_hw_tag(cfqd);
3258
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003259 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003260 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003261 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003262 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003263 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003264 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Vivek Goyale98ef892010-06-18 10:39:47 -04003265 rq_start_time_ns(rq), rq_io_start_time_ns(rq),
3266 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003268 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003269
Vivek Goyal365722b2009-10-03 15:21:27 +02003270 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003271 struct cfq_rb_root *service_tree;
3272
Shaohua Li383cd722011-07-12 14:24:35 +02003273 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003274
3275 if (cfq_cfqq_on_rr(cfqq))
3276 service_tree = cfqq->service_tree;
3277 else
3278 service_tree = service_tree_for(cfqq->cfqg,
3279 cfqq_prio(cfqq), cfqq_type(cfqq));
3280 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003281 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3282 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003283 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003284
Shaohua Li7700fc42011-07-12 14:24:56 +02003285#ifdef CONFIG_CFQ_GROUP_IOSCHED
3286 cfqq->cfqg->ttime.last_end_request = now;
3287#endif
3288
Jens Axboecaaa5f92006-06-16 11:23:00 +02003289 /*
3290 * If this is the active queue, check if it needs to be expired,
3291 * or if we want to idle in case it has no pending requests.
3292 */
3293 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003294 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3295
Jens Axboe44f7c162007-01-19 11:51:58 +11003296 if (cfq_cfqq_slice_new(cfqq)) {
3297 cfq_set_prio_slice(cfqd, cfqq);
3298 cfq_clear_cfqq_slice_new(cfqq);
3299 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003300
3301 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003302 * Should we wait for next request to come in before we expire
3303 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003304 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003305 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003306 unsigned long extend_sl = cfqd->cfq_slice_idle;
3307 if (!cfqd->cfq_slice_idle)
3308 extend_sl = cfqd->cfq_group_idle;
3309 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003310 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003311 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003312 }
3313
Jens Axboea36e71f2009-04-15 12:15:11 +02003314 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003315 * Idling is not enabled on:
3316 * - expired queues
3317 * - idle-priority queues
3318 * - async queues
3319 * - queues with still some requests queued
3320 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003321 */
Jens Axboe08717142008-01-28 11:38:15 +01003322 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003323 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003324 else if (sync && cfqq_empty &&
3325 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003326 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003327 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003328 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003329
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003330 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003331 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Jens Axboe89850f72006-07-22 16:48:31 +02003334static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003335{
Jens Axboe1b379d82009-08-11 08:26:11 +02003336 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003337 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003338 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003339 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003340
3341 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003342}
3343
Jens Axboe165125e2007-07-24 09:28:11 +02003344static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003345{
3346 struct cfq_data *cfqd = q->elevator->elevator_data;
3347 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003348 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003349 struct cfq_queue *cfqq;
3350
3351 /*
3352 * don't force setup of a queue from here, as a call to may_queue
3353 * does not necessarily imply that a request actually will be queued.
3354 * so just lookup a possibly existing queue, or return 'may queue'
3355 * if that fails
3356 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003357 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003358 if (!cic)
3359 return ELV_MQUEUE_MAY;
3360
Jens Axboeb0b78f82009-04-08 10:56:08 +02003361 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003362 if (cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003363 cfq_init_prio_data(cfqq, cic->icq.ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003364
Jens Axboe89850f72006-07-22 16:48:31 +02003365 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003366 }
3367
3368 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/*
3372 * queue lock held here
3373 */
Jens Axboebb37b942006-12-01 10:42:33 +01003374static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
Jens Axboe5e705372006-07-13 12:39:25 +02003376 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Jens Axboe5e705372006-07-13 12:39:25 +02003378 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003379 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Jens Axboe22e2c502005-06-27 10:55:12 +02003381 BUG_ON(!cfqq->allocated[rw]);
3382 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003384 /* Put down rq reference on cfqg */
3385 cfq_put_cfqg(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003386 rq->elv.priv[0] = NULL;
3387 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 cfq_put_queue(cfqq);
3390 }
3391}
3392
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003393static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003394cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003395 struct cfq_queue *cfqq)
3396{
3397 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3398 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003399 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003400 cfq_put_queue(cfqq);
3401 return cic_to_cfqq(cic, 1);
3402}
3403
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003404/*
3405 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3406 * was the last process referring to said cfqq.
3407 */
3408static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003409split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003410{
3411 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003412 cfqq->pid = current->pid;
3413 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003414 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003415 return cfqq;
3416 }
3417
3418 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003419
3420 cfq_put_cooperator(cfqq);
3421
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003422 cfq_put_queue(cfqq);
3423 return NULL;
3424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003426 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003428static int
Jens Axboe165125e2007-07-24 09:28:11 +02003429cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003432 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003434 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003435 struct cfq_queue *cfqq;
Tejun Heod705ae62012-02-15 09:45:49 +01003436 unsigned int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
3438 might_sleep_if(gfp_mask & __GFP_WAIT);
3439
Tejun Heo216284c2011-12-14 00:33:38 +01003440 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003441
3442 /* handle changed notifications */
Tejun Heod705ae62012-02-15 09:45:49 +01003443 changed = icq_get_changed(&cic->icq);
3444 if (unlikely(changed & ICQ_IOPRIO_CHANGED))
3445 changed_ioprio(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003446#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heod705ae62012-02-15 09:45:49 +01003447 if (unlikely(changed & ICQ_CGROUP_CHANGED))
3448 changed_cgroup(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003449#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003451new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003452 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003453 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003454 cfqq = cfq_get_queue(cfqd, is_sync, cic->icq.ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003455 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003456 } else {
3457 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003458 * If the queue was seeky for too long, break it apart.
3459 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003460 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003461 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3462 cfqq = split_cfqq(cic, cfqq);
3463 if (!cfqq)
3464 goto new_queue;
3465 }
3466
3467 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003468 * Check to see if this queue is scheduled to merge with
3469 * another, closely cooperating queue. The merging of
3470 * queues happens here as it must be done in process context.
3471 * The reference on new_cfqq was taken in merge_cfqqs.
3472 */
3473 if (cfqq->new_cfqq)
3474 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003478
Jens Axboe6fae9c22011-03-01 15:04:39 -05003479 cfqq->ref++;
Tejun Heoa612fdd2011-12-14 00:33:41 +01003480 rq->elv.priv[0] = cfqq;
3481 rq->elv.priv[1] = cfq_ref_get_cfqg(cfqq->cfqg);
Tejun Heo216284c2011-12-14 00:33:38 +01003482 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
David Howells65f27f32006-11-22 14:55:48 +00003486static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003487{
David Howells65f27f32006-11-22 14:55:48 +00003488 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003489 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003490 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003491
Jens Axboe40bb54d2009-04-15 12:11:10 +02003492 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003493 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003494 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003495}
3496
3497/*
3498 * Timer running if the active_queue is currently idling inside its time slice
3499 */
3500static void cfq_idle_slice_timer(unsigned long data)
3501{
3502 struct cfq_data *cfqd = (struct cfq_data *) data;
3503 struct cfq_queue *cfqq;
3504 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003505 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003506
Jens Axboe7b679132008-05-30 12:23:07 +02003507 cfq_log(cfqd, "idle timer fired");
3508
Jens Axboe22e2c502005-06-27 10:55:12 +02003509 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3510
Jens Axboefe094d92008-01-31 13:08:54 +01003511 cfqq = cfqd->active_queue;
3512 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003513 timed_out = 0;
3514
Jens Axboe22e2c502005-06-27 10:55:12 +02003515 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003516 * We saw a request before the queue expired, let it through
3517 */
3518 if (cfq_cfqq_must_dispatch(cfqq))
3519 goto out_kick;
3520
3521 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003522 * expired
3523 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003524 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003525 goto expire;
3526
3527 /*
3528 * only expire and reinvoke request handler, if there are
3529 * other queues with pending requests
3530 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003531 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003532 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003533
3534 /*
3535 * not expired and it has a request pending, let it dispatch
3536 */
Jens Axboe75e50982009-04-07 08:56:14 +02003537 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003538 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003539
3540 /*
3541 * Queue depth flag is reset only when the idle didn't succeed
3542 */
3543 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003544 }
3545expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003546 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003547out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003548 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003549out_cont:
3550 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3551}
3552
Jens Axboe3b181522005-06-27 10:56:24 +02003553static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3554{
3555 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003556 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003557}
Jens Axboe22e2c502005-06-27 10:55:12 +02003558
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003559static void cfq_put_async_queues(struct cfq_data *cfqd)
3560{
3561 int i;
3562
3563 for (i = 0; i < IOPRIO_BE_NR; i++) {
3564 if (cfqd->async_cfqq[0][i])
3565 cfq_put_queue(cfqd->async_cfqq[0][i]);
3566 if (cfqd->async_cfqq[1][i])
3567 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003568 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003569
3570 if (cfqd->async_idle_cfqq)
3571 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003572}
3573
Jens Axboeb374d182008-10-31 10:05:07 +01003574static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Jens Axboe22e2c502005-06-27 10:55:12 +02003576 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003577 struct request_queue *q = cfqd->queue;
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003578 bool wait = false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003579
Jens Axboe3b181522005-06-27 10:56:24 +02003580 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003581
Al Virod9ff4182006-03-18 13:51:22 -05003582 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003583
Al Virod9ff4182006-03-18 13:51:22 -05003584 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003585 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003586
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003587 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003588 cfq_release_cfq_groups(cfqd);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003589
3590 /*
3591 * If there are groups which we could not unlink from blkcg list,
3592 * wait for a rcu period for them to be freed.
3593 */
3594 if (cfqd->nr_blkcg_linked_grps)
3595 wait = true;
Jens Axboe15c31be2007-07-10 13:43:25 +02003596
Al Virod9ff4182006-03-18 13:51:22 -05003597 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003598
3599 cfq_shutdown_timer_wq(cfqd);
3600
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003601 /*
3602 * Wait for cfqg->blkg->key accessors to exit their grace periods.
3603 * Do this wait only if there are other unlinked groups out
3604 * there. This can happen if cgroup deletion path claimed the
3605 * responsibility of cleaning up a group before queue cleanup code
3606 * get to the group.
3607 *
3608 * Do not call synchronize_rcu() unconditionally as there are drivers
3609 * which create/delete request queue hundreds of times during scan/boot
3610 * and synchronize_rcu() can take significant time and slow down boot.
3611 */
3612 if (wait)
3613 synchronize_rcu();
Vivek Goyal2abae552011-05-23 10:02:19 +02003614
Tejun Heof51b8022012-03-05 13:15:05 -08003615#ifndef CONFIG_CFQ_GROUP_IOSCHED
3616 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003617#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003618 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
Tejun Heob2fab5a2012-03-05 13:14:57 -08003621static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
3623 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003624 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003625 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Christoph Lameter94f60302007-07-17 04:03:29 -07003627 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003628 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003629 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003630
Tejun Heof51b8022012-03-05 13:15:05 -08003631 cfqd->queue = q;
3632 q->elevator->elevator_data = cfqd;
3633
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003634 /* Init root service tree */
3635 cfqd->grp_service_tree = CFQ_RB_ROOT;
3636
Tejun Heof51b8022012-03-05 13:15:05 -08003637 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05003638#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08003639 rcu_read_lock();
3640 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003641
Tejun Heocd1604f2012-03-05 13:15:06 -08003642 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_PROP,
3643 true);
3644 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08003645 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08003646
3647 spin_unlock_irq(q->queue_lock);
3648 rcu_read_unlock();
3649#else
3650 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
3651 GFP_KERNEL, cfqd->queue->node);
3652 if (cfqd->root_group)
3653 cfq_init_cfqg_base(cfqd->root_group);
3654#endif
3655 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003656 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08003657 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003658 }
3659
Tejun Heof51b8022012-03-05 13:15:05 -08003660 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003661
Jens Axboe26a2ac02009-04-23 12:13:27 +02003662 /*
3663 * Not strictly needed (since RB_ROOT just clears the node and we
3664 * zeroed cfqd on alloc), but better be safe in case someone decides
3665 * to add magic to the rb code
3666 */
3667 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3668 cfqd->prio_trees[i] = RB_ROOT;
3669
Jens Axboe6118b702009-06-30 09:34:12 +02003670 /*
3671 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3672 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08003673 * will not attempt to free it. oom_cfqq is linked to root_group
3674 * but shouldn't hold a reference as it'll never be unlinked. Lose
3675 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02003676 */
3677 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01003678 cfqd->oom_cfqq.ref++;
Tejun Heof51b8022012-03-05 13:15:05 -08003679 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
3680 cfq_put_cfqg(cfqd->root_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Jens Axboe22e2c502005-06-27 10:55:12 +02003682 init_timer(&cfqd->idle_slice_timer);
3683 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3684 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3685
Jens Axboe23e018a2009-10-05 08:52:35 +02003686 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003689 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3690 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 cfqd->cfq_back_max = cfq_back_max;
3692 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003693 cfqd->cfq_slice[0] = cfq_slice_async;
3694 cfqd->cfq_slice[1] = cfq_slice_sync;
3695 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3696 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003697 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003698 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003699 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01003700 /*
3701 * we optimistically start assuming sync ops weren't delayed in last
3702 * second, in order to have larger depth for async operations.
3703 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003704 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08003705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/*
3709 * sysfs parts below -->
3710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711static ssize_t
3712cfq_var_show(unsigned int var, char *page)
3713{
3714 return sprintf(page, "%d\n", var);
3715}
3716
3717static ssize_t
3718cfq_var_store(unsigned int *var, const char *page, size_t count)
3719{
3720 char *p = (char *) page;
3721
3722 *var = simple_strtoul(p, &p, 10);
3723 return count;
3724}
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003727static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003729 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 unsigned int __data = __VAR; \
3731 if (__CONV) \
3732 __data = jiffies_to_msecs(__data); \
3733 return cfq_var_show(__data, (page)); \
3734}
3735SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003736SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3737SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003738SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3739SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003740SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003741SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003742SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3743SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3744SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003745SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746#undef SHOW_FUNCTION
3747
3748#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003749static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003751 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 unsigned int __data; \
3753 int ret = cfq_var_store(&__data, (page), count); \
3754 if (__data < (MIN)) \
3755 __data = (MIN); \
3756 else if (__data > (MAX)) \
3757 __data = (MAX); \
3758 if (__CONV) \
3759 *(__PTR) = msecs_to_jiffies(__data); \
3760 else \
3761 *(__PTR) = __data; \
3762 return ret; \
3763}
3764STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003765STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3766 UINT_MAX, 1);
3767STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3768 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003769STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003770STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3771 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003772STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003773STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003774STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3775STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003776STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3777 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003778STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779#undef STORE_FUNCTION
3780
Al Viroe572ec72006-03-18 22:27:18 -05003781#define CFQ_ATTR(name) \
3782 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003783
Al Viroe572ec72006-03-18 22:27:18 -05003784static struct elv_fs_entry cfq_attrs[] = {
3785 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003786 CFQ_ATTR(fifo_expire_sync),
3787 CFQ_ATTR(fifo_expire_async),
3788 CFQ_ATTR(back_seek_max),
3789 CFQ_ATTR(back_seek_penalty),
3790 CFQ_ATTR(slice_sync),
3791 CFQ_ATTR(slice_async),
3792 CFQ_ATTR(slice_async_rq),
3793 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003794 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003795 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003796 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797};
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799static struct elevator_type iosched_cfq = {
3800 .ops = {
3801 .elevator_merge_fn = cfq_merge,
3802 .elevator_merged_fn = cfq_merged_request,
3803 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003804 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07003805 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02003806 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003808 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003811 .elevator_former_req_fn = elv_rb_former_request,
3812 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01003813 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01003814 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 .elevator_set_req_fn = cfq_set_request,
3816 .elevator_put_req_fn = cfq_put_request,
3817 .elevator_may_queue_fn = cfq_may_queue,
3818 .elevator_init_fn = cfq_init_queue,
3819 .elevator_exit_fn = cfq_exit_queue,
3820 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01003821 .icq_size = sizeof(struct cfq_io_cq),
3822 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05003823 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01003824 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 .elevator_owner = THIS_MODULE,
3826};
3827
Vivek Goyal3e252062009-12-04 10:36:42 -05003828#ifdef CONFIG_CFQ_GROUP_IOSCHED
3829static struct blkio_policy_type blkio_policy_cfq = {
3830 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08003831 .blkio_init_group_fn = cfq_init_blkio_group,
Tejun Heocd1604f2012-03-05 13:15:06 -08003832 .blkio_link_group_fn = cfq_link_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05003833 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
Tejun Heo72e06c22012-03-05 13:15:00 -08003834 .blkio_clear_queue_fn = cfq_clear_queue,
Vivek Goyal3e252062009-12-04 10:36:42 -05003835 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3836 },
Vivek Goyal062a6442010-09-15 17:06:33 -04003837 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08003838 .pdata_size = sizeof(struct cfq_group),
Vivek Goyal3e252062009-12-04 10:36:42 -05003839};
Vivek Goyal3e252062009-12-04 10:36:42 -05003840#endif
3841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842static int __init cfq_init(void)
3843{
Tejun Heo3d3c2372011-12-14 00:33:42 +01003844 int ret;
3845
Jens Axboe22e2c502005-06-27 10:55:12 +02003846 /*
3847 * could be 0 on HZ < 1000 setups
3848 */
3849 if (!cfq_slice_async)
3850 cfq_slice_async = 1;
3851 if (!cfq_slice_idle)
3852 cfq_slice_idle = 1;
3853
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003854#ifdef CONFIG_CFQ_GROUP_IOSCHED
3855 if (!cfq_group_idle)
3856 cfq_group_idle = 1;
3857#else
3858 cfq_group_idle = 0;
3859#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01003860 cfq_pool = KMEM_CACHE(cfq_queue, 0);
3861 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return -ENOMEM;
3863
Tejun Heo3d3c2372011-12-14 00:33:42 +01003864 ret = elv_register(&iosched_cfq);
3865 if (ret) {
3866 kmem_cache_destroy(cfq_pool);
3867 return ret;
3868 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01003869
Tejun Heob95ada52012-03-05 13:14:55 -08003870#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003871 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003872#endif
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
3876static void __exit cfq_exit(void)
3877{
Tejun Heob95ada52012-03-05 13:14:55 -08003878#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003879 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01003882 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
3885module_init(cfq_init);
3886module_exit(cfq_exit);
3887
3888MODULE_AUTHOR("Jens Axboe");
3889MODULE_LICENSE("GPL");
3890MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");