blob: effd894895061d8a2489bbb3901000f8904e923d [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"
Tejun Heo629ed0b2012-04-01 14:38:44 -070018#include "blk-cgroup.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;
Tejun Heo4eef3042012-03-05 13:15:18 -0800211
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200212 /* number of requests that are on the dispatch list or inside driver */
213 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200214 struct cfq_ttime ttime;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500215};
216
Tejun Heoc5869802011-12-14 00:33:41 +0100217struct cfq_io_cq {
218 struct io_cq icq; /* must be the first member */
219 struct cfq_queue *cfqq[2];
220 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700221 int ioprio; /* the current ioprio */
222#ifdef CONFIG_CFQ_GROUP_IOSCHED
223 uint64_t blkcg_id; /* the current blkcg ID */
224#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100225};
226
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500227/*
228 * Per block device queue structure
229 */
230struct cfq_data {
231 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500232 /* Root service tree for cfq_groups */
233 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800234 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500235
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100236 /*
237 * The priority currently being served
238 */
239 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100240 enum wl_type_t serving_type;
241 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500242 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200243
244 /*
245 * Each priority tree is sorted by next_request position. These
246 * trees are used when determining if two or more queues are
247 * interleaving requests (see cfq_close_cooperator).
248 */
249 struct rb_root prio_trees[CFQ_PRIO_LISTS];
250
Jens Axboe22e2c502005-06-27 10:55:12 +0200251 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100252 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200253
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100254 int rq_in_driver;
255 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200256
257 /*
258 * queue-depth detection
259 */
260 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200261 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100262 /*
263 * hw_tag can be
264 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
265 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
266 * 0 => no NCQ
267 */
268 int hw_tag_est_depth;
269 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200270
271 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200272 * idle window management
273 */
274 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200275 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200276
277 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100278 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200279
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200280 /*
281 * async queue for each priority case
282 */
283 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
284 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200285
Jens Axboe6d048f52007-04-25 12:44:27 +0200286 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /*
289 * tunables, see top of file
290 */
291 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200292 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned int cfq_back_penalty;
294 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200295 unsigned int cfq_slice[2];
296 unsigned int cfq_slice_async_rq;
297 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200298 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200299 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500300
Jens Axboe6118b702009-06-30 09:34:12 +0200301 /*
302 * Fallback dummy cfqq for extreme OOM conditions
303 */
304 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200305
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100306 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Vivek Goyal25fb5162009-12-03 12:59:46 -0500309static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
310
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500311static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
312 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500313 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100314{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500315 if (!cfqg)
316 return NULL;
317
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100318 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500319 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100320
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500321 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100322}
323
Jens Axboe3b181522005-06-27 10:56:24 +0200324enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100325 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
326 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200327 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100328 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100329 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
330 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
331 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100332 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200333 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400334 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100335 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100336 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500337 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200338};
339
340#define CFQ_CFQQ_FNS(name) \
341static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
342{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100343 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200344} \
345static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
346{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100347 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200348} \
349static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
350{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100351 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200352}
353
354CFQ_CFQQ_FNS(on_rr);
355CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200356CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200357CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200358CFQ_CFQQ_FNS(fifo_expire);
359CFQ_CFQQ_FNS(idle_window);
360CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100361CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200362CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200363CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100364CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100365CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500366CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200367#undef CFQ_CFQQ_FNS
368
Tejun Heo629ed0b2012-04-01 14:38:44 -0700369#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700370
Tejun Heo629ed0b2012-04-01 14:38:44 -0700371/* blkg state flags */
372enum blkg_state_flags {
373 BLKG_waiting = 0,
374 BLKG_idling,
375 BLKG_empty,
376};
377
378#define BLKG_FLAG_FNS(name) \
379static inline void blkio_mark_blkg_##name( \
380 struct blkio_group_stats *stats) \
381{ \
382 stats->flags |= (1 << BLKG_##name); \
383} \
384static inline void blkio_clear_blkg_##name( \
385 struct blkio_group_stats *stats) \
386{ \
387 stats->flags &= ~(1 << BLKG_##name); \
388} \
389static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \
390{ \
391 return (stats->flags & (1 << BLKG_##name)) != 0; \
392} \
393
394BLKG_FLAG_FNS(waiting)
395BLKG_FLAG_FNS(idling)
396BLKG_FLAG_FNS(empty)
397#undef BLKG_FLAG_FNS
398
399/* This should be called with the queue_lock held. */
400static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
401{
402 unsigned long long now;
403
404 if (!blkio_blkg_waiting(stats))
405 return;
406
407 now = sched_clock();
408 if (time_after64(now, stats->start_group_wait_time))
409 blkg_stat_add(&stats->group_wait_time,
410 now - stats->start_group_wait_time);
411 blkio_clear_blkg_waiting(stats);
412}
413
414/* This should be called with the queue_lock held. */
415static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
416 struct blkio_policy_type *pol,
417 struct blkio_group *curr_blkg)
418{
419 struct blkg_policy_data *pd = blkg->pd[pol->plid];
420
421 if (blkio_blkg_waiting(&pd->stats))
422 return;
423 if (blkg == curr_blkg)
424 return;
425 pd->stats.start_group_wait_time = sched_clock();
426 blkio_mark_blkg_waiting(&pd->stats);
427}
428
429/* This should be called with the queue_lock held. */
430static void blkio_end_empty_time(struct blkio_group_stats *stats)
431{
432 unsigned long long now;
433
434 if (!blkio_blkg_empty(stats))
435 return;
436
437 now = sched_clock();
438 if (time_after64(now, stats->start_empty_time))
439 blkg_stat_add(&stats->empty_time,
440 now - stats->start_empty_time);
441 blkio_clear_blkg_empty(stats);
442}
443
444static void cfq_blkiocg_update_dequeue_stats(struct blkio_group *blkg,
445 struct blkio_policy_type *pol,
446 unsigned long dequeue)
447{
448 struct blkg_policy_data *pd = blkg->pd[pol->plid];
449
450 lockdep_assert_held(blkg->q->queue_lock);
451
452 blkg_stat_add(&pd->stats.dequeue, dequeue);
453}
454
455static void cfq_blkiocg_set_start_empty_time(struct blkio_group *blkg,
456 struct blkio_policy_type *pol)
457{
458 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
459
460 lockdep_assert_held(blkg->q->queue_lock);
461
462 if (blkg_rwstat_sum(&stats->queued))
463 return;
464
465 /*
466 * group is already marked empty. This can happen if cfqq got new
467 * request in parent group and moved to this group while being added
468 * to service tree. Just ignore the event and move on.
469 */
470 if (blkio_blkg_empty(stats))
471 return;
472
473 stats->start_empty_time = sched_clock();
474 blkio_mark_blkg_empty(stats);
475}
476
477static void cfq_blkiocg_update_idle_time_stats(struct blkio_group *blkg,
478 struct blkio_policy_type *pol)
479{
480 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
481
482 lockdep_assert_held(blkg->q->queue_lock);
483
484 if (blkio_blkg_idling(stats)) {
485 unsigned long long now = sched_clock();
486
487 if (time_after64(now, stats->start_idle_time))
488 blkg_stat_add(&stats->idle_time,
489 now - stats->start_idle_time);
490 blkio_clear_blkg_idling(stats);
491 }
492}
493
494static void cfq_blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
495 struct blkio_policy_type *pol)
496{
497 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
498
499 lockdep_assert_held(blkg->q->queue_lock);
500 BUG_ON(blkio_blkg_idling(stats));
501
502 stats->start_idle_time = sched_clock();
503 blkio_mark_blkg_idling(stats);
504}
505
506static void cfq_blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
507 struct blkio_policy_type *pol)
508{
509 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
510
511 lockdep_assert_held(blkg->q->queue_lock);
512
513 blkg_stat_add(&stats->avg_queue_size_sum,
514 blkg_rwstat_sum(&stats->queued));
515 blkg_stat_add(&stats->avg_queue_size_samples, 1);
516 blkio_update_group_wait_time(stats);
517}
518
519#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
520
521static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
522 struct blkio_policy_type *pol,
523 struct blkio_group *curr_blkg) { }
524static void blkio_end_empty_time(struct blkio_group_stats *stats) { }
525static void cfq_blkiocg_update_dequeue_stats(struct blkio_group *blkg,
526 struct blkio_policy_type *pol,
527 unsigned long dequeue) { }
528static void cfq_blkiocg_set_start_empty_time(struct blkio_group *blkg,
529 struct blkio_policy_type *pol) { }
530static void cfq_blkiocg_update_idle_time_stats(struct blkio_group *blkg,
531 struct blkio_policy_type *pol) { }
532static void cfq_blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
533 struct blkio_policy_type *pol) { }
534static void cfq_blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
535 struct blkio_policy_type *pol) { }
536
537#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
538
539#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700540
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100541static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
542{
543 return blkg_to_pdata(blkg, &blkio_policy_cfq);
544}
545
546static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
547{
Tejun Heoaaec55a2012-04-01 14:38:42 -0700548 return pdata_to_blkg(cfqg);
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100549}
550
551static inline void cfqg_get(struct cfq_group *cfqg)
552{
553 return blkg_get(cfqg_to_blkg(cfqg));
554}
555
556static inline void cfqg_put(struct cfq_group *cfqg)
557{
558 return blkg_put(cfqg_to_blkg(cfqg));
559}
560
Vivek Goyal2868ef72009-12-03 12:59:48 -0500561#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
562 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
563 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
Tejun Heo03814112012-03-05 13:15:14 -0800564 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500565
566#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
567 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800568 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
Vivek Goyal2868ef72009-12-03 12:59:48 -0500569
Tejun Heo2ce4d502012-04-01 14:38:43 -0700570static inline void cfq_blkiocg_update_io_add_stats(struct blkio_group *blkg,
571 struct blkio_policy_type *pol,
572 struct blkio_group *curr_blkg,
573 bool direction, bool sync)
574{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700575 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
576 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700577
Tejun Heo629ed0b2012-04-01 14:38:44 -0700578 lockdep_assert_held(blkg->q->queue_lock);
579
580 blkg_rwstat_add(&stats->queued, rw, 1);
581 blkio_end_empty_time(stats);
582 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700583}
584
585static inline void cfq_blkiocg_update_timeslice_used(struct blkio_group *blkg,
586 struct blkio_policy_type *pol, unsigned long time,
587 unsigned long unaccounted_time)
588{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700589 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heo2ce4d502012-04-01 14:38:43 -0700590
Tejun Heo629ed0b2012-04-01 14:38:44 -0700591 lockdep_assert_held(blkg->q->queue_lock);
592
593 blkg_stat_add(&stats->time, time);
594#ifdef CONFIG_DEBUG_BLK_CGROUP
595 blkg_stat_add(&stats->unaccounted_time, unaccounted_time);
596#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700597}
598
599static inline void cfq_blkiocg_update_io_remove_stats(struct blkio_group *blkg,
600 struct blkio_policy_type *pol, bool direction,
601 bool sync)
602{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700603 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
604 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
605
606 lockdep_assert_held(blkg->q->queue_lock);
607
608 blkg_rwstat_add(&stats->queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700609}
610
611static inline void cfq_blkiocg_update_io_merged_stats(struct blkio_group *blkg,
612 struct blkio_policy_type *pol, bool direction,
613 bool sync)
614{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700615 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
616 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700617
Tejun Heo629ed0b2012-04-01 14:38:44 -0700618 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700619
Tejun Heo629ed0b2012-04-01 14:38:44 -0700620 blkg_rwstat_add(&stats->merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700621}
622
623static inline void cfq_blkiocg_update_dispatch_stats(struct blkio_group *blkg,
624 struct blkio_policy_type *pol, uint64_t bytes,
625 bool direction, bool sync)
626{
Tejun Heo41b38b62012-04-01 14:38:44 -0700627 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700628 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700629
Tejun Heo41b38b62012-04-01 14:38:44 -0700630 blkg_stat_add(&stats->sectors, bytes >> 9);
631 blkg_rwstat_add(&stats->serviced, rw, 1);
632 blkg_rwstat_add(&stats->service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700633}
634
635static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
636 struct blkio_policy_type *pol, uint64_t start_time,
637 uint64_t io_start_time, bool direction, bool sync)
638{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700639 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
640 unsigned long long now = sched_clock();
641 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
642
643 lockdep_assert_held(blkg->q->queue_lock);
644
645 if (time_after64(now, io_start_time))
646 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
647 if (time_after64(io_start_time, start_time))
648 blkg_rwstat_add(&stats->wait_time, rw,
649 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700650}
651
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100652#else /* CONFIG_CFQ_GROUP_IOSCHED */
653
654static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg) { return NULL; }
655static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg) { return NULL; }
656static inline void cfqg_get(struct cfq_group *cfqg) { }
657static inline void cfqg_put(struct cfq_group *cfqg) { }
658
Jens Axboe7b679132008-05-30 12:23:07 +0200659#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
660 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200661#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100662
Tejun Heo2ce4d502012-04-01 14:38:43 -0700663static inline void cfq_blkiocg_update_io_add_stats(struct blkio_group *blkg,
664 struct blkio_policy_type *pol,
665 struct blkio_group *curr_blkg, bool direction,
666 bool sync) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700667static inline void cfq_blkiocg_update_timeslice_used(struct blkio_group *blkg,
668 struct blkio_policy_type *pol, unsigned long time,
669 unsigned long unaccounted_time) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700670static inline void cfq_blkiocg_update_io_remove_stats(struct blkio_group *blkg,
671 struct blkio_policy_type *pol, bool direction,
672 bool sync) { }
673static inline void cfq_blkiocg_update_io_merged_stats(struct blkio_group *blkg,
674 struct blkio_policy_type *pol, bool direction,
675 bool sync) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700676static inline void cfq_blkiocg_update_dispatch_stats(struct blkio_group *blkg,
677 struct blkio_policy_type *pol, uint64_t bytes,
678 bool direction, bool sync) { }
679static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
680 struct blkio_policy_type *pol, uint64_t start_time,
681 uint64_t io_start_time, bool direction, bool sync) { }
682
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100683#endif /* CONFIG_CFQ_GROUP_IOSCHED */
684
Jens Axboe7b679132008-05-30 12:23:07 +0200685#define cfq_log(cfqd, fmt, args...) \
686 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
687
Vivek Goyal615f0252009-12-03 12:59:39 -0500688/* Traverses through cfq group service trees */
689#define for_each_cfqg_st(cfqg, i, j, st) \
690 for (i = 0; i <= IDLE_WORKLOAD; i++) \
691 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
692 : &cfqg->service_tree_idle; \
693 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
694 (i == IDLE_WORKLOAD && j == 0); \
695 j++, st = i < IDLE_WORKLOAD ? \
696 &cfqg->service_trees[i][j]: NULL) \
697
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200698static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
699 struct cfq_ttime *ttime, bool group_idle)
700{
701 unsigned long slice;
702 if (!sample_valid(ttime->ttime_samples))
703 return false;
704 if (group_idle)
705 slice = cfqd->cfq_group_idle;
706 else
707 slice = cfqd->cfq_slice_idle;
708 return ttime->ttime_mean > slice;
709}
Vivek Goyal615f0252009-12-03 12:59:39 -0500710
Vivek Goyal02b35082010-08-23 12:23:53 +0200711static inline bool iops_mode(struct cfq_data *cfqd)
712{
713 /*
714 * If we are not idling on queues and it is a NCQ drive, parallel
715 * execution of requests is on and measuring time is not possible
716 * in most of the cases until and unless we drive shallower queue
717 * depths and that becomes a performance bottleneck. In such cases
718 * switch to start providing fairness in terms of number of IOs.
719 */
720 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
721 return true;
722 else
723 return false;
724}
725
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100726static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
727{
728 if (cfq_class_idle(cfqq))
729 return IDLE_WORKLOAD;
730 if (cfq_class_rt(cfqq))
731 return RT_WORKLOAD;
732 return BE_WORKLOAD;
733}
734
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100735
736static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
737{
738 if (!cfq_cfqq_sync(cfqq))
739 return ASYNC_WORKLOAD;
740 if (!cfq_cfqq_idle_window(cfqq))
741 return SYNC_NOIDLE_WORKLOAD;
742 return SYNC_WORKLOAD;
743}
744
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500745static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
746 struct cfq_data *cfqd,
747 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100748{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500749 if (wl == IDLE_WORKLOAD)
750 return cfqg->service_tree_idle.count;
751
752 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
753 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
754 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100755}
756
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500757static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
758 struct cfq_group *cfqg)
759{
760 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
761 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
762}
763
Jens Axboe165125e2007-07-24 09:28:11 +0200764static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800765static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700766 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800767 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200768
Tejun Heoc5869802011-12-14 00:33:41 +0100769static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
770{
771 /* cic->icq is the first member, %NULL will convert to %NULL */
772 return container_of(icq, struct cfq_io_cq, icq);
773}
774
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100775static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
776 struct io_context *ioc)
777{
778 if (ioc)
779 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
780 return NULL;
781}
782
Tejun Heoc5869802011-12-14 00:33:41 +0100783static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200784{
Jens Axboea6151c32009-10-07 20:02:57 +0200785 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200786}
787
Tejun Heoc5869802011-12-14 00:33:41 +0100788static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
789 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200790{
Jens Axboea6151c32009-10-07 20:02:57 +0200791 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200792}
793
Tejun Heoc5869802011-12-14 00:33:41 +0100794static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400795{
Tejun Heoc5869802011-12-14 00:33:41 +0100796 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400797}
798
Vasily Tarasov91fac312007-04-25 12:29:51 +0200799/*
800 * We regard a request as SYNC, if it's either a read or has the SYNC bit
801 * set (in which case it could also be direct WRITE).
802 */
Jens Axboea6151c32009-10-07 20:02:57 +0200803static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200804{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200805 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200806}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700809 * scheduler run of queue, if there are requests pending and no one in the
810 * driver that will restart queueing
811 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200812static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700813{
Jens Axboe7b679132008-05-30 12:23:07 +0200814 if (cfqd->busy_queues) {
815 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200816 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200817 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100821 * Scale schedule slice based on io priority. Use the sync time slice only
822 * if a queue is marked sync and has sync io queued. A sync queue with async
823 * io only, should not get full sync slice length.
824 */
Jens Axboea6151c32009-10-07 20:02:57 +0200825static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200826 unsigned short prio)
827{
828 const int base_slice = cfqd->cfq_slice[sync];
829
830 WARN_ON(prio >= IOPRIO_BE_NR);
831
832 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
833}
834
Jens Axboe44f7c162007-01-19 11:51:58 +1100835static inline int
836cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
837{
Jens Axboed9e76202007-04-20 14:27:50 +0200838 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100839}
840
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500841static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
842{
843 u64 d = delta << CFQ_SERVICE_SHIFT;
844
845 d = d * BLKIO_WEIGHT_DEFAULT;
846 do_div(d, cfqg->weight);
847 return d;
848}
849
850static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
851{
852 s64 delta = (s64)(vdisktime - min_vdisktime);
853 if (delta > 0)
854 min_vdisktime = vdisktime;
855
856 return min_vdisktime;
857}
858
859static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
860{
861 s64 delta = (s64)(vdisktime - min_vdisktime);
862 if (delta < 0)
863 min_vdisktime = vdisktime;
864
865 return min_vdisktime;
866}
867
868static void update_min_vdisktime(struct cfq_rb_root *st)
869{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500870 struct cfq_group *cfqg;
871
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500872 if (st->left) {
873 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100874 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
875 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500876 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500877}
878
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100879/*
880 * get averaged number of queues of RT/BE priority.
881 * average is updated, with a formula that gives more weight to higher numbers,
882 * to quickly follows sudden increases and decrease slowly
883 */
884
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500885static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
886 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100887{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100888 unsigned min_q, max_q;
889 unsigned mult = cfq_hist_divisor - 1;
890 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500891 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100892
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500893 min_q = min(cfqg->busy_queues_avg[rt], busy);
894 max_q = max(cfqg->busy_queues_avg[rt], busy);
895 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100896 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500897 return cfqg->busy_queues_avg[rt];
898}
899
900static inline unsigned
901cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
902{
903 struct cfq_rb_root *st = &cfqd->grp_service_tree;
904
905 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100906}
907
Shaohua Lic553f8e2011-01-14 08:41:03 +0100908static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700909cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100910{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100911 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
912 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500913 /*
914 * interested queues (we consider only the ones with the same
915 * priority class in the cfq group)
916 */
917 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
918 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100919 unsigned sync_slice = cfqd->cfq_slice[1];
920 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500921 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
922
923 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100924 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
925 /* scale low_slice according to IO priority
926 * and sync vs async */
927 unsigned low_slice =
928 min(slice, base_low_slice * slice / sync_slice);
929 /* the adapted slice value is scaled to fit all iqs
930 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500931 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100932 low_slice);
933 }
934 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100935 return slice;
936}
937
938static inline void
939cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
940{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700941 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100942
Vivek Goyaldae739e2009-12-03 12:59:45 -0500943 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100944 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500945 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200946 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100947}
948
949/*
950 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
951 * isn't valid until the first request from the dispatch is activated
952 * and the slice time set.
953 */
Jens Axboea6151c32009-10-07 20:02:57 +0200954static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100955{
956 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100957 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100958 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100959 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100960
Shaohua Lic1e44752010-11-08 15:01:02 +0100961 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100962}
963
964/*
Jens Axboe5e705372006-07-13 12:39:25 +0200965 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200967 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Jens Axboe5e705372006-07-13 12:39:25 +0200969static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100970cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100972 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200974#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
975#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
976 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jens Axboe5e705372006-07-13 12:39:25 +0200978 if (rq1 == NULL || rq1 == rq2)
979 return rq2;
980 if (rq2 == NULL)
981 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200982
Namhyung Kim229836b2011-05-24 10:23:21 +0200983 if (rq_is_sync(rq1) != rq_is_sync(rq2))
984 return rq_is_sync(rq1) ? rq1 : rq2;
985
Christoph Hellwig65299a32011-08-23 14:50:29 +0200986 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
987 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +0200988
Tejun Heo83096eb2009-05-07 22:24:39 +0900989 s1 = blk_rq_pos(rq1);
990 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
993 * by definition, 1KiB is 2 sectors
994 */
995 back_max = cfqd->cfq_back_max * 2;
996
997 /*
998 * Strict one way elevator _except_ in the case where we allow
999 * short backward seeks which are biased as twice the cost of a
1000 * similar forward seek.
1001 */
1002 if (s1 >= last)
1003 d1 = s1 - last;
1004 else if (s1 + back_max >= last)
1005 d1 = (last - s1) * cfqd->cfq_back_penalty;
1006 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001007 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (s2 >= last)
1010 d2 = s2 - last;
1011 else if (s2 + back_max >= last)
1012 d2 = (last - s2) * cfqd->cfq_back_penalty;
1013 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001014 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Andreas Mohre8a99052006-03-28 08:59:49 +02001018 /*
1019 * By doing switch() on the bit mask "wrap" we avoid having to
1020 * check two variables for all permutations: --> faster!
1021 */
1022 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001023 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001024 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001025 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001026 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001027 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001028 else {
1029 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001030 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001031 else
Jens Axboe5e705372006-07-13 12:39:25 +02001032 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001033 }
1034
1035 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001036 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001037 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001038 return rq2;
1039 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001040 default:
1041 /*
1042 * Since both rqs are wrapped,
1043 * start with the one that's further behind head
1044 * (--> only *one* back seek required),
1045 * since back seek takes more time than forward.
1046 */
1047 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001048 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 else
Jens Axboe5e705372006-07-13 12:39:25 +02001050 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052}
1053
Jens Axboe498d3aa22007-04-26 12:54:48 +02001054/*
1055 * The below is leftmost cache rbtree addon
1056 */
Jens Axboe08717142008-01-28 11:38:15 +01001057static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001058{
Vivek Goyal615f0252009-12-03 12:59:39 -05001059 /* Service tree is empty */
1060 if (!root->count)
1061 return NULL;
1062
Jens Axboecc09e292007-04-26 12:53:50 +02001063 if (!root->left)
1064 root->left = rb_first(&root->rb);
1065
Jens Axboe08717142008-01-28 11:38:15 +01001066 if (root->left)
1067 return rb_entry(root->left, struct cfq_queue, rb_node);
1068
1069 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001070}
1071
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001072static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1073{
1074 if (!root->left)
1075 root->left = rb_first(&root->rb);
1076
1077 if (root->left)
1078 return rb_entry_cfqg(root->left);
1079
1080 return NULL;
1081}
1082
Jens Axboea36e71f2009-04-15 12:15:11 +02001083static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1084{
1085 rb_erase(n, root);
1086 RB_CLEAR_NODE(n);
1087}
1088
Jens Axboecc09e292007-04-26 12:53:50 +02001089static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1090{
1091 if (root->left == n)
1092 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001093 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001094 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/*
1098 * would be nice to take fifo expire time into account as well
1099 */
Jens Axboe5e705372006-07-13 12:39:25 +02001100static struct request *
1101cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1102 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Jens Axboe21183b02006-07-13 12:33:14 +02001104 struct rb_node *rbnext = rb_next(&last->rb_node);
1105 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001106 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jens Axboe21183b02006-07-13 12:33:14 +02001108 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001111 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001114 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001115 else {
1116 rbnext = rb_first(&cfqq->sort_list);
1117 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001118 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001121 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Jens Axboed9e76202007-04-20 14:27:50 +02001124static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1125 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Jens Axboed9e76202007-04-20 14:27:50 +02001127 /*
1128 * just an approximation, should be ok.
1129 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001130 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001131 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001134static inline s64
1135cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1136{
1137 return cfqg->vdisktime - st->min_vdisktime;
1138}
1139
1140static void
1141__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1142{
1143 struct rb_node **node = &st->rb.rb_node;
1144 struct rb_node *parent = NULL;
1145 struct cfq_group *__cfqg;
1146 s64 key = cfqg_key(st, cfqg);
1147 int left = 1;
1148
1149 while (*node != NULL) {
1150 parent = *node;
1151 __cfqg = rb_entry_cfqg(parent);
1152
1153 if (key < cfqg_key(st, __cfqg))
1154 node = &parent->rb_left;
1155 else {
1156 node = &parent->rb_right;
1157 left = 0;
1158 }
1159 }
1160
1161 if (left)
1162 st->left = &cfqg->rb_node;
1163
1164 rb_link_node(&cfqg->rb_node, parent, node);
1165 rb_insert_color(&cfqg->rb_node, &st->rb);
1166}
1167
1168static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001169cfq_update_group_weight(struct cfq_group *cfqg)
1170{
1171 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1172 if (cfqg->needs_update) {
1173 cfqg->weight = cfqg->new_weight;
1174 cfqg->needs_update = false;
1175 }
1176}
1177
1178static void
1179cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1180{
1181 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1182
1183 cfq_update_group_weight(cfqg);
1184 __cfq_group_service_tree_add(st, cfqg);
1185 st->total_weight += cfqg->weight;
1186}
1187
1188static void
1189cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001190{
1191 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1192 struct cfq_group *__cfqg;
1193 struct rb_node *n;
1194
1195 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001196 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001197 return;
1198
1199 /*
1200 * Currently put the group at the end. Later implement something
1201 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001202 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001203 */
1204 n = rb_last(&st->rb);
1205 if (n) {
1206 __cfqg = rb_entry_cfqg(n);
1207 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1208 } else
1209 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001210 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001211}
1212
1213static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001214cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1215{
1216 st->total_weight -= cfqg->weight;
1217 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1218 cfq_rb_erase(&cfqg->rb_node, st);
1219}
1220
1221static void
1222cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001223{
1224 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1225
1226 BUG_ON(cfqg->nr_cfqq < 1);
1227 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001228
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001229 /* If there are other cfq queues under this group, don't delete it */
1230 if (cfqg->nr_cfqq)
1231 return;
1232
Vivek Goyal2868ef72009-12-03 12:59:48 -05001233 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001234 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001235 cfqg->saved_workload_slice = 0;
Tejun Heoc1768262012-03-05 13:15:17 -08001236 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg),
1237 &blkio_policy_cfq, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001238}
1239
Justin TerAvest167400d2011-03-12 16:54:00 +01001240static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1241 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001242{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001243 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001244
1245 /*
1246 * Queue got expired before even a single request completed or
1247 * got expired immediately after first request completion.
1248 */
1249 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1250 /*
1251 * Also charge the seek time incurred to the group, otherwise
1252 * if there are mutiple queues in the group, each can dispatch
1253 * a single request on seeky media and cause lots of seek time
1254 * and group will never know it.
1255 */
1256 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1257 1);
1258 } else {
1259 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001260 if (slice_used > cfqq->allocated_slice) {
1261 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001262 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001263 }
1264 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1265 *unaccounted_time += cfqq->slice_start -
1266 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001267 }
1268
Vivek Goyaldae739e2009-12-03 12:59:45 -05001269 return slice_used;
1270}
1271
1272static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001273 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001274{
1275 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001276 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001277 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1278 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001279
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001280 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001281 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001282
Vivek Goyal02b35082010-08-23 12:23:53 +02001283 if (iops_mode(cfqd))
1284 charge = cfqq->slice_dispatch;
1285 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1286 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001287
1288 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001289 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001290 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001291 /* If a new weight was requested, update now, off tree */
1292 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001293
1294 /* This group is being expired. Save the context */
1295 if (time_after(cfqd->workload_expires, jiffies)) {
1296 cfqg->saved_workload_slice = cfqd->workload_expires
1297 - jiffies;
1298 cfqg->saved_workload = cfqd->serving_type;
1299 cfqg->saved_serving_prio = cfqd->serving_prio;
1300 } else
1301 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001302
1303 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1304 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001305 cfq_log_cfqq(cfqq->cfqd, cfqq,
1306 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1307 used_sl, cfqq->slice_dispatch, charge,
1308 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heoc1768262012-03-05 13:15:17 -08001309 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), &blkio_policy_cfq,
1310 used_sl, unaccounted_sl);
1311 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg), &blkio_policy_cfq);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001312}
1313
Tejun Heof51b8022012-03-05 13:15:05 -08001314/**
1315 * cfq_init_cfqg_base - initialize base part of a cfq_group
1316 * @cfqg: cfq_group to initialize
1317 *
1318 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1319 * is enabled or not.
1320 */
1321static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1322{
1323 struct cfq_rb_root *st;
1324 int i, j;
1325
1326 for_each_cfqg_st(cfqg, i, j, st)
1327 *st = CFQ_RB_ROOT;
1328 RB_CLEAR_NODE(&cfqg->rb_node);
1329
1330 cfqg->ttime.last_end_request = jiffies;
1331}
1332
Vivek Goyal25fb5162009-12-03 12:59:46 -05001333#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334static void cfq_update_blkio_group_weight(struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001335 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001336{
Tejun Heo03814112012-03-05 13:15:14 -08001337 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1338
Justin TerAvest8184f932011-03-17 16:12:36 +01001339 cfqg->new_weight = weight;
1340 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001341}
1342
Tejun Heo03814112012-03-05 13:15:14 -08001343static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001344{
Tejun Heo03814112012-03-05 13:15:14 -08001345 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001346
Tejun Heof51b8022012-03-05 13:15:05 -08001347 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001348 cfqg->weight = blkg->blkcg->weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001349}
1350
1351/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001352 * Search for the cfq group current task belongs to. request_queue lock must
1353 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001354 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001355static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1356 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001357{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001358 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001359 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001360
Tejun Heocd1604f2012-03-05 13:15:06 -08001361 /* avoid lookup for the common case where there's no blkio cgroup */
1362 if (blkcg == &blkio_root_cgroup) {
1363 cfqg = cfqd->root_group;
1364 } else {
1365 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001366
Tejun Heoaaec55a2012-04-01 14:38:42 -07001367 blkg = blkg_lookup_create(blkcg, q, false);
Tejun Heocd1604f2012-03-05 13:15:06 -08001368 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001369 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001370 }
1371
Vivek Goyal25fb5162009-12-03 12:59:46 -05001372 return cfqg;
1373}
1374
1375static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1376{
1377 /* Currently, all async queues are mapped to root group */
1378 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001379 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001380
1381 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001382 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001383 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001384}
1385
Tejun Heo60c2bc22012-04-01 14:38:43 -07001386static u64 blkg_prfill_weight_device(struct seq_file *sf,
1387 struct blkg_policy_data *pd, int off)
1388{
1389 if (!pd->conf.weight)
1390 return 0;
1391 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
1392}
1393
1394static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1395 struct seq_file *sf)
1396{
1397 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1398 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1399 false);
1400 return 0;
1401}
1402
1403static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1404 struct seq_file *sf)
1405{
1406 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
1407 return 0;
1408}
1409
1410static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1411 const char *buf)
1412{
1413 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1414 struct blkg_policy_data *pd;
1415 struct blkg_conf_ctx ctx;
1416 int ret;
1417
1418 ret = blkg_conf_prep(blkcg, buf, &ctx);
1419 if (ret)
1420 return ret;
1421
1422 ret = -EINVAL;
1423 pd = ctx.blkg->pd[BLKIO_POLICY_PROP];
1424 if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN &&
1425 ctx.v <= BLKIO_WEIGHT_MAX))) {
1426 pd->conf.weight = ctx.v;
1427 cfq_update_blkio_group_weight(ctx.blkg, ctx.v ?: blkcg->weight);
1428 ret = 0;
1429 }
1430
1431 blkg_conf_finish(&ctx);
1432 return ret;
1433}
1434
1435static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1436{
1437 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1438 struct blkio_group *blkg;
1439 struct hlist_node *n;
1440
1441 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1442 return -EINVAL;
1443
1444 spin_lock_irq(&blkcg->lock);
1445 blkcg->weight = (unsigned int)val;
1446
1447 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1448 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
1449
1450 if (pd && !pd->conf.weight)
1451 cfq_update_blkio_group_weight(blkg, blkcg->weight);
1452 }
1453
1454 spin_unlock_irq(&blkcg->lock);
1455 return 0;
1456}
1457
1458#ifdef CONFIG_DEBUG_BLK_CGROUP
1459static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
1460 struct blkg_policy_data *pd, int off)
1461{
1462 u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
1463 u64 v = 0;
1464
1465 if (samples) {
1466 v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
1467 do_div(v, samples);
1468 }
1469 __blkg_prfill_u64(sf, pd, v);
1470 return 0;
1471}
1472
1473/* print avg_queue_size */
1474static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1475 struct seq_file *sf)
1476{
1477 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1478
1479 blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
1480 BLKIO_POLICY_PROP, 0, false);
1481 return 0;
1482}
1483#endif /* CONFIG_DEBUG_BLK_CGROUP */
1484
1485static struct cftype cfq_blkcg_files[] = {
1486 {
1487 .name = "weight_device",
1488 .read_seq_string = blkcg_print_weight_device,
1489 .write_string = blkcg_set_weight_device,
1490 .max_write_len = 256,
1491 },
1492 {
1493 .name = "weight",
1494 .read_seq_string = blkcg_print_weight,
1495 .write_u64 = blkcg_set_weight,
1496 },
1497 {
1498 .name = "time",
1499 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1500 offsetof(struct blkio_group_stats, time)),
1501 .read_seq_string = blkcg_print_stat,
1502 },
1503 {
1504 .name = "sectors",
1505 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
Tejun Heo41b38b62012-04-01 14:38:44 -07001506 offsetof(struct blkio_group_stats, sectors)),
1507 .read_seq_string = blkcg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001508 },
1509 {
1510 .name = "io_service_bytes",
1511 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
Tejun Heo41b38b62012-04-01 14:38:44 -07001512 offsetof(struct blkio_group_stats, service_bytes)),
1513 .read_seq_string = blkcg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001514 },
1515 {
1516 .name = "io_serviced",
1517 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
Tejun Heo41b38b62012-04-01 14:38:44 -07001518 offsetof(struct blkio_group_stats, serviced)),
1519 .read_seq_string = blkcg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001520 },
1521 {
1522 .name = "io_service_time",
1523 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1524 offsetof(struct blkio_group_stats, service_time)),
1525 .read_seq_string = blkcg_print_rwstat,
1526 },
1527 {
1528 .name = "io_wait_time",
1529 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1530 offsetof(struct blkio_group_stats, wait_time)),
1531 .read_seq_string = blkcg_print_rwstat,
1532 },
1533 {
1534 .name = "io_merged",
1535 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1536 offsetof(struct blkio_group_stats, merged)),
1537 .read_seq_string = blkcg_print_rwstat,
1538 },
1539 {
1540 .name = "io_queued",
1541 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1542 offsetof(struct blkio_group_stats, queued)),
1543 .read_seq_string = blkcg_print_rwstat,
1544 },
1545#ifdef CONFIG_DEBUG_BLK_CGROUP
1546 {
1547 .name = "avg_queue_size",
1548 .read_seq_string = blkcg_print_avg_queue_size,
1549 },
1550 {
1551 .name = "group_wait_time",
1552 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1553 offsetof(struct blkio_group_stats, group_wait_time)),
1554 .read_seq_string = blkcg_print_stat,
1555 },
1556 {
1557 .name = "idle_time",
1558 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1559 offsetof(struct blkio_group_stats, idle_time)),
1560 .read_seq_string = blkcg_print_stat,
1561 },
1562 {
1563 .name = "empty_time",
1564 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1565 offsetof(struct blkio_group_stats, empty_time)),
1566 .read_seq_string = blkcg_print_stat,
1567 },
1568 {
1569 .name = "dequeue",
1570 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1571 offsetof(struct blkio_group_stats, dequeue)),
1572 .read_seq_string = blkcg_print_stat,
1573 },
1574 {
1575 .name = "unaccounted_time",
1576 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1577 offsetof(struct blkio_group_stats, unaccounted_time)),
1578 .read_seq_string = blkcg_print_stat,
1579 },
1580#endif /* CONFIG_DEBUG_BLK_CGROUP */
1581 { } /* terminate */
1582};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001583#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001584static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1585 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001586{
Tejun Heof51b8022012-03-05 13:15:05 -08001587 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001588}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001589
Vivek Goyal25fb5162009-12-03 12:59:46 -05001590static inline void
1591cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1592 cfqq->cfqg = cfqg;
1593}
1594
1595#endif /* GROUP_IOSCHED */
1596
Jens Axboe498d3aa22007-04-26 12:54:48 +02001597/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001598 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001599 * requests waiting to be processed. It is sorted in the order that
1600 * we will service the queues.
1601 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001602static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001603 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001604{
Jens Axboe08717142008-01-28 11:38:15 +01001605 struct rb_node **p, *parent;
1606 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001607 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001608 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001609 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001610 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001611
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001612 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001613 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001614 if (cfq_class_idle(cfqq)) {
1615 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001616 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001617 if (parent && parent != &cfqq->rb_node) {
1618 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1619 rb_key += __cfqq->rb_key;
1620 } else
1621 rb_key += jiffies;
1622 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001623 /*
1624 * Get our rb key offset. Subtract any residual slice
1625 * value carried from last service. A negative resid
1626 * count indicates slice overrun, and this should position
1627 * the next service time further away in the tree.
1628 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001629 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001630 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001631 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001632 } else {
1633 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001634 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001635 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1636 }
Jens Axboed9e76202007-04-20 14:27:50 +02001637
1638 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001639 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001640 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001641 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001642 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001643 if (rb_key == cfqq->rb_key &&
1644 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001645 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001646
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001647 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1648 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001649 }
Jens Axboed9e76202007-04-20 14:27:50 +02001650
Jens Axboe498d3aa22007-04-26 12:54:48 +02001651 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001652 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001653 cfqq->service_tree = service_tree;
1654 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001655 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001656 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001657
Jens Axboed9e76202007-04-20 14:27:50 +02001658 parent = *p;
1659 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1660
Jens Axboe0c534e02007-04-18 20:01:57 +02001661 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001662 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001663 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001664 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001665 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001666 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001667 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001668 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001669 }
Jens Axboe67060e32007-04-18 20:13:32 +02001670
1671 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001672 }
1673
Jens Axboecc09e292007-04-26 12:53:50 +02001674 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001675 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001676
Jens Axboed9e76202007-04-20 14:27:50 +02001677 cfqq->rb_key = rb_key;
1678 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001679 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1680 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001681 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001682 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001683 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Jens Axboea36e71f2009-04-15 12:15:11 +02001686static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001687cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1688 sector_t sector, struct rb_node **ret_parent,
1689 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001690{
Jens Axboea36e71f2009-04-15 12:15:11 +02001691 struct rb_node **p, *parent;
1692 struct cfq_queue *cfqq = NULL;
1693
1694 parent = NULL;
1695 p = &root->rb_node;
1696 while (*p) {
1697 struct rb_node **n;
1698
1699 parent = *p;
1700 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1701
1702 /*
1703 * Sort strictly based on sector. Smallest to the left,
1704 * largest to the right.
1705 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001706 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001707 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001708 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001709 n = &(*p)->rb_left;
1710 else
1711 break;
1712 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001713 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001714 }
1715
1716 *ret_parent = parent;
1717 if (rb_link)
1718 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001719 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001720}
1721
1722static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1723{
Jens Axboea36e71f2009-04-15 12:15:11 +02001724 struct rb_node **p, *parent;
1725 struct cfq_queue *__cfqq;
1726
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001727 if (cfqq->p_root) {
1728 rb_erase(&cfqq->p_node, cfqq->p_root);
1729 cfqq->p_root = NULL;
1730 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001731
1732 if (cfq_class_idle(cfqq))
1733 return;
1734 if (!cfqq->next_rq)
1735 return;
1736
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001737 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001738 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1739 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001740 if (!__cfqq) {
1741 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001742 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1743 } else
1744 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001745}
1746
Jens Axboe498d3aa22007-04-26 12:54:48 +02001747/*
1748 * Update cfqq's position in the service tree.
1749 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001750static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001751{
Jens Axboe6d048f52007-04-25 12:44:27 +02001752 /*
1753 * Resorting requires the cfqq to be on the RR list already.
1754 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001755 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001756 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001757 cfq_prio_tree_add(cfqd, cfqq);
1758 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/*
1762 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001763 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 */
Jens Axboefebffd62008-01-28 13:19:43 +01001765static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Jens Axboe7b679132008-05-30 12:23:07 +02001767 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001768 BUG_ON(cfq_cfqq_on_rr(cfqq));
1769 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001771 if (cfq_cfqq_sync(cfqq))
1772 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Jens Axboeedd75ff2007-04-19 12:03:34 +02001774 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Jens Axboe498d3aa22007-04-26 12:54:48 +02001777/*
1778 * Called when the cfqq no longer has requests pending, remove it from
1779 * the service tree.
1780 */
Jens Axboefebffd62008-01-28 13:19:43 +01001781static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Jens Axboe7b679132008-05-30 12:23:07 +02001783 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001784 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1785 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001787 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1788 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1789 cfqq->service_tree = NULL;
1790 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001791 if (cfqq->p_root) {
1792 rb_erase(&cfqq->p_node, cfqq->p_root);
1793 cfqq->p_root = NULL;
1794 }
Jens Axboed9e76202007-04-20 14:27:50 +02001795
Justin TerAvest8184f932011-03-17 16:12:36 +01001796 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 BUG_ON(!cfqd->busy_queues);
1798 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001799 if (cfq_cfqq_sync(cfqq))
1800 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803/*
1804 * rb tree support functions
1805 */
Jens Axboefebffd62008-01-28 13:19:43 +01001806static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Jens Axboe5e705372006-07-13 12:39:25 +02001808 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001809 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jens Axboeb4878f22005-10-20 16:42:29 +02001811 BUG_ON(!cfqq->queued[sync]);
1812 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Jens Axboe5e705372006-07-13 12:39:25 +02001814 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Vivek Goyalf04a6422009-12-03 12:59:40 -05001816 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1817 /*
1818 * Queue will be deleted from service tree when we actually
1819 * expire it later. Right now just remove it from prio tree
1820 * as it is empty.
1821 */
1822 if (cfqq->p_root) {
1823 rb_erase(&cfqq->p_node, cfqq->p_root);
1824 cfqq->p_root = NULL;
1825 }
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Jens Axboe5e705372006-07-13 12:39:25 +02001829static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Jens Axboe5e705372006-07-13 12:39:25 +02001831 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001833 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jens Axboe5380a102006-07-13 12:37:56 +02001835 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jeff Moyer796d5112011-06-02 21:19:05 +02001837 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001838
1839 if (!cfq_cfqq_on_rr(cfqq))
1840 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001841
1842 /*
1843 * check if this request is a better next-serve candidate
1844 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001845 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001846 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001847
1848 /*
1849 * adjust priority tree position, if ->next_rq changes
1850 */
1851 if (prev != cfqq->next_rq)
1852 cfq_prio_tree_add(cfqd, cfqq);
1853
Jens Axboe5044eed2007-04-25 11:53:48 +02001854 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Jens Axboefebffd62008-01-28 13:19:43 +01001857static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Jens Axboe5380a102006-07-13 12:37:56 +02001859 elv_rb_del(&cfqq->sort_list, rq);
1860 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001861 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001862 &blkio_policy_cfq, rq_data_dir(rq),
1863 rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001864 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001865 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001866 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08001867 cfqg_to_blkg(cfqq->cfqd->serving_group),
1868 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Jens Axboe206dc692006-03-28 13:03:44 +02001871static struct request *
1872cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Jens Axboe206dc692006-03-28 13:03:44 +02001874 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001875 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001876 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Jens Axboe4ac845a2008-01-24 08:44:49 +01001878 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001879 if (!cic)
1880 return NULL;
1881
1882 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001883 if (cfqq) {
1884 sector_t sector = bio->bi_sector + bio_sectors(bio);
1885
Jens Axboe21183b02006-07-13 12:33:14 +02001886 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return NULL;
1890}
1891
Jens Axboe165125e2007-07-24 09:28:11 +02001892static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001893{
1894 struct cfq_data *cfqd = q->elevator->elevator_data;
1895
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001896 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001897 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001898 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001899
Tejun Heo5b936292009-05-07 22:24:38 +09001900 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001901}
1902
Jens Axboe165125e2007-07-24 09:28:11 +02001903static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Jens Axboe22e2c502005-06-27 10:55:12 +02001905 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001907 WARN_ON(!cfqd->rq_in_driver);
1908 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001909 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001910 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Jens Axboeb4878f22005-10-20 16:42:29 +02001913static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Jens Axboe5e705372006-07-13 12:39:25 +02001915 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001916
Jens Axboe5e705372006-07-13 12:39:25 +02001917 if (cfqq->next_rq == rq)
1918 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Jens Axboeb4878f22005-10-20 16:42:29 +02001920 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001921 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001922
Aaron Carroll45333d52008-08-26 15:52:36 +02001923 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001924 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001925 &blkio_policy_cfq, rq_data_dir(rq),
1926 rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001927 if (rq->cmd_flags & REQ_PRIO) {
1928 WARN_ON(!cfqq->prio_pending);
1929 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Jens Axboe165125e2007-07-24 09:28:11 +02001933static int cfq_merge(struct request_queue *q, struct request **req,
1934 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct cfq_data *cfqd = q->elevator->elevator_data;
1937 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jens Axboe206dc692006-03-28 13:03:44 +02001939 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001940 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001941 *req = __rq;
1942 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Jens Axboe165125e2007-07-24 09:28:11 +02001948static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001949 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Jens Axboe21183b02006-07-13 12:33:14 +02001951 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001952 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Jens Axboe5e705372006-07-13 12:39:25 +02001954 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Divyesh Shah812d4022010-04-08 21:14:23 -07001958static void cfq_bio_merged(struct request_queue *q, struct request *req,
1959 struct bio *bio)
1960{
Tejun Heo03814112012-03-05 13:15:14 -08001961 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Tejun Heoc1768262012-03-05 13:15:17 -08001962 &blkio_policy_cfq, bio_data_dir(bio),
1963 cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966static void
Jens Axboe165125e2007-07-24 09:28:11 +02001967cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct request *next)
1969{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001970 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001971 struct cfq_data *cfqd = q->elevator->elevator_data;
1972
Jens Axboe22e2c502005-06-27 10:55:12 +02001973 /*
1974 * reposition in fifo if next is older than rq
1975 */
1976 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001977 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001978 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001979 rq_set_fifo_time(rq, rq_fifo_time(next));
1980 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001981
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001982 if (cfqq->next_rq == next)
1983 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001984 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08001985 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001986 &blkio_policy_cfq, rq_data_dir(next),
1987 rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001988
1989 cfqq = RQ_CFQQ(next);
1990 /*
1991 * all requests of this queue are merged to other queues, delete it
1992 * from the service tree. If it's the active_queue,
1993 * cfq_dispatch_requests() will choose to expire it or do idle
1994 */
1995 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1996 cfqq != cfqd->active_queue)
1997 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001998}
1999
Jens Axboe165125e2007-07-24 09:28:11 +02002000static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002001 struct bio *bio)
2002{
2003 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002004 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002005 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002006
2007 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002008 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002009 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002010 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002011 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002012
2013 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002014 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002015 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002016 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002017 cic = cfq_cic_lookup(cfqd, current->io_context);
2018 if (!cic)
2019 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002020
Vasily Tarasov91fac312007-04-25 12:29:51 +02002021 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002022 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002023}
2024
Divyesh Shah812df482010-04-08 21:15:35 -07002025static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2026{
2027 del_timer(&cfqd->idle_slice_timer);
Tejun Heoc1768262012-03-05 13:15:17 -08002028 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
2029 &blkio_policy_cfq);
Divyesh Shah812df482010-04-08 21:15:35 -07002030}
2031
Jens Axboefebffd62008-01-28 13:19:43 +01002032static void __cfq_set_active_queue(struct cfq_data *cfqd,
2033 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002034{
2035 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002036 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
2037 cfqd->serving_prio, cfqd->serving_type);
Tejun Heoc1768262012-03-05 13:15:17 -08002038 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg),
2039 &blkio_policy_cfq);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002040 cfqq->slice_start = 0;
2041 cfqq->dispatch_start = jiffies;
2042 cfqq->allocated_slice = 0;
2043 cfqq->slice_end = 0;
2044 cfqq->slice_dispatch = 0;
2045 cfqq->nr_sectors = 0;
2046
2047 cfq_clear_cfqq_wait_request(cfqq);
2048 cfq_clear_cfqq_must_dispatch(cfqq);
2049 cfq_clear_cfqq_must_alloc_slice(cfqq);
2050 cfq_clear_cfqq_fifo_expire(cfqq);
2051 cfq_mark_cfqq_slice_new(cfqq);
2052
2053 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002054 }
2055
2056 cfqd->active_queue = cfqq;
2057}
2058
2059/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002060 * current cfqq expired its slice (or was too idle), select new one
2061 */
2062static void
2063__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002064 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002065{
Jens Axboe7b679132008-05-30 12:23:07 +02002066 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2067
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002068 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002069 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002070
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002071 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002072 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002073
2074 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002075 * If this cfqq is shared between multiple processes, check to
2076 * make sure that those processes are still issuing I/Os within
2077 * the mean seek distance. If not, it may be time to break the
2078 * queues apart again.
2079 */
2080 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2081 cfq_mark_cfqq_split_coop(cfqq);
2082
2083 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002084 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002085 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002086 if (timed_out) {
2087 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002088 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002089 else
2090 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002091 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2092 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002093
Vivek Goyale5ff0822010-04-26 19:25:11 +02002094 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002095
Vivek Goyalf04a6422009-12-03 12:59:40 -05002096 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2097 cfq_del_cfqq_rr(cfqd, cfqq);
2098
Jens Axboeedd75ff2007-04-19 12:03:34 +02002099 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002100
2101 if (cfqq == cfqd->active_queue)
2102 cfqd->active_queue = NULL;
2103
2104 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002105 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002106 cfqd->active_cic = NULL;
2107 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002108}
2109
Vivek Goyale5ff0822010-04-26 19:25:11 +02002110static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002111{
2112 struct cfq_queue *cfqq = cfqd->active_queue;
2113
2114 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002115 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002116}
2117
Jens Axboe498d3aa22007-04-26 12:54:48 +02002118/*
2119 * Get next queue for service. Unless we have a queue preemption,
2120 * we'll simply select the first cfqq in the service tree.
2121 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002122static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002123{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002124 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002125 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002126 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002127
Vivek Goyalf04a6422009-12-03 12:59:40 -05002128 if (!cfqd->rq_queued)
2129 return NULL;
2130
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002131 /* There is nothing to dispatch */
2132 if (!service_tree)
2133 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002134 if (RB_EMPTY_ROOT(&service_tree->rb))
2135 return NULL;
2136 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02002137}
2138
Vivek Goyalf04a6422009-12-03 12:59:40 -05002139static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2140{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002141 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002142 struct cfq_queue *cfqq;
2143 int i, j;
2144 struct cfq_rb_root *st;
2145
2146 if (!cfqd->rq_queued)
2147 return NULL;
2148
Vivek Goyal25fb5162009-12-03 12:59:46 -05002149 cfqg = cfq_get_next_cfqg(cfqd);
2150 if (!cfqg)
2151 return NULL;
2152
Vivek Goyalf04a6422009-12-03 12:59:40 -05002153 for_each_cfqg_st(cfqg, i, j, st)
2154 if ((cfqq = cfq_rb_first(st)) != NULL)
2155 return cfqq;
2156 return NULL;
2157}
2158
Jens Axboe498d3aa22007-04-26 12:54:48 +02002159/*
2160 * Get and set a new active queue for service.
2161 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002162static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2163 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002164{
Jens Axboee00ef792009-11-04 08:54:55 +01002165 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002166 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002167
Jens Axboe22e2c502005-06-27 10:55:12 +02002168 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002169 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002170}
2171
Jens Axboed9e76202007-04-20 14:27:50 +02002172static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2173 struct request *rq)
2174{
Tejun Heo83096eb2009-05-07 22:24:39 +09002175 if (blk_rq_pos(rq) >= cfqd->last_position)
2176 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002177 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002178 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002179}
2180
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002181static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002182 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002183{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002184 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002185}
2186
Jens Axboea36e71f2009-04-15 12:15:11 +02002187static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2188 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002189{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002190 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002191 struct rb_node *parent, *node;
2192 struct cfq_queue *__cfqq;
2193 sector_t sector = cfqd->last_position;
2194
2195 if (RB_EMPTY_ROOT(root))
2196 return NULL;
2197
2198 /*
2199 * First, if we find a request starting at the end of the last
2200 * request, choose it.
2201 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002202 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002203 if (__cfqq)
2204 return __cfqq;
2205
2206 /*
2207 * If the exact sector wasn't found, the parent of the NULL leaf
2208 * will contain the closest sector.
2209 */
2210 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002211 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002212 return __cfqq;
2213
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002214 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002215 node = rb_next(&__cfqq->p_node);
2216 else
2217 node = rb_prev(&__cfqq->p_node);
2218 if (!node)
2219 return NULL;
2220
2221 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002222 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002223 return __cfqq;
2224
2225 return NULL;
2226}
2227
2228/*
2229 * cfqd - obvious
2230 * cur_cfqq - passed in so that we don't decide that the current queue is
2231 * closely cooperating with itself.
2232 *
2233 * So, basically we're assuming that that cur_cfqq has dispatched at least
2234 * one request, and that cfqd->last_position reflects a position on the disk
2235 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2236 * assumption.
2237 */
2238static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002239 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002240{
2241 struct cfq_queue *cfqq;
2242
Divyesh Shah39c01b22010-03-25 15:45:57 +01002243 if (cfq_class_idle(cur_cfqq))
2244 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002245 if (!cfq_cfqq_sync(cur_cfqq))
2246 return NULL;
2247 if (CFQQ_SEEKY(cur_cfqq))
2248 return NULL;
2249
Jens Axboea36e71f2009-04-15 12:15:11 +02002250 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002251 * Don't search priority tree if it's the only queue in the group.
2252 */
2253 if (cur_cfqq->cfqg->nr_cfqq == 1)
2254 return NULL;
2255
2256 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002257 * We should notice if some of the queues are cooperating, eg
2258 * working closely on the same area of the disk. In that case,
2259 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002260 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002261 cfqq = cfqq_close(cfqd, cur_cfqq);
2262 if (!cfqq)
2263 return NULL;
2264
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002265 /* If new queue belongs to different cfq_group, don't choose it */
2266 if (cur_cfqq->cfqg != cfqq->cfqg)
2267 return NULL;
2268
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002269 /*
2270 * It only makes sense to merge sync queues.
2271 */
2272 if (!cfq_cfqq_sync(cfqq))
2273 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002274 if (CFQQ_SEEKY(cfqq))
2275 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002276
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002277 /*
2278 * Do not merge queues of different priority classes
2279 */
2280 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2281 return NULL;
2282
Jens Axboea36e71f2009-04-15 12:15:11 +02002283 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002284}
2285
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002286/*
2287 * Determine whether we should enforce idle window for this queue.
2288 */
2289
2290static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2291{
2292 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002293 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002294
Vivek Goyalf04a6422009-12-03 12:59:40 -05002295 BUG_ON(!service_tree);
2296 BUG_ON(!service_tree->count);
2297
Vivek Goyalb6508c12010-08-23 12:23:33 +02002298 if (!cfqd->cfq_slice_idle)
2299 return false;
2300
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002301 /* We never do for idle class queues. */
2302 if (prio == IDLE_WORKLOAD)
2303 return false;
2304
2305 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002306 if (cfq_cfqq_idle_window(cfqq) &&
2307 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002308 return true;
2309
2310 /*
2311 * Otherwise, we do only if they are the last ones
2312 * in their service tree.
2313 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002314 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
2315 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002316 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002317 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
2318 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002319 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002320}
2321
Jens Axboe6d048f52007-04-25 12:44:27 +02002322static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002323{
Jens Axboe17926692007-01-19 11:59:30 +11002324 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002325 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002326 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002327
Jens Axboea68bbdd2008-09-24 13:03:33 +02002328 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002329 * SSD device without seek penalty, disable idling. But only do so
2330 * for devices that support queuing, otherwise we still have a problem
2331 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002332 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002333 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002334 return;
2335
Jens Axboedd67d052006-06-21 09:36:18 +02002336 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002337 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002338
2339 /*
2340 * idle is disabled, either manually or by past process history
2341 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002342 if (!cfq_should_idle(cfqd, cfqq)) {
2343 /* no queue idling. Check for group idling */
2344 if (cfqd->cfq_group_idle)
2345 group_idle = cfqd->cfq_group_idle;
2346 else
2347 return;
2348 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002349
Jens Axboe22e2c502005-06-27 10:55:12 +02002350 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002351 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002352 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002353 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002354 return;
2355
2356 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002357 * task has exited, don't wait
2358 */
Jens Axboe206dc692006-03-28 13:03:44 +02002359 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002360 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002361 return;
2362
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002363 /*
2364 * If our average think time is larger than the remaining time
2365 * slice, then don't idle. This avoids overrunning the allotted
2366 * time slice.
2367 */
Shaohua Li383cd722011-07-12 14:24:35 +02002368 if (sample_valid(cic->ttime.ttime_samples) &&
2369 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002370 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002371 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002372 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002373 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002374
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002375 /* There are other queues in the group, don't do group idle */
2376 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2377 return;
2378
Jens Axboe3b181522005-06-27 10:56:24 +02002379 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002380
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002381 if (group_idle)
2382 sl = cfqd->cfq_group_idle;
2383 else
2384 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002385
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002386 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heoc1768262012-03-05 13:15:17 -08002387 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
2388 &blkio_policy_cfq);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002389 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2390 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Jens Axboe498d3aa22007-04-26 12:54:48 +02002393/*
2394 * Move request from internal lists to the request queue dispatch list.
2395 */
Jens Axboe165125e2007-07-24 09:28:11 +02002396static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002398 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002399 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002400
Jens Axboe7b679132008-05-30 12:23:07 +02002401 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2402
Jeff Moyer06d21882009-09-11 17:08:59 +02002403 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002404 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002405 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002406 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002407 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002408
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002409 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002410 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08002411 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08002412 &blkio_policy_cfq, blk_rq_bytes(rq),
2413 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
2416/*
2417 * return expired entry, or NULL to just start from scratch in rbtree
2418 */
Jens Axboefebffd62008-01-28 13:19:43 +01002419static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Jens Axboe30996f42009-10-05 11:03:39 +02002421 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jens Axboe3b181522005-06-27 10:56:24 +02002423 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002425
2426 cfq_mark_cfqq_fifo_expire(cfqq);
2427
Jens Axboe89850f72006-07-22 16:48:31 +02002428 if (list_empty(&cfqq->fifo))
2429 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Jens Axboe89850f72006-07-22 16:48:31 +02002431 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002432 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002433 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Jens Axboe30996f42009-10-05 11:03:39 +02002435 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002436 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Jens Axboe22e2c502005-06-27 10:55:12 +02002439static inline int
2440cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2441{
2442 const int base_rq = cfqd->cfq_slice_async_rq;
2443
2444 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2445
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002446 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002447}
2448
2449/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002450 * Must be called with the queue_lock held.
2451 */
2452static int cfqq_process_refs(struct cfq_queue *cfqq)
2453{
2454 int process_refs, io_refs;
2455
2456 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002457 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002458 BUG_ON(process_refs < 0);
2459 return process_refs;
2460}
2461
2462static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2463{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002464 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002465 struct cfq_queue *__cfqq;
2466
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002467 /*
2468 * If there are no process references on the new_cfqq, then it is
2469 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2470 * chain may have dropped their last reference (not just their
2471 * last process reference).
2472 */
2473 if (!cfqq_process_refs(new_cfqq))
2474 return;
2475
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002476 /* Avoid a circular list and skip interim queue merges */
2477 while ((__cfqq = new_cfqq->new_cfqq)) {
2478 if (__cfqq == cfqq)
2479 return;
2480 new_cfqq = __cfqq;
2481 }
2482
2483 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002484 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002485 /*
2486 * If the process for the cfqq has gone away, there is no
2487 * sense in merging the queues.
2488 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002489 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002490 return;
2491
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002492 /*
2493 * Merge in the direction of the lesser amount of work.
2494 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002495 if (new_process_refs >= process_refs) {
2496 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002497 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002498 } else {
2499 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002500 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002501 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002502}
2503
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002504static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002505 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002506{
2507 struct cfq_queue *queue;
2508 int i;
2509 bool key_valid = false;
2510 unsigned long lowest_key = 0;
2511 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2512
Vivek Goyal65b32a52009-12-16 17:52:59 -05002513 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2514 /* select the one with lowest rb_key */
2515 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002516 if (queue &&
2517 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2518 lowest_key = queue->rb_key;
2519 cur_best = i;
2520 key_valid = true;
2521 }
2522 }
2523
2524 return cur_best;
2525}
2526
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002527static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002528{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002529 unsigned slice;
2530 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002531 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002532 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002533 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002534
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002535 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002536 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002537 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002538 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002539 cfqd->serving_prio = BE_WORKLOAD;
2540 else {
2541 cfqd->serving_prio = IDLE_WORKLOAD;
2542 cfqd->workload_expires = jiffies + 1;
2543 return;
2544 }
2545
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002546 if (original_prio != cfqd->serving_prio)
2547 goto new_workload;
2548
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002549 /*
2550 * For RT and BE, we have to choose also the type
2551 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2552 * expiration time
2553 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002554 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002555 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002556
2557 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002558 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002559 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002560 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002561 return;
2562
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002563new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002564 /* otherwise select new workload type */
2565 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002566 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2567 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002568 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002569
2570 /*
2571 * the workload slice is computed as a fraction of target latency
2572 * proportional to the number of queues in that workload, over
2573 * all the queues in the same priority class
2574 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002575 group_slice = cfq_group_slice(cfqd, cfqg);
2576
2577 slice = group_slice * count /
2578 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2579 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002580
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002581 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2582 unsigned int tmp;
2583
2584 /*
2585 * Async queues are currently system wide. Just taking
2586 * proportion of queues with-in same group will lead to higher
2587 * async ratio system wide as generally root group is going
2588 * to have higher weight. A more accurate thing would be to
2589 * calculate system wide asnc/sync ratio.
2590 */
2591 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2592 tmp = tmp/cfqd->busy_queues;
2593 slice = min_t(unsigned, slice, tmp);
2594
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002595 /* async workload slice is scaled down according to
2596 * the sync/async slice ratio. */
2597 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002598 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002599 /* sync workload slice is at least 2 * cfq_slice_idle */
2600 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2601
2602 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002603 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002604 cfqd->workload_expires = jiffies + slice;
2605}
2606
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002607static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2608{
2609 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002610 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002611
2612 if (RB_EMPTY_ROOT(&st->rb))
2613 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002614 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002615 update_min_vdisktime(st);
2616 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002617}
2618
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002619static void cfq_choose_cfqg(struct cfq_data *cfqd)
2620{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002621 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2622
2623 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002624
2625 /* Restore the workload type data */
2626 if (cfqg->saved_workload_slice) {
2627 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2628 cfqd->serving_type = cfqg->saved_workload;
2629 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002630 } else
2631 cfqd->workload_expires = jiffies - 1;
2632
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002633 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002634}
2635
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002636/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002637 * Select a queue for service. If we have a current active queue,
2638 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002639 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002640static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002641{
Jens Axboea36e71f2009-04-15 12:15:11 +02002642 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002643
2644 cfqq = cfqd->active_queue;
2645 if (!cfqq)
2646 goto new_queue;
2647
Vivek Goyalf04a6422009-12-03 12:59:40 -05002648 if (!cfqd->rq_queued)
2649 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002650
2651 /*
2652 * We were waiting for group to get backlogged. Expire the queue
2653 */
2654 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2655 goto expire;
2656
Jens Axboe22e2c502005-06-27 10:55:12 +02002657 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002658 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002659 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002660 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2661 /*
2662 * If slice had not expired at the completion of last request
2663 * we might not have turned on wait_busy flag. Don't expire
2664 * the queue yet. Allow the group to get backlogged.
2665 *
2666 * The very fact that we have used the slice, that means we
2667 * have been idling all along on this queue and it should be
2668 * ok to wait for this request to complete.
2669 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002670 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2671 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2672 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002673 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002674 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002675 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002676 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002677
2678 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002679 * The active queue has requests and isn't expired, allow it to
2680 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002681 */
Jens Axboedd67d052006-06-21 09:36:18 +02002682 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002683 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002684
2685 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002686 * If another queue has a request waiting within our mean seek
2687 * distance, let it run. The expire code will check for close
2688 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002689 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002690 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002691 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002692 if (new_cfqq) {
2693 if (!cfqq->new_cfqq)
2694 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002695 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002696 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002697
2698 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002699 * No requests pending. If the active queue still has requests in
2700 * flight or is idling for a new request, allow either of these
2701 * conditions to happen (or time out) before selecting a new queue.
2702 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002703 if (timer_pending(&cfqd->idle_slice_timer)) {
2704 cfqq = NULL;
2705 goto keep_queue;
2706 }
2707
Shaohua Li8e1ac662010-11-08 15:01:04 +01002708 /*
2709 * This is a deep seek queue, but the device is much faster than
2710 * the queue can deliver, don't idle
2711 **/
2712 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2713 (cfq_cfqq_slice_new(cfqq) ||
2714 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2715 cfq_clear_cfqq_deep(cfqq);
2716 cfq_clear_cfqq_idle_window(cfqq);
2717 }
2718
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002719 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2720 cfqq = NULL;
2721 goto keep_queue;
2722 }
2723
2724 /*
2725 * If group idle is enabled and there are requests dispatched from
2726 * this group, wait for requests to complete.
2727 */
2728check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002729 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2730 cfqq->cfqg->dispatched &&
2731 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002732 cfqq = NULL;
2733 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002734 }
2735
Jens Axboe3b181522005-06-27 10:56:24 +02002736expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002737 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002738new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002739 /*
2740 * Current queue expired. Check if we have to switch to a new
2741 * service tree
2742 */
2743 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002744 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002745
Jens Axboea36e71f2009-04-15 12:15:11 +02002746 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002747keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002748 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002749}
2750
Jens Axboefebffd62008-01-28 13:19:43 +01002751static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002752{
2753 int dispatched = 0;
2754
2755 while (cfqq->next_rq) {
2756 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2757 dispatched++;
2758 }
2759
2760 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002761
2762 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002763 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002764 return dispatched;
2765}
2766
Jens Axboe498d3aa22007-04-26 12:54:48 +02002767/*
2768 * Drain our current requests. Used for barriers and when switching
2769 * io schedulers on-the-fly.
2770 */
Jens Axboed9e76202007-04-20 14:27:50 +02002771static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002772{
Jens Axboe08717142008-01-28 11:38:15 +01002773 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002774 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002775
Divyesh Shah3440c492010-04-09 09:29:57 +02002776 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002777 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002778 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2779 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002780 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002781 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002782
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002783 BUG_ON(cfqd->busy_queues);
2784
Jeff Moyer69237152009-06-12 15:29:30 +02002785 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002786 return dispatched;
2787}
2788
Shaohua Liabc3c742010-03-01 09:20:54 +01002789static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2790 struct cfq_queue *cfqq)
2791{
2792 /* the queue hasn't finished any request, can't estimate */
2793 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002794 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002795 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2796 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002797 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002798
Shaohua Lic1e44752010-11-08 15:01:02 +01002799 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002800}
2801
Jens Axboe0b182d62009-10-06 20:49:37 +02002802static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002803{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002804 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Jens Axboe2f5cb732009-04-07 08:51:19 +02002806 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002807 * Drain async requests before we start sync IO
2808 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002809 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002810 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002811
2812 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002813 * If this is an async queue and we have sync IO in flight, let it wait
2814 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002815 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002816 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002817
Shaohua Liabc3c742010-03-01 09:20:54 +01002818 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002819 if (cfq_class_idle(cfqq))
2820 max_dispatch = 1;
2821
2822 /*
2823 * Does this cfqq already have too much IO in flight?
2824 */
2825 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002826 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002827 /*
2828 * idle queue must always only have a single IO in flight
2829 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002830 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002831 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002832
Jens Axboe2f5cb732009-04-07 08:51:19 +02002833 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002834 * If there is only one sync queue
2835 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002836 * queue no dispatch limit. The reason is a sync queue can
2837 * preempt async queue, limiting the sync queue doesn't make
2838 * sense. This is useful for aiostress test.
2839 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002840 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2841 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002842
2843 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002844 * We have other queues, don't allow more IO from this one
2845 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002846 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2847 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002848 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002849
Jens Axboe2f5cb732009-04-07 08:51:19 +02002850 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002851 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002852 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002853 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002854 max_dispatch = -1;
2855 else
2856 /*
2857 * Normally we start throttling cfqq when cfq_quantum/2
2858 * requests have been dispatched. But we can drive
2859 * deeper queue depths at the beginning of slice
2860 * subjected to upper limit of cfq_quantum.
2861 * */
2862 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002863 }
2864
2865 /*
2866 * Async queues must wait a bit before being allowed dispatch.
2867 * We also ramp up the dispatch depth gradually for async IO,
2868 * based on the last sync IO we serviced
2869 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002870 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002871 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002872 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002873
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002874 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002875 if (!depth && !cfqq->dispatched)
2876 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002877 if (depth < max_dispatch)
2878 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880
Jens Axboe0b182d62009-10-06 20:49:37 +02002881 /*
2882 * If we're below the current max, allow a dispatch
2883 */
2884 return cfqq->dispatched < max_dispatch;
2885}
2886
2887/*
2888 * Dispatch a request from cfqq, moving them to the request queue
2889 * dispatch list.
2890 */
2891static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2892{
2893 struct request *rq;
2894
2895 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2896
2897 if (!cfq_may_dispatch(cfqd, cfqq))
2898 return false;
2899
2900 /*
2901 * follow expired path, else get first next available
2902 */
2903 rq = cfq_check_fifo(cfqq);
2904 if (!rq)
2905 rq = cfqq->next_rq;
2906
2907 /*
2908 * insert request into driver dispatch list
2909 */
2910 cfq_dispatch_insert(cfqd->queue, rq);
2911
2912 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002913 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002914
Tejun Heoc5869802011-12-14 00:33:41 +01002915 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002916 cfqd->active_cic = cic;
2917 }
2918
2919 return true;
2920}
2921
2922/*
2923 * Find the cfqq that we need to service and move a request from that to the
2924 * dispatch list
2925 */
2926static int cfq_dispatch_requests(struct request_queue *q, int force)
2927{
2928 struct cfq_data *cfqd = q->elevator->elevator_data;
2929 struct cfq_queue *cfqq;
2930
2931 if (!cfqd->busy_queues)
2932 return 0;
2933
2934 if (unlikely(force))
2935 return cfq_forced_dispatch(cfqd);
2936
2937 cfqq = cfq_select_queue(cfqd);
2938 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002939 return 0;
2940
Jens Axboe2f5cb732009-04-07 08:51:19 +02002941 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002942 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002943 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002944 if (!cfq_dispatch_request(cfqd, cfqq))
2945 return 0;
2946
Jens Axboe2f5cb732009-04-07 08:51:19 +02002947 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002948 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002949
2950 /*
2951 * expire an async queue immediately if it has used up its slice. idle
2952 * queue always expire after 1 dispatch round.
2953 */
2954 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2955 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2956 cfq_class_idle(cfqq))) {
2957 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002958 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002959 }
2960
Shan Weib217a902009-09-01 10:06:42 +02002961 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002962 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965/*
Jens Axboe5e705372006-07-13 12:39:25 +02002966 * task holds one reference to the queue, dropped when task exits. each rq
2967 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002969 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * queue lock must be held here.
2971 */
2972static void cfq_put_queue(struct cfq_queue *cfqq)
2973{
Jens Axboe22e2c502005-06-27 10:55:12 +02002974 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002975 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002976
Shaohua Li30d7b942011-01-07 08:46:59 +01002977 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Shaohua Li30d7b942011-01-07 08:46:59 +01002979 cfqq->ref--;
2980 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return;
2982
Jens Axboe7b679132008-05-30 12:23:07 +02002983 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002985 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002986 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002988 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002989 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002990 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002991 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002992
Vivek Goyalf04a6422009-12-03 12:59:40 -05002993 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01002995 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Shaohua Lid02a2c02010-05-25 10:16:53 +02002998static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002999{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003000 struct cfq_queue *__cfqq, *next;
3001
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003002 /*
3003 * If this queue was scheduled to merge with another queue, be
3004 * sure to drop the reference taken on that queue (and others in
3005 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3006 */
3007 __cfqq = cfqq->new_cfqq;
3008 while (__cfqq) {
3009 if (__cfqq == cfqq) {
3010 WARN(1, "cfqq->new_cfqq loop detected\n");
3011 break;
3012 }
3013 next = __cfqq->new_cfqq;
3014 cfq_put_queue(__cfqq);
3015 __cfqq = next;
3016 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003017}
3018
3019static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3020{
3021 if (unlikely(cfqq == cfqd->active_queue)) {
3022 __cfq_slice_expired(cfqd, cfqq, 0);
3023 cfq_schedule_dispatch(cfqd);
3024 }
3025
3026 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003027
Jens Axboe89850f72006-07-22 16:48:31 +02003028 cfq_put_queue(cfqq);
3029}
3030
Tejun Heo9b84cac2011-12-14 00:33:42 +01003031static void cfq_init_icq(struct io_cq *icq)
3032{
3033 struct cfq_io_cq *cic = icq_to_cic(icq);
3034
3035 cic->ttime.last_end_request = jiffies;
3036}
3037
Tejun Heoc5869802011-12-14 00:33:41 +01003038static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003039{
Tejun Heoc5869802011-12-14 00:33:41 +01003040 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003041 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003042
Jens Axboeff6657c2009-04-08 10:58:57 +02003043 if (cic->cfqq[BLK_RW_ASYNC]) {
3044 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3045 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003046 }
3047
Jens Axboeff6657c2009-04-08 10:58:57 +02003048 if (cic->cfqq[BLK_RW_SYNC]) {
3049 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3050 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003051 }
Jens Axboe89850f72006-07-22 16:48:31 +02003052}
3053
Tejun Heoabede6d2012-03-19 15:10:57 -07003054static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003055{
3056 struct task_struct *tsk = current;
3057 int ioprio_class;
3058
Jens Axboe3b181522005-06-27 10:56:24 +02003059 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003060 return;
3061
Tejun Heo598971b2012-03-19 15:10:58 -07003062 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003063 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003064 default:
3065 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3066 case IOPRIO_CLASS_NONE:
3067 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003068 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003069 */
3070 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003071 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003072 break;
3073 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003074 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003075 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3076 break;
3077 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003078 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003079 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3080 break;
3081 case IOPRIO_CLASS_IDLE:
3082 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3083 cfqq->ioprio = 7;
3084 cfq_clear_cfqq_idle_window(cfqq);
3085 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003086 }
3087
3088 /*
3089 * keep track of original prio settings in case we have to temporarily
3090 * elevate the priority of this queue
3091 */
3092 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003093 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003094}
3095
Tejun Heo598971b2012-03-19 15:10:58 -07003096static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003097{
Tejun Heo598971b2012-03-19 15:10:58 -07003098 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003099 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003100 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003101
Tejun Heo598971b2012-03-19 15:10:58 -07003102 /*
3103 * Check whether ioprio has changed. The condition may trigger
3104 * spuriously on a newly created cic but there's no harm.
3105 */
3106 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003107 return;
3108
Jens Axboeff6657c2009-04-08 10:58:57 +02003109 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003110 if (cfqq) {
3111 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003112 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3113 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003114 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003115 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003116 cfq_put_queue(cfqq);
3117 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003118 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003119
Jens Axboeff6657c2009-04-08 10:58:57 +02003120 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003121 if (cfqq)
3122 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003123
3124 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003125}
3126
Jens Axboed5036d72009-06-26 10:44:34 +02003127static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003128 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003129{
3130 RB_CLEAR_NODE(&cfqq->rb_node);
3131 RB_CLEAR_NODE(&cfqq->p_node);
3132 INIT_LIST_HEAD(&cfqq->fifo);
3133
Shaohua Li30d7b942011-01-07 08:46:59 +01003134 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003135 cfqq->cfqd = cfqd;
3136
3137 cfq_mark_cfqq_prio_changed(cfqq);
3138
3139 if (is_sync) {
3140 if (!cfq_class_idle(cfqq))
3141 cfq_mark_cfqq_idle_window(cfqq);
3142 cfq_mark_cfqq_sync(cfqq);
3143 }
3144 cfqq->pid = pid;
3145}
3146
Vivek Goyal246103332009-12-03 12:59:51 -05003147#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003148static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003149{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003150 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003151 struct cfq_queue *sync_cfqq;
3152 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003153
Tejun Heo598971b2012-03-19 15:10:58 -07003154 rcu_read_lock();
3155 id = bio_blkio_cgroup(bio)->id;
3156 rcu_read_unlock();
3157
3158 /*
3159 * Check whether blkcg has changed. The condition may trigger
3160 * spuriously on a newly created cic but there's no harm.
3161 */
3162 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003163 return;
3164
Tejun Heo598971b2012-03-19 15:10:58 -07003165 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003166 if (sync_cfqq) {
3167 /*
3168 * Drop reference to sync queue. A new sync queue will be
3169 * assigned in new group upon arrival of a fresh request.
3170 */
3171 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3172 cic_set_cfqq(cic, NULL, 1);
3173 cfq_put_queue(sync_cfqq);
3174 }
Tejun Heo598971b2012-03-19 15:10:58 -07003175
3176 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003177}
Tejun Heo598971b2012-03-19 15:10:58 -07003178#else
3179static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003180#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003183cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3184 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08003186 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003188 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003191 rcu_read_lock();
3192
Tejun Heo4f85cb92012-03-05 13:15:28 -08003193 blkcg = bio_blkio_cgroup(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003194 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003195 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Jens Axboe6118b702009-06-30 09:34:12 +02003197 /*
3198 * Always try a new alloc if we fell back to the OOM cfqq
3199 * originally, since it should just be a temporary situation.
3200 */
3201 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3202 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (new_cfqq) {
3204 cfqq = new_cfqq;
3205 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003206 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003209 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003210 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003211 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003213 if (new_cfqq)
3214 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003215 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003216 cfqq = kmem_cache_alloc_node(cfq_pool,
3217 gfp_mask | __GFP_ZERO,
3218 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Jens Axboe6118b702009-06-30 09:34:12 +02003221 if (cfqq) {
3222 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003223 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003224 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003225 cfq_log_cfqq(cfqd, cfqq, "alloced");
3226 } else
3227 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
3229
3230 if (new_cfqq)
3231 kmem_cache_free(cfq_pool, new_cfqq);
3232
Tejun Heo2a7f1242012-03-05 13:15:01 -08003233 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return cfqq;
3235}
3236
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003237static struct cfq_queue **
3238cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3239{
Jens Axboefe094d92008-01-31 13:08:54 +01003240 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003241 case IOPRIO_CLASS_RT:
3242 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003243 case IOPRIO_CLASS_NONE:
3244 ioprio = IOPRIO_NORM;
3245 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003246 case IOPRIO_CLASS_BE:
3247 return &cfqd->async_cfqq[1][ioprio];
3248 case IOPRIO_CLASS_IDLE:
3249 return &cfqd->async_idle_cfqq;
3250 default:
3251 BUG();
3252 }
3253}
3254
Jens Axboe15c31be2007-07-10 13:43:25 +02003255static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003256cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003257 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003258{
Tejun Heo598971b2012-03-19 15:10:58 -07003259 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3260 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003261 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003262 struct cfq_queue *cfqq = NULL;
3263
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003264 if (!is_sync) {
3265 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3266 cfqq = *async_cfqq;
3267 }
3268
Jens Axboe6118b702009-06-30 09:34:12 +02003269 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003270 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003271
3272 /*
3273 * pin the queue now that it's allocated, scheduler exit will prune it
3274 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003275 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003276 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003277 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003278 }
3279
Shaohua Li30d7b942011-01-07 08:46:59 +01003280 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003281 return cfqq;
3282}
3283
Jens Axboe22e2c502005-06-27 10:55:12 +02003284static void
Shaohua Li383cd722011-07-12 14:24:35 +02003285__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003286{
Shaohua Li383cd722011-07-12 14:24:35 +02003287 unsigned long elapsed = jiffies - ttime->last_end_request;
3288 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003289
Shaohua Li383cd722011-07-12 14:24:35 +02003290 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3291 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3292 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3293}
3294
3295static void
3296cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003297 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003298{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003299 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003300 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003301 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3302 cfqd->cfq_slice_idle);
3303 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003304#ifdef CONFIG_CFQ_GROUP_IOSCHED
3305 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3306#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003307}
3308
Jens Axboe206dc692006-03-28 13:03:44 +02003309static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003310cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003311 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003312{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003313 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003314 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003315 if (cfqq->last_request_pos) {
3316 if (cfqq->last_request_pos < blk_rq_pos(rq))
3317 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3318 else
3319 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3320 }
Jens Axboe206dc692006-03-28 13:03:44 +02003321
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003322 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003323 if (blk_queue_nonrot(cfqd->queue))
3324 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3325 else
3326 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003327}
Jens Axboe22e2c502005-06-27 10:55:12 +02003328
3329/*
3330 * Disable idle window if the process thinks too long or seeks so much that
3331 * it doesn't matter
3332 */
3333static void
3334cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003335 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003336{
Jens Axboe7b679132008-05-30 12:23:07 +02003337 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003338
Jens Axboe08717142008-01-28 11:38:15 +01003339 /*
3340 * Don't idle for async or idle io prio class
3341 */
3342 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003343 return;
3344
Jens Axboec265a7f2008-06-26 13:49:33 +02003345 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003346
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003347 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3348 cfq_mark_cfqq_deep(cfqq);
3349
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003350 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3351 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003352 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003353 !cfqd->cfq_slice_idle ||
3354 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003355 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003356 else if (sample_valid(cic->ttime.ttime_samples)) {
3357 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003358 enable_idle = 0;
3359 else
3360 enable_idle = 1;
3361 }
3362
Jens Axboe7b679132008-05-30 12:23:07 +02003363 if (old_idle != enable_idle) {
3364 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3365 if (enable_idle)
3366 cfq_mark_cfqq_idle_window(cfqq);
3367 else
3368 cfq_clear_cfqq_idle_window(cfqq);
3369 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003370}
3371
Jens Axboe22e2c502005-06-27 10:55:12 +02003372/*
3373 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3374 * no or if we aren't sure, a 1 will cause a preempt.
3375 */
Jens Axboea6151c32009-10-07 20:02:57 +02003376static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003377cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003378 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003379{
Jens Axboe6d048f52007-04-25 12:44:27 +02003380 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003381
Jens Axboe6d048f52007-04-25 12:44:27 +02003382 cfqq = cfqd->active_queue;
3383 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003384 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003385
Jens Axboe6d048f52007-04-25 12:44:27 +02003386 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003387 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003388
3389 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003390 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003391
Jens Axboe22e2c502005-06-27 10:55:12 +02003392 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003393 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3394 */
3395 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3396 return false;
3397
3398 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003399 * if the new request is sync, but the currently running queue is
3400 * not, let the sync request have priority.
3401 */
Jens Axboe5e705372006-07-13 12:39:25 +02003402 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003403 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003404
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003405 if (new_cfqq->cfqg != cfqq->cfqg)
3406 return false;
3407
3408 if (cfq_slice_used(cfqq))
3409 return true;
3410
3411 /* Allow preemption only if we are idling on sync-noidle tree */
3412 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3413 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3414 new_cfqq->service_tree->count == 2 &&
3415 RB_EMPTY_ROOT(&cfqq->sort_list))
3416 return true;
3417
Jens Axboe374f84a2006-07-23 01:42:19 +02003418 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003419 * So both queues are sync. Let the new request get disk time if
3420 * it's a metadata request and the current queue is doing regular IO.
3421 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003422 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003423 return true;
3424
3425 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003426 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3427 */
3428 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003429 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003430
Shaohua Lid2d59e12010-11-08 15:01:03 +01003431 /* An idle queue should not be idle now for some reason */
3432 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3433 return true;
3434
Jens Axboe1e3335d2007-02-14 19:59:49 +01003435 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003436 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003437
3438 /*
3439 * if this request is as-good as one we would expect from the
3440 * current cfqq, let it preempt
3441 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003442 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003443 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003444
Jens Axboea6151c32009-10-07 20:02:57 +02003445 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003446}
3447
3448/*
3449 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3450 * let it have half of its nominal slice.
3451 */
3452static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3453{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003454 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3455
Jens Axboe7b679132008-05-30 12:23:07 +02003456 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003457 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003458
Jens Axboebf572252006-07-19 20:29:12 +02003459 /*
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003460 * workload type is changed, don't save slice, otherwise preempt
3461 * doesn't happen
3462 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003463 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003464 cfqq->cfqg->saved_workload_slice = 0;
3465
3466 /*
Jens Axboebf572252006-07-19 20:29:12 +02003467 * Put the new queue at the front of the of the current list,
3468 * so we know that it will be selected next.
3469 */
3470 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003471
3472 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003473
Justin TerAvest62a37f62011-03-23 08:25:44 +01003474 cfqq->slice_end = 0;
3475 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003476}
3477
3478/*
Jens Axboe5e705372006-07-13 12:39:25 +02003479 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003480 * something we should do about it
3481 */
3482static void
Jens Axboe5e705372006-07-13 12:39:25 +02003483cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3484 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003485{
Tejun Heoc5869802011-12-14 00:33:41 +01003486 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003487
Aaron Carroll45333d52008-08-26 15:52:36 +02003488 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003489 if (rq->cmd_flags & REQ_PRIO)
3490 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003491
Shaohua Li383cd722011-07-12 14:24:35 +02003492 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003493 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003494 cfq_update_idle_window(cfqd, cfqq, cic);
3495
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003496 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003497
3498 if (cfqq == cfqd->active_queue) {
3499 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003500 * Remember that we saw a request from this process, but
3501 * don't start queuing just yet. Otherwise we risk seeing lots
3502 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003503 * and merging. If the request is already larger than a single
3504 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003505 * merging is already done. Ditto for a busy system that
3506 * has other work pending, don't risk delaying until the
3507 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003508 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003509 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003510 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3511 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003512 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003513 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003514 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003515 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003516 cfq_blkiocg_update_idle_time_stats(
Tejun Heoc1768262012-03-05 13:15:17 -08003517 cfqg_to_blkg(cfqq->cfqg),
3518 &blkio_policy_cfq);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003519 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003520 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003521 }
Jens Axboe5e705372006-07-13 12:39:25 +02003522 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003523 /*
3524 * not the active queue - expire current slice if it is
3525 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003526 * has some old slice time left and is of higher priority or
3527 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003528 */
3529 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003530 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003531 }
3532}
3533
Jens Axboe165125e2007-07-24 09:28:11 +02003534static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003535{
Jens Axboeb4878f22005-10-20 16:42:29 +02003536 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003537 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003538
Jens Axboe7b679132008-05-30 12:23:07 +02003539 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003540 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Jens Axboe30996f42009-10-05 11:03:39 +02003542 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003543 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003544 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003545 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08003546 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08003547 cfqg_to_blkg(cfqd->serving_group),
3548 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003549 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Aaron Carroll45333d52008-08-26 15:52:36 +02003552/*
3553 * Update hw_tag based on peak queue depth over 50 samples under
3554 * sufficient load.
3555 */
3556static void cfq_update_hw_tag(struct cfq_data *cfqd)
3557{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003558 struct cfq_queue *cfqq = cfqd->active_queue;
3559
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003560 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3561 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003562
3563 if (cfqd->hw_tag == 1)
3564 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003565
3566 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003567 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003568 return;
3569
Shaohua Li1a1238a2009-10-27 08:46:23 +01003570 /*
3571 * If active queue hasn't enough requests and can idle, cfq might not
3572 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3573 * case
3574 */
3575 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3576 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003577 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003578 return;
3579
Aaron Carroll45333d52008-08-26 15:52:36 +02003580 if (cfqd->hw_tag_samples++ < 50)
3581 return;
3582
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003583 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003584 cfqd->hw_tag = 1;
3585 else
3586 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003587}
3588
Vivek Goyal7667aa02009-12-08 17:52:58 -05003589static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3590{
Tejun Heoc5869802011-12-14 00:33:41 +01003591 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003592
Justin TerAvest02a8f012011-02-09 14:20:03 +01003593 /* If the queue already has requests, don't wait */
3594 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3595 return false;
3596
Vivek Goyal7667aa02009-12-08 17:52:58 -05003597 /* If there are other queues in the group, don't wait */
3598 if (cfqq->cfqg->nr_cfqq > 1)
3599 return false;
3600
Shaohua Li7700fc42011-07-12 14:24:56 +02003601 /* the only queue in the group, but think time is big */
3602 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3603 return false;
3604
Vivek Goyal7667aa02009-12-08 17:52:58 -05003605 if (cfq_slice_used(cfqq))
3606 return true;
3607
3608 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003609 if (cic && sample_valid(cic->ttime.ttime_samples)
3610 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003611 return true;
3612
3613 /*
3614 * If think times is less than a jiffy than ttime_mean=0 and above
3615 * will not be true. It might happen that slice has not expired yet
3616 * but will expire soon (4-5 ns) during select_queue(). To cover the
3617 * case where think time is less than a jiffy, mark the queue wait
3618 * busy if only 1 jiffy is left in the slice.
3619 */
3620 if (cfqq->slice_end - jiffies == 1)
3621 return true;
3622
3623 return false;
3624}
3625
Jens Axboe165125e2007-07-24 09:28:11 +02003626static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Jens Axboe5e705372006-07-13 12:39:25 +02003628 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003629 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003630 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003631 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Jens Axboeb4878f22005-10-20 16:42:29 +02003633 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003634 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3635 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Aaron Carroll45333d52008-08-26 15:52:36 +02003637 cfq_update_hw_tag(cfqd);
3638
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003639 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003640 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003641 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003642 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003643 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003644 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08003645 &blkio_policy_cfq, rq_start_time_ns(rq),
3646 rq_io_start_time_ns(rq), rq_data_dir(rq),
3647 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003649 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003650
Vivek Goyal365722b2009-10-03 15:21:27 +02003651 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003652 struct cfq_rb_root *service_tree;
3653
Shaohua Li383cd722011-07-12 14:24:35 +02003654 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003655
3656 if (cfq_cfqq_on_rr(cfqq))
3657 service_tree = cfqq->service_tree;
3658 else
3659 service_tree = service_tree_for(cfqq->cfqg,
3660 cfqq_prio(cfqq), cfqq_type(cfqq));
3661 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003662 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3663 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003664 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003665
Shaohua Li7700fc42011-07-12 14:24:56 +02003666#ifdef CONFIG_CFQ_GROUP_IOSCHED
3667 cfqq->cfqg->ttime.last_end_request = now;
3668#endif
3669
Jens Axboecaaa5f92006-06-16 11:23:00 +02003670 /*
3671 * If this is the active queue, check if it needs to be expired,
3672 * or if we want to idle in case it has no pending requests.
3673 */
3674 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003675 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3676
Jens Axboe44f7c162007-01-19 11:51:58 +11003677 if (cfq_cfqq_slice_new(cfqq)) {
3678 cfq_set_prio_slice(cfqd, cfqq);
3679 cfq_clear_cfqq_slice_new(cfqq);
3680 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003681
3682 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003683 * Should we wait for next request to come in before we expire
3684 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003685 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003686 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003687 unsigned long extend_sl = cfqd->cfq_slice_idle;
3688 if (!cfqd->cfq_slice_idle)
3689 extend_sl = cfqd->cfq_group_idle;
3690 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003691 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003692 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003693 }
3694
Jens Axboea36e71f2009-04-15 12:15:11 +02003695 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003696 * Idling is not enabled on:
3697 * - expired queues
3698 * - idle-priority queues
3699 * - async queues
3700 * - queues with still some requests queued
3701 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003702 */
Jens Axboe08717142008-01-28 11:38:15 +01003703 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003704 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003705 else if (sync && cfqq_empty &&
3706 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003707 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003708 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003709 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003710
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003711 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003712 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Jens Axboe89850f72006-07-22 16:48:31 +02003715static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003716{
Jens Axboe1b379d82009-08-11 08:26:11 +02003717 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003718 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003719 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003720 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003721
3722 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003723}
3724
Jens Axboe165125e2007-07-24 09:28:11 +02003725static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003726{
3727 struct cfq_data *cfqd = q->elevator->elevator_data;
3728 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003729 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003730 struct cfq_queue *cfqq;
3731
3732 /*
3733 * don't force setup of a queue from here, as a call to may_queue
3734 * does not necessarily imply that a request actually will be queued.
3735 * so just lookup a possibly existing queue, or return 'may queue'
3736 * if that fails
3737 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003738 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003739 if (!cic)
3740 return ELV_MQUEUE_MAY;
3741
Jens Axboeb0b78f82009-04-08 10:56:08 +02003742 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003743 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003744 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003745
Jens Axboe89850f72006-07-22 16:48:31 +02003746 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003747 }
3748
3749 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752/*
3753 * queue lock held here
3754 */
Jens Axboebb37b942006-12-01 10:42:33 +01003755static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Jens Axboe5e705372006-07-13 12:39:25 +02003757 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Jens Axboe5e705372006-07-13 12:39:25 +02003759 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003760 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Jens Axboe22e2c502005-06-27 10:55:12 +02003762 BUG_ON(!cfqq->allocated[rw]);
3763 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003765 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003766 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003767 rq->elv.priv[0] = NULL;
3768 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 cfq_put_queue(cfqq);
3771 }
3772}
3773
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003774static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003775cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003776 struct cfq_queue *cfqq)
3777{
3778 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3779 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003780 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003781 cfq_put_queue(cfqq);
3782 return cic_to_cfqq(cic, 1);
3783}
3784
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003785/*
3786 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3787 * was the last process referring to said cfqq.
3788 */
3789static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003790split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003791{
3792 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003793 cfqq->pid = current->pid;
3794 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003795 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003796 return cfqq;
3797 }
3798
3799 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003800
3801 cfq_put_cooperator(cfqq);
3802
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003803 cfq_put_queue(cfqq);
3804 return NULL;
3805}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003807 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003809static int
Tejun Heo852c7882012-03-05 13:15:27 -08003810cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
3811 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003814 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003816 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003817 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 might_sleep_if(gfp_mask & __GFP_WAIT);
3820
Tejun Heo216284c2011-12-14 00:33:38 +01003821 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003822
Tejun Heo598971b2012-03-19 15:10:58 -07003823 check_ioprio_changed(cic, bio);
3824 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003825new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003826 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003827 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003828 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003829 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003830 } else {
3831 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003832 * If the queue was seeky for too long, break it apart.
3833 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003834 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003835 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3836 cfqq = split_cfqq(cic, cfqq);
3837 if (!cfqq)
3838 goto new_queue;
3839 }
3840
3841 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003842 * Check to see if this queue is scheduled to merge with
3843 * another, closely cooperating queue. The merging of
3844 * queues happens here as it must be done in process context.
3845 * The reference on new_cfqq was taken in merge_cfqqs.
3846 */
3847 if (cfqq->new_cfqq)
3848 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003852
Jens Axboe6fae9c22011-03-01 15:04:39 -05003853 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003854 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01003855 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003856 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01003857 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
David Howells65f27f32006-11-22 14:55:48 +00003861static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003862{
David Howells65f27f32006-11-22 14:55:48 +00003863 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003864 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003865 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003866
Jens Axboe40bb54d2009-04-15 12:11:10 +02003867 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003868 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003869 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003870}
3871
3872/*
3873 * Timer running if the active_queue is currently idling inside its time slice
3874 */
3875static void cfq_idle_slice_timer(unsigned long data)
3876{
3877 struct cfq_data *cfqd = (struct cfq_data *) data;
3878 struct cfq_queue *cfqq;
3879 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003880 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003881
Jens Axboe7b679132008-05-30 12:23:07 +02003882 cfq_log(cfqd, "idle timer fired");
3883
Jens Axboe22e2c502005-06-27 10:55:12 +02003884 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3885
Jens Axboefe094d92008-01-31 13:08:54 +01003886 cfqq = cfqd->active_queue;
3887 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003888 timed_out = 0;
3889
Jens Axboe22e2c502005-06-27 10:55:12 +02003890 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003891 * We saw a request before the queue expired, let it through
3892 */
3893 if (cfq_cfqq_must_dispatch(cfqq))
3894 goto out_kick;
3895
3896 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003897 * expired
3898 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003899 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003900 goto expire;
3901
3902 /*
3903 * only expire and reinvoke request handler, if there are
3904 * other queues with pending requests
3905 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003906 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003907 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003908
3909 /*
3910 * not expired and it has a request pending, let it dispatch
3911 */
Jens Axboe75e50982009-04-07 08:56:14 +02003912 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003913 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003914
3915 /*
3916 * Queue depth flag is reset only when the idle didn't succeed
3917 */
3918 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003919 }
3920expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003921 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003922out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003923 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003924out_cont:
3925 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3926}
3927
Jens Axboe3b181522005-06-27 10:56:24 +02003928static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3929{
3930 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003931 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003932}
Jens Axboe22e2c502005-06-27 10:55:12 +02003933
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003934static void cfq_put_async_queues(struct cfq_data *cfqd)
3935{
3936 int i;
3937
3938 for (i = 0; i < IOPRIO_BE_NR; i++) {
3939 if (cfqd->async_cfqq[0][i])
3940 cfq_put_queue(cfqd->async_cfqq[0][i]);
3941 if (cfqd->async_cfqq[1][i])
3942 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003943 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003944
3945 if (cfqd->async_idle_cfqq)
3946 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003947}
3948
Jens Axboeb374d182008-10-31 10:05:07 +01003949static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Jens Axboe22e2c502005-06-27 10:55:12 +02003951 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003952 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003953
Jens Axboe3b181522005-06-27 10:56:24 +02003954 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003955
Al Virod9ff4182006-03-18 13:51:22 -05003956 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003957
Al Virod9ff4182006-03-18 13:51:22 -05003958 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003959 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003960
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003961 cfq_put_async_queues(cfqd);
Tejun Heo03aa264a2012-03-05 13:15:19 -08003962
3963 spin_unlock_irq(q->queue_lock);
3964
Al Viroa90d7422006-03-18 12:05:37 -05003965 cfq_shutdown_timer_wq(cfqd);
3966
Tejun Heof51b8022012-03-05 13:15:05 -08003967#ifndef CONFIG_CFQ_GROUP_IOSCHED
3968 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003969#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -08003970 update_root_blkg_pd(q, BLKIO_POLICY_PROP);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003971 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Tejun Heob2fab5a2012-03-05 13:14:57 -08003974static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
3976 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003977 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003978 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Christoph Lameter94f60302007-07-17 04:03:29 -07003980 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003981 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003982 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003983
Tejun Heof51b8022012-03-05 13:15:05 -08003984 cfqd->queue = q;
3985 q->elevator->elevator_data = cfqd;
3986
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003987 /* Init root service tree */
3988 cfqd->grp_service_tree = CFQ_RB_ROOT;
3989
Tejun Heof51b8022012-03-05 13:15:05 -08003990 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05003991#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08003992 rcu_read_lock();
3993 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003994
Tejun Heoaaec55a2012-04-01 14:38:42 -07003995 blkg = blkg_lookup_create(&blkio_root_cgroup, q, true);
Tejun Heocd1604f2012-03-05 13:15:06 -08003996 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08003997 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08003998
3999 spin_unlock_irq(q->queue_lock);
4000 rcu_read_unlock();
4001#else
4002 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4003 GFP_KERNEL, cfqd->queue->node);
4004 if (cfqd->root_group)
4005 cfq_init_cfqg_base(cfqd->root_group);
4006#endif
4007 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004008 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08004009 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004010 }
4011
Tejun Heof51b8022012-03-05 13:15:05 -08004012 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004013
Jens Axboe26a2ac02009-04-23 12:13:27 +02004014 /*
4015 * Not strictly needed (since RB_ROOT just clears the node and we
4016 * zeroed cfqd on alloc), but better be safe in case someone decides
4017 * to add magic to the rb code
4018 */
4019 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4020 cfqd->prio_trees[i] = RB_ROOT;
4021
Jens Axboe6118b702009-06-30 09:34:12 +02004022 /*
4023 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4024 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004025 * will not attempt to free it. oom_cfqq is linked to root_group
4026 * but shouldn't hold a reference as it'll never be unlinked. Lose
4027 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004028 */
4029 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004030 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004031
4032 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004033 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004034 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004035 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Jens Axboe22e2c502005-06-27 10:55:12 +02004037 init_timer(&cfqd->idle_slice_timer);
4038 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4039 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4040
Jens Axboe23e018a2009-10-05 08:52:35 +02004041 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004044 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4045 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 cfqd->cfq_back_max = cfq_back_max;
4047 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004048 cfqd->cfq_slice[0] = cfq_slice_async;
4049 cfqd->cfq_slice[1] = cfq_slice_sync;
4050 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4051 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004052 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004053 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004054 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004055 /*
4056 * we optimistically start assuming sync ops weren't delayed in last
4057 * second, in order to have larger depth for async operations.
4058 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004059 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063/*
4064 * sysfs parts below -->
4065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066static ssize_t
4067cfq_var_show(unsigned int var, char *page)
4068{
4069 return sprintf(page, "%d\n", var);
4070}
4071
4072static ssize_t
4073cfq_var_store(unsigned int *var, const char *page, size_t count)
4074{
4075 char *p = (char *) page;
4076
4077 *var = simple_strtoul(p, &p, 10);
4078 return count;
4079}
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004082static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004084 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 unsigned int __data = __VAR; \
4086 if (__CONV) \
4087 __data = jiffies_to_msecs(__data); \
4088 return cfq_var_show(__data, (page)); \
4089}
4090SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004091SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4092SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004093SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4094SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004095SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004096SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004097SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4098SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4099SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004100SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101#undef SHOW_FUNCTION
4102
4103#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004104static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004106 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 unsigned int __data; \
4108 int ret = cfq_var_store(&__data, (page), count); \
4109 if (__data < (MIN)) \
4110 __data = (MIN); \
4111 else if (__data > (MAX)) \
4112 __data = (MAX); \
4113 if (__CONV) \
4114 *(__PTR) = msecs_to_jiffies(__data); \
4115 else \
4116 *(__PTR) = __data; \
4117 return ret; \
4118}
4119STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004120STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4121 UINT_MAX, 1);
4122STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4123 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004124STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004125STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4126 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004127STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004128STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004129STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4130STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004131STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4132 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004133STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134#undef STORE_FUNCTION
4135
Al Viroe572ec72006-03-18 22:27:18 -05004136#define CFQ_ATTR(name) \
4137 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004138
Al Viroe572ec72006-03-18 22:27:18 -05004139static struct elv_fs_entry cfq_attrs[] = {
4140 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004141 CFQ_ATTR(fifo_expire_sync),
4142 CFQ_ATTR(fifo_expire_async),
4143 CFQ_ATTR(back_seek_max),
4144 CFQ_ATTR(back_seek_penalty),
4145 CFQ_ATTR(slice_sync),
4146 CFQ_ATTR(slice_async),
4147 CFQ_ATTR(slice_async_rq),
4148 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004149 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004150 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004151 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152};
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154static struct elevator_type iosched_cfq = {
4155 .ops = {
4156 .elevator_merge_fn = cfq_merge,
4157 .elevator_merged_fn = cfq_merged_request,
4158 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004159 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004160 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004161 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004163 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004166 .elevator_former_req_fn = elv_rb_former_request,
4167 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004168 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004169 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 .elevator_set_req_fn = cfq_set_request,
4171 .elevator_put_req_fn = cfq_put_request,
4172 .elevator_may_queue_fn = cfq_may_queue,
4173 .elevator_init_fn = cfq_init_queue,
4174 .elevator_exit_fn = cfq_exit_queue,
4175 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004176 .icq_size = sizeof(struct cfq_io_cq),
4177 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004178 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004179 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 .elevator_owner = THIS_MODULE,
4181};
4182
Vivek Goyal3e252062009-12-04 10:36:42 -05004183#ifdef CONFIG_CFQ_GROUP_IOSCHED
4184static struct blkio_policy_type blkio_policy_cfq = {
4185 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08004186 .blkio_init_group_fn = cfq_init_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05004187 },
Vivek Goyal062a6442010-09-15 17:06:33 -04004188 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08004189 .pdata_size = sizeof(struct cfq_group),
Tejun Heo60c2bc22012-04-01 14:38:43 -07004190 .cftypes = cfq_blkcg_files,
Vivek Goyal3e252062009-12-04 10:36:42 -05004191};
Vivek Goyal3e252062009-12-04 10:36:42 -05004192#endif
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194static int __init cfq_init(void)
4195{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004196 int ret;
4197
Jens Axboe22e2c502005-06-27 10:55:12 +02004198 /*
4199 * could be 0 on HZ < 1000 setups
4200 */
4201 if (!cfq_slice_async)
4202 cfq_slice_async = 1;
4203 if (!cfq_slice_idle)
4204 cfq_slice_idle = 1;
4205
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004206#ifdef CONFIG_CFQ_GROUP_IOSCHED
4207 if (!cfq_group_idle)
4208 cfq_group_idle = 1;
4209#else
4210 cfq_group_idle = 0;
4211#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01004212 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4213 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return -ENOMEM;
4215
Tejun Heo3d3c2372011-12-14 00:33:42 +01004216 ret = elv_register(&iosched_cfq);
4217 if (ret) {
4218 kmem_cache_destroy(cfq_pool);
4219 return ret;
4220 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01004221
Tejun Heob95ada52012-03-05 13:14:55 -08004222#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004223 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004224#endif
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
4228static void __exit cfq_exit(void)
4229{
Tejun Heob95ada52012-03-05 13:14:55 -08004230#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004231 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004234 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
4237module_init(cfq_init);
4238module_exit(cfq_exit);
4239
4240MODULE_AUTHOR("Jens Axboe");
4241MODULE_LICENSE("GPL");
4242MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");