blob: 49913804e8dd9da3f71e4062de3cff2916765657 [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 Heo629ed0b2012-04-01 14:38:44 -0700627 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
628 struct blkg_policy_data *pd = blkg->pd[pol->plid];
629 struct blkio_group_stats_cpu *stats_cpu;
630 unsigned long flags;
631
632 /* If per cpu stats are not allocated yet, don't do any accounting. */
633 if (pd->stats_cpu == NULL)
634 return;
635
636 /*
637 * Disabling interrupts to provide mutual exclusion between two
638 * writes on same cpu. It probably is not needed for 64bit. Not
639 * optimizing that case yet.
640 */
641 local_irq_save(flags);
642
643 stats_cpu = this_cpu_ptr(pd->stats_cpu);
644
645 blkg_stat_add(&stats_cpu->sectors, bytes >> 9);
646 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
647 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
648
649 local_irq_restore(flags);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700650}
651
652static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
653 struct blkio_policy_type *pol, uint64_t start_time,
654 uint64_t io_start_time, bool direction, bool sync)
655{
Tejun Heo629ed0b2012-04-01 14:38:44 -0700656 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
657 unsigned long long now = sched_clock();
658 int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0);
659
660 lockdep_assert_held(blkg->q->queue_lock);
661
662 if (time_after64(now, io_start_time))
663 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
664 if (time_after64(io_start_time, start_time))
665 blkg_rwstat_add(&stats->wait_time, rw,
666 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700667}
668
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100669#else /* CONFIG_CFQ_GROUP_IOSCHED */
670
671static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg) { return NULL; }
672static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg) { return NULL; }
673static inline void cfqg_get(struct cfq_group *cfqg) { }
674static inline void cfqg_put(struct cfq_group *cfqg) { }
675
Jens Axboe7b679132008-05-30 12:23:07 +0200676#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
677 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200678#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100679
Tejun Heo2ce4d502012-04-01 14:38:43 -0700680static inline void cfq_blkiocg_update_io_add_stats(struct blkio_group *blkg,
681 struct blkio_policy_type *pol,
682 struct blkio_group *curr_blkg, bool direction,
683 bool sync) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700684static inline void cfq_blkiocg_update_timeslice_used(struct blkio_group *blkg,
685 struct blkio_policy_type *pol, unsigned long time,
686 unsigned long unaccounted_time) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700687static inline void cfq_blkiocg_update_io_remove_stats(struct blkio_group *blkg,
688 struct blkio_policy_type *pol, bool direction,
689 bool sync) { }
690static inline void cfq_blkiocg_update_io_merged_stats(struct blkio_group *blkg,
691 struct blkio_policy_type *pol, bool direction,
692 bool sync) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700693static inline void cfq_blkiocg_update_dispatch_stats(struct blkio_group *blkg,
694 struct blkio_policy_type *pol, uint64_t bytes,
695 bool direction, bool sync) { }
696static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
697 struct blkio_policy_type *pol, uint64_t start_time,
698 uint64_t io_start_time, bool direction, bool sync) { }
699
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100700#endif /* CONFIG_CFQ_GROUP_IOSCHED */
701
Jens Axboe7b679132008-05-30 12:23:07 +0200702#define cfq_log(cfqd, fmt, args...) \
703 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
704
Vivek Goyal615f0252009-12-03 12:59:39 -0500705/* Traverses through cfq group service trees */
706#define for_each_cfqg_st(cfqg, i, j, st) \
707 for (i = 0; i <= IDLE_WORKLOAD; i++) \
708 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
709 : &cfqg->service_tree_idle; \
710 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
711 (i == IDLE_WORKLOAD && j == 0); \
712 j++, st = i < IDLE_WORKLOAD ? \
713 &cfqg->service_trees[i][j]: NULL) \
714
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200715static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
716 struct cfq_ttime *ttime, bool group_idle)
717{
718 unsigned long slice;
719 if (!sample_valid(ttime->ttime_samples))
720 return false;
721 if (group_idle)
722 slice = cfqd->cfq_group_idle;
723 else
724 slice = cfqd->cfq_slice_idle;
725 return ttime->ttime_mean > slice;
726}
Vivek Goyal615f0252009-12-03 12:59:39 -0500727
Vivek Goyal02b35082010-08-23 12:23:53 +0200728static inline bool iops_mode(struct cfq_data *cfqd)
729{
730 /*
731 * If we are not idling on queues and it is a NCQ drive, parallel
732 * execution of requests is on and measuring time is not possible
733 * in most of the cases until and unless we drive shallower queue
734 * depths and that becomes a performance bottleneck. In such cases
735 * switch to start providing fairness in terms of number of IOs.
736 */
737 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
738 return true;
739 else
740 return false;
741}
742
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100743static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
744{
745 if (cfq_class_idle(cfqq))
746 return IDLE_WORKLOAD;
747 if (cfq_class_rt(cfqq))
748 return RT_WORKLOAD;
749 return BE_WORKLOAD;
750}
751
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100752
753static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
754{
755 if (!cfq_cfqq_sync(cfqq))
756 return ASYNC_WORKLOAD;
757 if (!cfq_cfqq_idle_window(cfqq))
758 return SYNC_NOIDLE_WORKLOAD;
759 return SYNC_WORKLOAD;
760}
761
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500762static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
763 struct cfq_data *cfqd,
764 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100765{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500766 if (wl == IDLE_WORKLOAD)
767 return cfqg->service_tree_idle.count;
768
769 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
770 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
771 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100772}
773
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500774static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
775 struct cfq_group *cfqg)
776{
777 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
778 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
779}
780
Jens Axboe165125e2007-07-24 09:28:11 +0200781static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800782static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700783 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800784 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200785
Tejun Heoc5869802011-12-14 00:33:41 +0100786static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
787{
788 /* cic->icq is the first member, %NULL will convert to %NULL */
789 return container_of(icq, struct cfq_io_cq, icq);
790}
791
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100792static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
793 struct io_context *ioc)
794{
795 if (ioc)
796 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
797 return NULL;
798}
799
Tejun Heoc5869802011-12-14 00:33:41 +0100800static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200801{
Jens Axboea6151c32009-10-07 20:02:57 +0200802 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200803}
804
Tejun Heoc5869802011-12-14 00:33:41 +0100805static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
806 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200807{
Jens Axboea6151c32009-10-07 20:02:57 +0200808 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200809}
810
Tejun Heoc5869802011-12-14 00:33:41 +0100811static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400812{
Tejun Heoc5869802011-12-14 00:33:41 +0100813 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400814}
815
Vasily Tarasov91fac312007-04-25 12:29:51 +0200816/*
817 * We regard a request as SYNC, if it's either a read or has the SYNC bit
818 * set (in which case it could also be direct WRITE).
819 */
Jens Axboea6151c32009-10-07 20:02:57 +0200820static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200821{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200822 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200823}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700826 * scheduler run of queue, if there are requests pending and no one in the
827 * driver that will restart queueing
828 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200829static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700830{
Jens Axboe7b679132008-05-30 12:23:07 +0200831 if (cfqd->busy_queues) {
832 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200833 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200834 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100838 * Scale schedule slice based on io priority. Use the sync time slice only
839 * if a queue is marked sync and has sync io queued. A sync queue with async
840 * io only, should not get full sync slice length.
841 */
Jens Axboea6151c32009-10-07 20:02:57 +0200842static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200843 unsigned short prio)
844{
845 const int base_slice = cfqd->cfq_slice[sync];
846
847 WARN_ON(prio >= IOPRIO_BE_NR);
848
849 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
850}
851
Jens Axboe44f7c162007-01-19 11:51:58 +1100852static inline int
853cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
854{
Jens Axboed9e76202007-04-20 14:27:50 +0200855 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100856}
857
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500858static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
859{
860 u64 d = delta << CFQ_SERVICE_SHIFT;
861
862 d = d * BLKIO_WEIGHT_DEFAULT;
863 do_div(d, cfqg->weight);
864 return d;
865}
866
867static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
868{
869 s64 delta = (s64)(vdisktime - min_vdisktime);
870 if (delta > 0)
871 min_vdisktime = vdisktime;
872
873 return min_vdisktime;
874}
875
876static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
877{
878 s64 delta = (s64)(vdisktime - min_vdisktime);
879 if (delta < 0)
880 min_vdisktime = vdisktime;
881
882 return min_vdisktime;
883}
884
885static void update_min_vdisktime(struct cfq_rb_root *st)
886{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500887 struct cfq_group *cfqg;
888
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500889 if (st->left) {
890 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100891 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
892 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500893 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500894}
895
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100896/*
897 * get averaged number of queues of RT/BE priority.
898 * average is updated, with a formula that gives more weight to higher numbers,
899 * to quickly follows sudden increases and decrease slowly
900 */
901
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500902static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
903 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100904{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100905 unsigned min_q, max_q;
906 unsigned mult = cfq_hist_divisor - 1;
907 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500908 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100909
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500910 min_q = min(cfqg->busy_queues_avg[rt], busy);
911 max_q = max(cfqg->busy_queues_avg[rt], busy);
912 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100913 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500914 return cfqg->busy_queues_avg[rt];
915}
916
917static inline unsigned
918cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
919{
920 struct cfq_rb_root *st = &cfqd->grp_service_tree;
921
922 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100923}
924
Shaohua Lic553f8e2011-01-14 08:41:03 +0100925static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700926cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100927{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100928 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
929 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500930 /*
931 * interested queues (we consider only the ones with the same
932 * priority class in the cfq group)
933 */
934 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
935 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100936 unsigned sync_slice = cfqd->cfq_slice[1];
937 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500938 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
939
940 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100941 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
942 /* scale low_slice according to IO priority
943 * and sync vs async */
944 unsigned low_slice =
945 min(slice, base_low_slice * slice / sync_slice);
946 /* the adapted slice value is scaled to fit all iqs
947 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500948 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100949 low_slice);
950 }
951 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100952 return slice;
953}
954
955static inline void
956cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
957{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700958 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100959
Vivek Goyaldae739e2009-12-03 12:59:45 -0500960 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100961 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500962 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200963 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100964}
965
966/*
967 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
968 * isn't valid until the first request from the dispatch is activated
969 * and the slice time set.
970 */
Jens Axboea6151c32009-10-07 20:02:57 +0200971static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100972{
973 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100974 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100975 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100976 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100977
Shaohua Lic1e44752010-11-08 15:01:02 +0100978 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100979}
980
981/*
Jens Axboe5e705372006-07-13 12:39:25 +0200982 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200984 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Jens Axboe5e705372006-07-13 12:39:25 +0200986static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100987cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100989 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200991#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
992#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
993 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Jens Axboe5e705372006-07-13 12:39:25 +0200995 if (rq1 == NULL || rq1 == rq2)
996 return rq2;
997 if (rq2 == NULL)
998 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200999
Namhyung Kim229836b2011-05-24 10:23:21 +02001000 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1001 return rq_is_sync(rq1) ? rq1 : rq2;
1002
Christoph Hellwig65299a32011-08-23 14:50:29 +02001003 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1004 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001005
Tejun Heo83096eb2009-05-07 22:24:39 +09001006 s1 = blk_rq_pos(rq1);
1007 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * by definition, 1KiB is 2 sectors
1011 */
1012 back_max = cfqd->cfq_back_max * 2;
1013
1014 /*
1015 * Strict one way elevator _except_ in the case where we allow
1016 * short backward seeks which are biased as twice the cost of a
1017 * similar forward seek.
1018 */
1019 if (s1 >= last)
1020 d1 = s1 - last;
1021 else if (s1 + back_max >= last)
1022 d1 = (last - s1) * cfqd->cfq_back_penalty;
1023 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001024 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (s2 >= last)
1027 d2 = s2 - last;
1028 else if (s2 + back_max >= last)
1029 d2 = (last - s2) * cfqd->cfq_back_penalty;
1030 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001031 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Andreas Mohre8a99052006-03-28 08:59:49 +02001035 /*
1036 * By doing switch() on the bit mask "wrap" we avoid having to
1037 * check two variables for all permutations: --> faster!
1038 */
1039 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001040 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001041 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001042 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001043 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001044 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001045 else {
1046 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001047 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001048 else
Jens Axboe5e705372006-07-13 12:39:25 +02001049 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001050 }
1051
1052 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001053 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001054 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001055 return rq2;
1056 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001057 default:
1058 /*
1059 * Since both rqs are wrapped,
1060 * start with the one that's further behind head
1061 * (--> only *one* back seek required),
1062 * since back seek takes more time than forward.
1063 */
1064 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001065 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 else
Jens Axboe5e705372006-07-13 12:39:25 +02001067 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
Jens Axboe498d3aa22007-04-26 12:54:48 +02001071/*
1072 * The below is leftmost cache rbtree addon
1073 */
Jens Axboe08717142008-01-28 11:38:15 +01001074static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001075{
Vivek Goyal615f0252009-12-03 12:59:39 -05001076 /* Service tree is empty */
1077 if (!root->count)
1078 return NULL;
1079
Jens Axboecc09e292007-04-26 12:53:50 +02001080 if (!root->left)
1081 root->left = rb_first(&root->rb);
1082
Jens Axboe08717142008-01-28 11:38:15 +01001083 if (root->left)
1084 return rb_entry(root->left, struct cfq_queue, rb_node);
1085
1086 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001087}
1088
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001089static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1090{
1091 if (!root->left)
1092 root->left = rb_first(&root->rb);
1093
1094 if (root->left)
1095 return rb_entry_cfqg(root->left);
1096
1097 return NULL;
1098}
1099
Jens Axboea36e71f2009-04-15 12:15:11 +02001100static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1101{
1102 rb_erase(n, root);
1103 RB_CLEAR_NODE(n);
1104}
1105
Jens Axboecc09e292007-04-26 12:53:50 +02001106static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1107{
1108 if (root->left == n)
1109 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001110 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001111 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * would be nice to take fifo expire time into account as well
1116 */
Jens Axboe5e705372006-07-13 12:39:25 +02001117static struct request *
1118cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1119 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Jens Axboe21183b02006-07-13 12:33:14 +02001121 struct rb_node *rbnext = rb_next(&last->rb_node);
1122 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001123 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jens Axboe21183b02006-07-13 12:33:14 +02001125 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001128 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001131 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001132 else {
1133 rbnext = rb_first(&cfqq->sort_list);
1134 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001135 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001138 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Jens Axboed9e76202007-04-20 14:27:50 +02001141static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1142 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jens Axboed9e76202007-04-20 14:27:50 +02001144 /*
1145 * just an approximation, should be ok.
1146 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001147 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001148 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001151static inline s64
1152cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1153{
1154 return cfqg->vdisktime - st->min_vdisktime;
1155}
1156
1157static void
1158__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1159{
1160 struct rb_node **node = &st->rb.rb_node;
1161 struct rb_node *parent = NULL;
1162 struct cfq_group *__cfqg;
1163 s64 key = cfqg_key(st, cfqg);
1164 int left = 1;
1165
1166 while (*node != NULL) {
1167 parent = *node;
1168 __cfqg = rb_entry_cfqg(parent);
1169
1170 if (key < cfqg_key(st, __cfqg))
1171 node = &parent->rb_left;
1172 else {
1173 node = &parent->rb_right;
1174 left = 0;
1175 }
1176 }
1177
1178 if (left)
1179 st->left = &cfqg->rb_node;
1180
1181 rb_link_node(&cfqg->rb_node, parent, node);
1182 rb_insert_color(&cfqg->rb_node, &st->rb);
1183}
1184
1185static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001186cfq_update_group_weight(struct cfq_group *cfqg)
1187{
1188 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1189 if (cfqg->needs_update) {
1190 cfqg->weight = cfqg->new_weight;
1191 cfqg->needs_update = false;
1192 }
1193}
1194
1195static void
1196cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1197{
1198 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1199
1200 cfq_update_group_weight(cfqg);
1201 __cfq_group_service_tree_add(st, cfqg);
1202 st->total_weight += cfqg->weight;
1203}
1204
1205static void
1206cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001207{
1208 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1209 struct cfq_group *__cfqg;
1210 struct rb_node *n;
1211
1212 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001213 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001214 return;
1215
1216 /*
1217 * Currently put the group at the end. Later implement something
1218 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001219 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001220 */
1221 n = rb_last(&st->rb);
1222 if (n) {
1223 __cfqg = rb_entry_cfqg(n);
1224 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1225 } else
1226 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001227 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001228}
1229
1230static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001231cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1232{
1233 st->total_weight -= cfqg->weight;
1234 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1235 cfq_rb_erase(&cfqg->rb_node, st);
1236}
1237
1238static void
1239cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001240{
1241 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1242
1243 BUG_ON(cfqg->nr_cfqq < 1);
1244 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001245
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001246 /* If there are other cfq queues under this group, don't delete it */
1247 if (cfqg->nr_cfqq)
1248 return;
1249
Vivek Goyal2868ef72009-12-03 12:59:48 -05001250 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001251 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001252 cfqg->saved_workload_slice = 0;
Tejun Heoc1768262012-03-05 13:15:17 -08001253 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg),
1254 &blkio_policy_cfq, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001255}
1256
Justin TerAvest167400d2011-03-12 16:54:00 +01001257static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1258 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001259{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001260 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001261
1262 /*
1263 * Queue got expired before even a single request completed or
1264 * got expired immediately after first request completion.
1265 */
1266 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1267 /*
1268 * Also charge the seek time incurred to the group, otherwise
1269 * if there are mutiple queues in the group, each can dispatch
1270 * a single request on seeky media and cause lots of seek time
1271 * and group will never know it.
1272 */
1273 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1274 1);
1275 } else {
1276 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001277 if (slice_used > cfqq->allocated_slice) {
1278 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001279 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001280 }
1281 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1282 *unaccounted_time += cfqq->slice_start -
1283 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001284 }
1285
Vivek Goyaldae739e2009-12-03 12:59:45 -05001286 return slice_used;
1287}
1288
1289static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001290 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001291{
1292 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001293 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001294 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1295 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001296
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001297 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001298 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001299
Vivek Goyal02b35082010-08-23 12:23:53 +02001300 if (iops_mode(cfqd))
1301 charge = cfqq->slice_dispatch;
1302 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1303 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001304
1305 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001306 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001307 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001308 /* If a new weight was requested, update now, off tree */
1309 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001310
1311 /* This group is being expired. Save the context */
1312 if (time_after(cfqd->workload_expires, jiffies)) {
1313 cfqg->saved_workload_slice = cfqd->workload_expires
1314 - jiffies;
1315 cfqg->saved_workload = cfqd->serving_type;
1316 cfqg->saved_serving_prio = cfqd->serving_prio;
1317 } else
1318 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001319
1320 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1321 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001322 cfq_log_cfqq(cfqq->cfqd, cfqq,
1323 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1324 used_sl, cfqq->slice_dispatch, charge,
1325 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heoc1768262012-03-05 13:15:17 -08001326 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), &blkio_policy_cfq,
1327 used_sl, unaccounted_sl);
1328 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg), &blkio_policy_cfq);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001329}
1330
Tejun Heof51b8022012-03-05 13:15:05 -08001331/**
1332 * cfq_init_cfqg_base - initialize base part of a cfq_group
1333 * @cfqg: cfq_group to initialize
1334 *
1335 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1336 * is enabled or not.
1337 */
1338static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1339{
1340 struct cfq_rb_root *st;
1341 int i, j;
1342
1343 for_each_cfqg_st(cfqg, i, j, st)
1344 *st = CFQ_RB_ROOT;
1345 RB_CLEAR_NODE(&cfqg->rb_node);
1346
1347 cfqg->ttime.last_end_request = jiffies;
1348}
1349
Vivek Goyal25fb5162009-12-03 12:59:46 -05001350#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo60c2bc22012-04-01 14:38:43 -07001351static void cfq_update_blkio_group_weight(struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001352 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001353{
Tejun Heo03814112012-03-05 13:15:14 -08001354 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1355
Justin TerAvest8184f932011-03-17 16:12:36 +01001356 cfqg->new_weight = weight;
1357 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001358}
1359
Tejun Heo03814112012-03-05 13:15:14 -08001360static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001361{
Tejun Heo03814112012-03-05 13:15:14 -08001362 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001363
Tejun Heof51b8022012-03-05 13:15:05 -08001364 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001365 cfqg->weight = blkg->blkcg->weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001366}
1367
1368/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001369 * Search for the cfq group current task belongs to. request_queue lock must
1370 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001371 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001372static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1373 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001374{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001375 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001376 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001377
Tejun Heocd1604f2012-03-05 13:15:06 -08001378 /* avoid lookup for the common case where there's no blkio cgroup */
1379 if (blkcg == &blkio_root_cgroup) {
1380 cfqg = cfqd->root_group;
1381 } else {
1382 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001383
Tejun Heoaaec55a2012-04-01 14:38:42 -07001384 blkg = blkg_lookup_create(blkcg, q, false);
Tejun Heocd1604f2012-03-05 13:15:06 -08001385 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001386 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001387 }
1388
Vivek Goyal25fb5162009-12-03 12:59:46 -05001389 return cfqg;
1390}
1391
1392static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1393{
1394 /* Currently, all async queues are mapped to root group */
1395 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001396 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001397
1398 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001399 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001400 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001401}
1402
Tejun Heo60c2bc22012-04-01 14:38:43 -07001403static u64 blkg_prfill_weight_device(struct seq_file *sf,
1404 struct blkg_policy_data *pd, int off)
1405{
1406 if (!pd->conf.weight)
1407 return 0;
1408 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
1409}
1410
1411static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1412 struct seq_file *sf)
1413{
1414 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1415 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1416 false);
1417 return 0;
1418}
1419
1420static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1421 struct seq_file *sf)
1422{
1423 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
1424 return 0;
1425}
1426
1427static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1428 const char *buf)
1429{
1430 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1431 struct blkg_policy_data *pd;
1432 struct blkg_conf_ctx ctx;
1433 int ret;
1434
1435 ret = blkg_conf_prep(blkcg, buf, &ctx);
1436 if (ret)
1437 return ret;
1438
1439 ret = -EINVAL;
1440 pd = ctx.blkg->pd[BLKIO_POLICY_PROP];
1441 if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN &&
1442 ctx.v <= BLKIO_WEIGHT_MAX))) {
1443 pd->conf.weight = ctx.v;
1444 cfq_update_blkio_group_weight(ctx.blkg, ctx.v ?: blkcg->weight);
1445 ret = 0;
1446 }
1447
1448 blkg_conf_finish(&ctx);
1449 return ret;
1450}
1451
1452static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1453{
1454 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1455 struct blkio_group *blkg;
1456 struct hlist_node *n;
1457
1458 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1459 return -EINVAL;
1460
1461 spin_lock_irq(&blkcg->lock);
1462 blkcg->weight = (unsigned int)val;
1463
1464 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1465 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
1466
1467 if (pd && !pd->conf.weight)
1468 cfq_update_blkio_group_weight(blkg, blkcg->weight);
1469 }
1470
1471 spin_unlock_irq(&blkcg->lock);
1472 return 0;
1473}
1474
1475#ifdef CONFIG_DEBUG_BLK_CGROUP
1476static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
1477 struct blkg_policy_data *pd, int off)
1478{
1479 u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
1480 u64 v = 0;
1481
1482 if (samples) {
1483 v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
1484 do_div(v, samples);
1485 }
1486 __blkg_prfill_u64(sf, pd, v);
1487 return 0;
1488}
1489
1490/* print avg_queue_size */
1491static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1492 struct seq_file *sf)
1493{
1494 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1495
1496 blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
1497 BLKIO_POLICY_PROP, 0, false);
1498 return 0;
1499}
1500#endif /* CONFIG_DEBUG_BLK_CGROUP */
1501
1502static struct cftype cfq_blkcg_files[] = {
1503 {
1504 .name = "weight_device",
1505 .read_seq_string = blkcg_print_weight_device,
1506 .write_string = blkcg_set_weight_device,
1507 .max_write_len = 256,
1508 },
1509 {
1510 .name = "weight",
1511 .read_seq_string = blkcg_print_weight,
1512 .write_u64 = blkcg_set_weight,
1513 },
1514 {
1515 .name = "time",
1516 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1517 offsetof(struct blkio_group_stats, time)),
1518 .read_seq_string = blkcg_print_stat,
1519 },
1520 {
1521 .name = "sectors",
1522 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1523 offsetof(struct blkio_group_stats_cpu, sectors)),
1524 .read_seq_string = blkcg_print_cpu_stat,
1525 },
1526 {
1527 .name = "io_service_bytes",
1528 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1529 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1530 .read_seq_string = blkcg_print_cpu_rwstat,
1531 },
1532 {
1533 .name = "io_serviced",
1534 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1535 offsetof(struct blkio_group_stats_cpu, serviced)),
1536 .read_seq_string = blkcg_print_cpu_rwstat,
1537 },
1538 {
1539 .name = "io_service_time",
1540 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1541 offsetof(struct blkio_group_stats, service_time)),
1542 .read_seq_string = blkcg_print_rwstat,
1543 },
1544 {
1545 .name = "io_wait_time",
1546 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1547 offsetof(struct blkio_group_stats, wait_time)),
1548 .read_seq_string = blkcg_print_rwstat,
1549 },
1550 {
1551 .name = "io_merged",
1552 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1553 offsetof(struct blkio_group_stats, merged)),
1554 .read_seq_string = blkcg_print_rwstat,
1555 },
1556 {
1557 .name = "io_queued",
1558 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1559 offsetof(struct blkio_group_stats, queued)),
1560 .read_seq_string = blkcg_print_rwstat,
1561 },
1562#ifdef CONFIG_DEBUG_BLK_CGROUP
1563 {
1564 .name = "avg_queue_size",
1565 .read_seq_string = blkcg_print_avg_queue_size,
1566 },
1567 {
1568 .name = "group_wait_time",
1569 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1570 offsetof(struct blkio_group_stats, group_wait_time)),
1571 .read_seq_string = blkcg_print_stat,
1572 },
1573 {
1574 .name = "idle_time",
1575 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1576 offsetof(struct blkio_group_stats, idle_time)),
1577 .read_seq_string = blkcg_print_stat,
1578 },
1579 {
1580 .name = "empty_time",
1581 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1582 offsetof(struct blkio_group_stats, empty_time)),
1583 .read_seq_string = blkcg_print_stat,
1584 },
1585 {
1586 .name = "dequeue",
1587 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1588 offsetof(struct blkio_group_stats, dequeue)),
1589 .read_seq_string = blkcg_print_stat,
1590 },
1591 {
1592 .name = "unaccounted_time",
1593 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1594 offsetof(struct blkio_group_stats, unaccounted_time)),
1595 .read_seq_string = blkcg_print_stat,
1596 },
1597#endif /* CONFIG_DEBUG_BLK_CGROUP */
1598 { } /* terminate */
1599};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001600#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001601static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1602 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001603{
Tejun Heof51b8022012-03-05 13:15:05 -08001604 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001605}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001606
Vivek Goyal25fb5162009-12-03 12:59:46 -05001607static inline void
1608cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1609 cfqq->cfqg = cfqg;
1610}
1611
1612#endif /* GROUP_IOSCHED */
1613
Jens Axboe498d3aa22007-04-26 12:54:48 +02001614/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001615 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001616 * requests waiting to be processed. It is sorted in the order that
1617 * we will service the queues.
1618 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001619static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001620 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001621{
Jens Axboe08717142008-01-28 11:38:15 +01001622 struct rb_node **p, *parent;
1623 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001624 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001625 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001626 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001627 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001628
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001629 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001630 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001631 if (cfq_class_idle(cfqq)) {
1632 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001633 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001634 if (parent && parent != &cfqq->rb_node) {
1635 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1636 rb_key += __cfqq->rb_key;
1637 } else
1638 rb_key += jiffies;
1639 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001640 /*
1641 * Get our rb key offset. Subtract any residual slice
1642 * value carried from last service. A negative resid
1643 * count indicates slice overrun, and this should position
1644 * the next service time further away in the tree.
1645 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001646 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001647 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001648 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001649 } else {
1650 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001651 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001652 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1653 }
Jens Axboed9e76202007-04-20 14:27:50 +02001654
1655 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001656 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001657 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001658 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001659 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001660 if (rb_key == cfqq->rb_key &&
1661 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001662 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001663
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001664 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1665 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001666 }
Jens Axboed9e76202007-04-20 14:27:50 +02001667
Jens Axboe498d3aa22007-04-26 12:54:48 +02001668 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001669 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001670 cfqq->service_tree = service_tree;
1671 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001672 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001673 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001674
Jens Axboed9e76202007-04-20 14:27:50 +02001675 parent = *p;
1676 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1677
Jens Axboe0c534e02007-04-18 20:01:57 +02001678 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001679 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001680 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001681 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001682 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001683 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001684 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001685 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001686 }
Jens Axboe67060e32007-04-18 20:13:32 +02001687
1688 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001689 }
1690
Jens Axboecc09e292007-04-26 12:53:50 +02001691 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001692 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001693
Jens Axboed9e76202007-04-20 14:27:50 +02001694 cfqq->rb_key = rb_key;
1695 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001696 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1697 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001698 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001699 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001700 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Jens Axboea36e71f2009-04-15 12:15:11 +02001703static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001704cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1705 sector_t sector, struct rb_node **ret_parent,
1706 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001707{
Jens Axboea36e71f2009-04-15 12:15:11 +02001708 struct rb_node **p, *parent;
1709 struct cfq_queue *cfqq = NULL;
1710
1711 parent = NULL;
1712 p = &root->rb_node;
1713 while (*p) {
1714 struct rb_node **n;
1715
1716 parent = *p;
1717 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1718
1719 /*
1720 * Sort strictly based on sector. Smallest to the left,
1721 * largest to the right.
1722 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001723 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001724 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001725 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001726 n = &(*p)->rb_left;
1727 else
1728 break;
1729 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001730 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001731 }
1732
1733 *ret_parent = parent;
1734 if (rb_link)
1735 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001736 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001737}
1738
1739static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1740{
Jens Axboea36e71f2009-04-15 12:15:11 +02001741 struct rb_node **p, *parent;
1742 struct cfq_queue *__cfqq;
1743
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001744 if (cfqq->p_root) {
1745 rb_erase(&cfqq->p_node, cfqq->p_root);
1746 cfqq->p_root = NULL;
1747 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001748
1749 if (cfq_class_idle(cfqq))
1750 return;
1751 if (!cfqq->next_rq)
1752 return;
1753
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001754 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001755 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1756 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001757 if (!__cfqq) {
1758 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001759 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1760 } else
1761 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001762}
1763
Jens Axboe498d3aa22007-04-26 12:54:48 +02001764/*
1765 * Update cfqq's position in the service tree.
1766 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001767static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001768{
Jens Axboe6d048f52007-04-25 12:44:27 +02001769 /*
1770 * Resorting requires the cfqq to be on the RR list already.
1771 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001772 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001773 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001774 cfq_prio_tree_add(cfqd, cfqq);
1775 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/*
1779 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001780 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Jens Axboefebffd62008-01-28 13:19:43 +01001782static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Jens Axboe7b679132008-05-30 12:23:07 +02001784 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001785 BUG_ON(cfq_cfqq_on_rr(cfqq));
1786 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001788 if (cfq_cfqq_sync(cfqq))
1789 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jens Axboeedd75ff2007-04-19 12:03:34 +02001791 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Jens Axboe498d3aa22007-04-26 12:54:48 +02001794/*
1795 * Called when the cfqq no longer has requests pending, remove it from
1796 * the service tree.
1797 */
Jens Axboefebffd62008-01-28 13:19:43 +01001798static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Jens Axboe7b679132008-05-30 12:23:07 +02001800 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001801 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1802 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001804 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1805 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1806 cfqq->service_tree = NULL;
1807 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001808 if (cfqq->p_root) {
1809 rb_erase(&cfqq->p_node, cfqq->p_root);
1810 cfqq->p_root = NULL;
1811 }
Jens Axboed9e76202007-04-20 14:27:50 +02001812
Justin TerAvest8184f932011-03-17 16:12:36 +01001813 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 BUG_ON(!cfqd->busy_queues);
1815 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001816 if (cfq_cfqq_sync(cfqq))
1817 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820/*
1821 * rb tree support functions
1822 */
Jens Axboefebffd62008-01-28 13:19:43 +01001823static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jens Axboe5e705372006-07-13 12:39:25 +02001825 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001826 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jens Axboeb4878f22005-10-20 16:42:29 +02001828 BUG_ON(!cfqq->queued[sync]);
1829 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Jens Axboe5e705372006-07-13 12:39:25 +02001831 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Vivek Goyalf04a6422009-12-03 12:59:40 -05001833 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1834 /*
1835 * Queue will be deleted from service tree when we actually
1836 * expire it later. Right now just remove it from prio tree
1837 * as it is empty.
1838 */
1839 if (cfqq->p_root) {
1840 rb_erase(&cfqq->p_node, cfqq->p_root);
1841 cfqq->p_root = NULL;
1842 }
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Jens Axboe5e705372006-07-13 12:39:25 +02001846static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Jens Axboe5e705372006-07-13 12:39:25 +02001848 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001850 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jens Axboe5380a102006-07-13 12:37:56 +02001852 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Jeff Moyer796d5112011-06-02 21:19:05 +02001854 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001855
1856 if (!cfq_cfqq_on_rr(cfqq))
1857 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001858
1859 /*
1860 * check if this request is a better next-serve candidate
1861 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001862 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001863 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001864
1865 /*
1866 * adjust priority tree position, if ->next_rq changes
1867 */
1868 if (prev != cfqq->next_rq)
1869 cfq_prio_tree_add(cfqd, cfqq);
1870
Jens Axboe5044eed2007-04-25 11:53:48 +02001871 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Jens Axboefebffd62008-01-28 13:19:43 +01001874static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Jens Axboe5380a102006-07-13 12:37:56 +02001876 elv_rb_del(&cfqq->sort_list, rq);
1877 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001878 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001879 &blkio_policy_cfq, rq_data_dir(rq),
1880 rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001881 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001882 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001883 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08001884 cfqg_to_blkg(cfqq->cfqd->serving_group),
1885 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Jens Axboe206dc692006-03-28 13:03:44 +02001888static struct request *
1889cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Jens Axboe206dc692006-03-28 13:03:44 +02001891 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001892 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001893 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Jens Axboe4ac845a2008-01-24 08:44:49 +01001895 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001896 if (!cic)
1897 return NULL;
1898
1899 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001900 if (cfqq) {
1901 sector_t sector = bio->bi_sector + bio_sectors(bio);
1902
Jens Axboe21183b02006-07-13 12:33:14 +02001903 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return NULL;
1907}
1908
Jens Axboe165125e2007-07-24 09:28:11 +02001909static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001910{
1911 struct cfq_data *cfqd = q->elevator->elevator_data;
1912
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001913 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001914 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001915 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001916
Tejun Heo5b936292009-05-07 22:24:38 +09001917 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001918}
1919
Jens Axboe165125e2007-07-24 09:28:11 +02001920static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Jens Axboe22e2c502005-06-27 10:55:12 +02001922 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001924 WARN_ON(!cfqd->rq_in_driver);
1925 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001926 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001927 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Jens Axboeb4878f22005-10-20 16:42:29 +02001930static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Jens Axboe5e705372006-07-13 12:39:25 +02001932 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001933
Jens Axboe5e705372006-07-13 12:39:25 +02001934 if (cfqq->next_rq == rq)
1935 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Jens Axboeb4878f22005-10-20 16:42:29 +02001937 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001938 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001939
Aaron Carroll45333d52008-08-26 15:52:36 +02001940 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001941 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001942 &blkio_policy_cfq, rq_data_dir(rq),
1943 rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001944 if (rq->cmd_flags & REQ_PRIO) {
1945 WARN_ON(!cfqq->prio_pending);
1946 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Jens Axboe165125e2007-07-24 09:28:11 +02001950static int cfq_merge(struct request_queue *q, struct request **req,
1951 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 struct cfq_data *cfqd = q->elevator->elevator_data;
1954 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jens Axboe206dc692006-03-28 13:03:44 +02001956 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001957 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001958 *req = __rq;
1959 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
1962 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Jens Axboe165125e2007-07-24 09:28:11 +02001965static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001966 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Jens Axboe21183b02006-07-13 12:33:14 +02001968 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001969 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jens Axboe5e705372006-07-13 12:39:25 +02001971 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Divyesh Shah812d4022010-04-08 21:14:23 -07001975static void cfq_bio_merged(struct request_queue *q, struct request *req,
1976 struct bio *bio)
1977{
Tejun Heo03814112012-03-05 13:15:14 -08001978 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Tejun Heoc1768262012-03-05 13:15:17 -08001979 &blkio_policy_cfq, bio_data_dir(bio),
1980 cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static void
Jens Axboe165125e2007-07-24 09:28:11 +02001984cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct request *next)
1986{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001987 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001988 struct cfq_data *cfqd = q->elevator->elevator_data;
1989
Jens Axboe22e2c502005-06-27 10:55:12 +02001990 /*
1991 * reposition in fifo if next is older than rq
1992 */
1993 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001994 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001995 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001996 rq_set_fifo_time(rq, rq_fifo_time(next));
1997 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001998
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001999 if (cfqq->next_rq == next)
2000 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002001 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08002002 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08002003 &blkio_policy_cfq, rq_data_dir(next),
2004 rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002005
2006 cfqq = RQ_CFQQ(next);
2007 /*
2008 * all requests of this queue are merged to other queues, delete it
2009 * from the service tree. If it's the active_queue,
2010 * cfq_dispatch_requests() will choose to expire it or do idle
2011 */
2012 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2013 cfqq != cfqd->active_queue)
2014 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002015}
2016
Jens Axboe165125e2007-07-24 09:28:11 +02002017static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002018 struct bio *bio)
2019{
2020 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002021 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002022 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002023
2024 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002025 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002026 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002027 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002028 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002029
2030 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002031 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002032 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002033 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002034 cic = cfq_cic_lookup(cfqd, current->io_context);
2035 if (!cic)
2036 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002037
Vasily Tarasov91fac312007-04-25 12:29:51 +02002038 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002039 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002040}
2041
Divyesh Shah812df482010-04-08 21:15:35 -07002042static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2043{
2044 del_timer(&cfqd->idle_slice_timer);
Tejun Heoc1768262012-03-05 13:15:17 -08002045 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
2046 &blkio_policy_cfq);
Divyesh Shah812df482010-04-08 21:15:35 -07002047}
2048
Jens Axboefebffd62008-01-28 13:19:43 +01002049static void __cfq_set_active_queue(struct cfq_data *cfqd,
2050 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002051{
2052 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002053 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
2054 cfqd->serving_prio, cfqd->serving_type);
Tejun Heoc1768262012-03-05 13:15:17 -08002055 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg),
2056 &blkio_policy_cfq);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002057 cfqq->slice_start = 0;
2058 cfqq->dispatch_start = jiffies;
2059 cfqq->allocated_slice = 0;
2060 cfqq->slice_end = 0;
2061 cfqq->slice_dispatch = 0;
2062 cfqq->nr_sectors = 0;
2063
2064 cfq_clear_cfqq_wait_request(cfqq);
2065 cfq_clear_cfqq_must_dispatch(cfqq);
2066 cfq_clear_cfqq_must_alloc_slice(cfqq);
2067 cfq_clear_cfqq_fifo_expire(cfqq);
2068 cfq_mark_cfqq_slice_new(cfqq);
2069
2070 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002071 }
2072
2073 cfqd->active_queue = cfqq;
2074}
2075
2076/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002077 * current cfqq expired its slice (or was too idle), select new one
2078 */
2079static void
2080__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002081 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002082{
Jens Axboe7b679132008-05-30 12:23:07 +02002083 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2084
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002085 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002086 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002087
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002088 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002089 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002090
2091 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002092 * If this cfqq is shared between multiple processes, check to
2093 * make sure that those processes are still issuing I/Os within
2094 * the mean seek distance. If not, it may be time to break the
2095 * queues apart again.
2096 */
2097 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2098 cfq_mark_cfqq_split_coop(cfqq);
2099
2100 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002101 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002102 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002103 if (timed_out) {
2104 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002105 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002106 else
2107 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002108 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2109 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002110
Vivek Goyale5ff0822010-04-26 19:25:11 +02002111 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002112
Vivek Goyalf04a6422009-12-03 12:59:40 -05002113 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2114 cfq_del_cfqq_rr(cfqd, cfqq);
2115
Jens Axboeedd75ff2007-04-19 12:03:34 +02002116 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002117
2118 if (cfqq == cfqd->active_queue)
2119 cfqd->active_queue = NULL;
2120
2121 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002122 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002123 cfqd->active_cic = NULL;
2124 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002125}
2126
Vivek Goyale5ff0822010-04-26 19:25:11 +02002127static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002128{
2129 struct cfq_queue *cfqq = cfqd->active_queue;
2130
2131 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002132 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002133}
2134
Jens Axboe498d3aa22007-04-26 12:54:48 +02002135/*
2136 * Get next queue for service. Unless we have a queue preemption,
2137 * we'll simply select the first cfqq in the service tree.
2138 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002139static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002140{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002141 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002142 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002143 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002144
Vivek Goyalf04a6422009-12-03 12:59:40 -05002145 if (!cfqd->rq_queued)
2146 return NULL;
2147
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002148 /* There is nothing to dispatch */
2149 if (!service_tree)
2150 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002151 if (RB_EMPTY_ROOT(&service_tree->rb))
2152 return NULL;
2153 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02002154}
2155
Vivek Goyalf04a6422009-12-03 12:59:40 -05002156static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2157{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002158 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002159 struct cfq_queue *cfqq;
2160 int i, j;
2161 struct cfq_rb_root *st;
2162
2163 if (!cfqd->rq_queued)
2164 return NULL;
2165
Vivek Goyal25fb5162009-12-03 12:59:46 -05002166 cfqg = cfq_get_next_cfqg(cfqd);
2167 if (!cfqg)
2168 return NULL;
2169
Vivek Goyalf04a6422009-12-03 12:59:40 -05002170 for_each_cfqg_st(cfqg, i, j, st)
2171 if ((cfqq = cfq_rb_first(st)) != NULL)
2172 return cfqq;
2173 return NULL;
2174}
2175
Jens Axboe498d3aa22007-04-26 12:54:48 +02002176/*
2177 * Get and set a new active queue for service.
2178 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002179static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2180 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002181{
Jens Axboee00ef792009-11-04 08:54:55 +01002182 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002183 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002184
Jens Axboe22e2c502005-06-27 10:55:12 +02002185 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002186 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002187}
2188
Jens Axboed9e76202007-04-20 14:27:50 +02002189static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2190 struct request *rq)
2191{
Tejun Heo83096eb2009-05-07 22:24:39 +09002192 if (blk_rq_pos(rq) >= cfqd->last_position)
2193 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002194 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002195 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002196}
2197
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002198static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002199 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002200{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002201 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002202}
2203
Jens Axboea36e71f2009-04-15 12:15:11 +02002204static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2205 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002206{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002207 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002208 struct rb_node *parent, *node;
2209 struct cfq_queue *__cfqq;
2210 sector_t sector = cfqd->last_position;
2211
2212 if (RB_EMPTY_ROOT(root))
2213 return NULL;
2214
2215 /*
2216 * First, if we find a request starting at the end of the last
2217 * request, choose it.
2218 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002219 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002220 if (__cfqq)
2221 return __cfqq;
2222
2223 /*
2224 * If the exact sector wasn't found, the parent of the NULL leaf
2225 * will contain the closest sector.
2226 */
2227 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002228 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002229 return __cfqq;
2230
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002231 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002232 node = rb_next(&__cfqq->p_node);
2233 else
2234 node = rb_prev(&__cfqq->p_node);
2235 if (!node)
2236 return NULL;
2237
2238 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002239 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002240 return __cfqq;
2241
2242 return NULL;
2243}
2244
2245/*
2246 * cfqd - obvious
2247 * cur_cfqq - passed in so that we don't decide that the current queue is
2248 * closely cooperating with itself.
2249 *
2250 * So, basically we're assuming that that cur_cfqq has dispatched at least
2251 * one request, and that cfqd->last_position reflects a position on the disk
2252 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2253 * assumption.
2254 */
2255static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002256 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002257{
2258 struct cfq_queue *cfqq;
2259
Divyesh Shah39c01b22010-03-25 15:45:57 +01002260 if (cfq_class_idle(cur_cfqq))
2261 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002262 if (!cfq_cfqq_sync(cur_cfqq))
2263 return NULL;
2264 if (CFQQ_SEEKY(cur_cfqq))
2265 return NULL;
2266
Jens Axboea36e71f2009-04-15 12:15:11 +02002267 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002268 * Don't search priority tree if it's the only queue in the group.
2269 */
2270 if (cur_cfqq->cfqg->nr_cfqq == 1)
2271 return NULL;
2272
2273 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002274 * We should notice if some of the queues are cooperating, eg
2275 * working closely on the same area of the disk. In that case,
2276 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002277 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002278 cfqq = cfqq_close(cfqd, cur_cfqq);
2279 if (!cfqq)
2280 return NULL;
2281
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002282 /* If new queue belongs to different cfq_group, don't choose it */
2283 if (cur_cfqq->cfqg != cfqq->cfqg)
2284 return NULL;
2285
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002286 /*
2287 * It only makes sense to merge sync queues.
2288 */
2289 if (!cfq_cfqq_sync(cfqq))
2290 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002291 if (CFQQ_SEEKY(cfqq))
2292 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002293
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002294 /*
2295 * Do not merge queues of different priority classes
2296 */
2297 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2298 return NULL;
2299
Jens Axboea36e71f2009-04-15 12:15:11 +02002300 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002301}
2302
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002303/*
2304 * Determine whether we should enforce idle window for this queue.
2305 */
2306
2307static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2308{
2309 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002310 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002311
Vivek Goyalf04a6422009-12-03 12:59:40 -05002312 BUG_ON(!service_tree);
2313 BUG_ON(!service_tree->count);
2314
Vivek Goyalb6508c12010-08-23 12:23:33 +02002315 if (!cfqd->cfq_slice_idle)
2316 return false;
2317
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002318 /* We never do for idle class queues. */
2319 if (prio == IDLE_WORKLOAD)
2320 return false;
2321
2322 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002323 if (cfq_cfqq_idle_window(cfqq) &&
2324 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002325 return true;
2326
2327 /*
2328 * Otherwise, we do only if they are the last ones
2329 * in their service tree.
2330 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002331 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
2332 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002333 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002334 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
2335 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002336 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002337}
2338
Jens Axboe6d048f52007-04-25 12:44:27 +02002339static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002340{
Jens Axboe17926692007-01-19 11:59:30 +11002341 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002342 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002343 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002344
Jens Axboea68bbddba2008-09-24 13:03:33 +02002345 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002346 * SSD device without seek penalty, disable idling. But only do so
2347 * for devices that support queuing, otherwise we still have a problem
2348 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002349 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002350 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002351 return;
2352
Jens Axboedd67d052006-06-21 09:36:18 +02002353 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002354 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002355
2356 /*
2357 * idle is disabled, either manually or by past process history
2358 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002359 if (!cfq_should_idle(cfqd, cfqq)) {
2360 /* no queue idling. Check for group idling */
2361 if (cfqd->cfq_group_idle)
2362 group_idle = cfqd->cfq_group_idle;
2363 else
2364 return;
2365 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002366
Jens Axboe22e2c502005-06-27 10:55:12 +02002367 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002368 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002369 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002370 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002371 return;
2372
2373 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002374 * task has exited, don't wait
2375 */
Jens Axboe206dc692006-03-28 13:03:44 +02002376 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002377 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002378 return;
2379
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002380 /*
2381 * If our average think time is larger than the remaining time
2382 * slice, then don't idle. This avoids overrunning the allotted
2383 * time slice.
2384 */
Shaohua Li383cd722011-07-12 14:24:35 +02002385 if (sample_valid(cic->ttime.ttime_samples) &&
2386 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002387 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002388 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002389 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002390 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002391
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002392 /* There are other queues in the group, don't do group idle */
2393 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2394 return;
2395
Jens Axboe3b181522005-06-27 10:56:24 +02002396 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002397
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002398 if (group_idle)
2399 sl = cfqd->cfq_group_idle;
2400 else
2401 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002402
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002403 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heoc1768262012-03-05 13:15:17 -08002404 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
2405 &blkio_policy_cfq);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002406 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2407 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Jens Axboe498d3aa22007-04-26 12:54:48 +02002410/*
2411 * Move request from internal lists to the request queue dispatch list.
2412 */
Jens Axboe165125e2007-07-24 09:28:11 +02002413static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002415 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002416 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002417
Jens Axboe7b679132008-05-30 12:23:07 +02002418 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2419
Jeff Moyer06d21882009-09-11 17:08:59 +02002420 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002421 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002422 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002423 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002424 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002425
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002426 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002427 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08002428 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08002429 &blkio_policy_cfq, blk_rq_bytes(rq),
2430 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433/*
2434 * return expired entry, or NULL to just start from scratch in rbtree
2435 */
Jens Axboefebffd62008-01-28 13:19:43 +01002436static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Jens Axboe30996f42009-10-05 11:03:39 +02002438 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jens Axboe3b181522005-06-27 10:56:24 +02002440 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002442
2443 cfq_mark_cfqq_fifo_expire(cfqq);
2444
Jens Axboe89850f72006-07-22 16:48:31 +02002445 if (list_empty(&cfqq->fifo))
2446 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Jens Axboe89850f72006-07-22 16:48:31 +02002448 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002449 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002450 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Jens Axboe30996f42009-10-05 11:03:39 +02002452 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002453 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Jens Axboe22e2c502005-06-27 10:55:12 +02002456static inline int
2457cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2458{
2459 const int base_rq = cfqd->cfq_slice_async_rq;
2460
2461 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2462
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002463 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002464}
2465
2466/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002467 * Must be called with the queue_lock held.
2468 */
2469static int cfqq_process_refs(struct cfq_queue *cfqq)
2470{
2471 int process_refs, io_refs;
2472
2473 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002474 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002475 BUG_ON(process_refs < 0);
2476 return process_refs;
2477}
2478
2479static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2480{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002481 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002482 struct cfq_queue *__cfqq;
2483
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002484 /*
2485 * If there are no process references on the new_cfqq, then it is
2486 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2487 * chain may have dropped their last reference (not just their
2488 * last process reference).
2489 */
2490 if (!cfqq_process_refs(new_cfqq))
2491 return;
2492
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002493 /* Avoid a circular list and skip interim queue merges */
2494 while ((__cfqq = new_cfqq->new_cfqq)) {
2495 if (__cfqq == cfqq)
2496 return;
2497 new_cfqq = __cfqq;
2498 }
2499
2500 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002501 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002502 /*
2503 * If the process for the cfqq has gone away, there is no
2504 * sense in merging the queues.
2505 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002506 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002507 return;
2508
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002509 /*
2510 * Merge in the direction of the lesser amount of work.
2511 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002512 if (new_process_refs >= process_refs) {
2513 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002514 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002515 } else {
2516 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002517 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002518 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002519}
2520
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002521static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002522 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002523{
2524 struct cfq_queue *queue;
2525 int i;
2526 bool key_valid = false;
2527 unsigned long lowest_key = 0;
2528 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2529
Vivek Goyal65b32a52009-12-16 17:52:59 -05002530 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2531 /* select the one with lowest rb_key */
2532 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002533 if (queue &&
2534 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2535 lowest_key = queue->rb_key;
2536 cur_best = i;
2537 key_valid = true;
2538 }
2539 }
2540
2541 return cur_best;
2542}
2543
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002544static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002545{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002546 unsigned slice;
2547 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002548 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002549 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002550 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002551
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002552 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002553 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002554 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002555 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002556 cfqd->serving_prio = BE_WORKLOAD;
2557 else {
2558 cfqd->serving_prio = IDLE_WORKLOAD;
2559 cfqd->workload_expires = jiffies + 1;
2560 return;
2561 }
2562
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002563 if (original_prio != cfqd->serving_prio)
2564 goto new_workload;
2565
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002566 /*
2567 * For RT and BE, we have to choose also the type
2568 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2569 * expiration time
2570 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002571 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002572 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002573
2574 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002575 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002576 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002577 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002578 return;
2579
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002580new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002581 /* otherwise select new workload type */
2582 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002583 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2584 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002585 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002586
2587 /*
2588 * the workload slice is computed as a fraction of target latency
2589 * proportional to the number of queues in that workload, over
2590 * all the queues in the same priority class
2591 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002592 group_slice = cfq_group_slice(cfqd, cfqg);
2593
2594 slice = group_slice * count /
2595 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2596 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002597
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002598 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2599 unsigned int tmp;
2600
2601 /*
2602 * Async queues are currently system wide. Just taking
2603 * proportion of queues with-in same group will lead to higher
2604 * async ratio system wide as generally root group is going
2605 * to have higher weight. A more accurate thing would be to
2606 * calculate system wide asnc/sync ratio.
2607 */
2608 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2609 tmp = tmp/cfqd->busy_queues;
2610 slice = min_t(unsigned, slice, tmp);
2611
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002612 /* async workload slice is scaled down according to
2613 * the sync/async slice ratio. */
2614 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002615 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002616 /* sync workload slice is at least 2 * cfq_slice_idle */
2617 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2618
2619 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002620 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002621 cfqd->workload_expires = jiffies + slice;
2622}
2623
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002624static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2625{
2626 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002627 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002628
2629 if (RB_EMPTY_ROOT(&st->rb))
2630 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002631 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002632 update_min_vdisktime(st);
2633 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002634}
2635
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002636static void cfq_choose_cfqg(struct cfq_data *cfqd)
2637{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002638 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2639
2640 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002641
2642 /* Restore the workload type data */
2643 if (cfqg->saved_workload_slice) {
2644 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2645 cfqd->serving_type = cfqg->saved_workload;
2646 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002647 } else
2648 cfqd->workload_expires = jiffies - 1;
2649
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002650 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002651}
2652
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002653/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002654 * Select a queue for service. If we have a current active queue,
2655 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002656 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002657static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002658{
Jens Axboea36e71f2009-04-15 12:15:11 +02002659 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002660
2661 cfqq = cfqd->active_queue;
2662 if (!cfqq)
2663 goto new_queue;
2664
Vivek Goyalf04a6422009-12-03 12:59:40 -05002665 if (!cfqd->rq_queued)
2666 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002667
2668 /*
2669 * We were waiting for group to get backlogged. Expire the queue
2670 */
2671 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2672 goto expire;
2673
Jens Axboe22e2c502005-06-27 10:55:12 +02002674 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002675 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002676 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002677 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2678 /*
2679 * If slice had not expired at the completion of last request
2680 * we might not have turned on wait_busy flag. Don't expire
2681 * the queue yet. Allow the group to get backlogged.
2682 *
2683 * The very fact that we have used the slice, that means we
2684 * have been idling all along on this queue and it should be
2685 * ok to wait for this request to complete.
2686 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002687 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2688 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2689 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002690 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002691 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002692 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002693 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002694
2695 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002696 * The active queue has requests and isn't expired, allow it to
2697 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002698 */
Jens Axboedd67d052006-06-21 09:36:18 +02002699 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002700 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002701
2702 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002703 * If another queue has a request waiting within our mean seek
2704 * distance, let it run. The expire code will check for close
2705 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002706 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002707 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002708 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002709 if (new_cfqq) {
2710 if (!cfqq->new_cfqq)
2711 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002712 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002713 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002714
2715 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002716 * No requests pending. If the active queue still has requests in
2717 * flight or is idling for a new request, allow either of these
2718 * conditions to happen (or time out) before selecting a new queue.
2719 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002720 if (timer_pending(&cfqd->idle_slice_timer)) {
2721 cfqq = NULL;
2722 goto keep_queue;
2723 }
2724
Shaohua Li8e1ac662010-11-08 15:01:04 +01002725 /*
2726 * This is a deep seek queue, but the device is much faster than
2727 * the queue can deliver, don't idle
2728 **/
2729 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2730 (cfq_cfqq_slice_new(cfqq) ||
2731 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2732 cfq_clear_cfqq_deep(cfqq);
2733 cfq_clear_cfqq_idle_window(cfqq);
2734 }
2735
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002736 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2737 cfqq = NULL;
2738 goto keep_queue;
2739 }
2740
2741 /*
2742 * If group idle is enabled and there are requests dispatched from
2743 * this group, wait for requests to complete.
2744 */
2745check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002746 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2747 cfqq->cfqg->dispatched &&
2748 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002749 cfqq = NULL;
2750 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002751 }
2752
Jens Axboe3b181522005-06-27 10:56:24 +02002753expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002754 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002755new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002756 /*
2757 * Current queue expired. Check if we have to switch to a new
2758 * service tree
2759 */
2760 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002761 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002762
Jens Axboea36e71f2009-04-15 12:15:11 +02002763 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002764keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002765 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002766}
2767
Jens Axboefebffd62008-01-28 13:19:43 +01002768static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002769{
2770 int dispatched = 0;
2771
2772 while (cfqq->next_rq) {
2773 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2774 dispatched++;
2775 }
2776
2777 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002778
2779 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002780 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002781 return dispatched;
2782}
2783
Jens Axboe498d3aa22007-04-26 12:54:48 +02002784/*
2785 * Drain our current requests. Used for barriers and when switching
2786 * io schedulers on-the-fly.
2787 */
Jens Axboed9e76202007-04-20 14:27:50 +02002788static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002789{
Jens Axboe08717142008-01-28 11:38:15 +01002790 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002791 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002792
Divyesh Shah3440c492010-04-09 09:29:57 +02002793 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002794 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002795 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2796 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002797 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002798 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002799
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002800 BUG_ON(cfqd->busy_queues);
2801
Jeff Moyer6923715a2009-06-12 15:29:30 +02002802 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002803 return dispatched;
2804}
2805
Shaohua Liabc3c742010-03-01 09:20:54 +01002806static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2807 struct cfq_queue *cfqq)
2808{
2809 /* the queue hasn't finished any request, can't estimate */
2810 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002811 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002812 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2813 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002814 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002815
Shaohua Lic1e44752010-11-08 15:01:02 +01002816 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002817}
2818
Jens Axboe0b182d62009-10-06 20:49:37 +02002819static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002820{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002821 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Jens Axboe2f5cb732009-04-07 08:51:19 +02002823 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002824 * Drain async requests before we start sync IO
2825 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002826 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002827 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002828
2829 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002830 * If this is an async queue and we have sync IO in flight, let it wait
2831 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002832 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002833 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002834
Shaohua Liabc3c742010-03-01 09:20:54 +01002835 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002836 if (cfq_class_idle(cfqq))
2837 max_dispatch = 1;
2838
2839 /*
2840 * Does this cfqq already have too much IO in flight?
2841 */
2842 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002843 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002844 /*
2845 * idle queue must always only have a single IO in flight
2846 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002847 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002848 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002849
Jens Axboe2f5cb732009-04-07 08:51:19 +02002850 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002851 * If there is only one sync queue
2852 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002853 * queue no dispatch limit. The reason is a sync queue can
2854 * preempt async queue, limiting the sync queue doesn't make
2855 * sense. This is useful for aiostress test.
2856 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002857 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2858 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002859
2860 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002861 * We have other queues, don't allow more IO from this one
2862 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002863 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2864 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002865 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002866
Jens Axboe2f5cb732009-04-07 08:51:19 +02002867 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002868 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002869 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002870 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002871 max_dispatch = -1;
2872 else
2873 /*
2874 * Normally we start throttling cfqq when cfq_quantum/2
2875 * requests have been dispatched. But we can drive
2876 * deeper queue depths at the beginning of slice
2877 * subjected to upper limit of cfq_quantum.
2878 * */
2879 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002880 }
2881
2882 /*
2883 * Async queues must wait a bit before being allowed dispatch.
2884 * We also ramp up the dispatch depth gradually for async IO,
2885 * based on the last sync IO we serviced
2886 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002887 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002888 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002889 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002890
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002891 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002892 if (!depth && !cfqq->dispatched)
2893 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002894 if (depth < max_dispatch)
2895 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897
Jens Axboe0b182d62009-10-06 20:49:37 +02002898 /*
2899 * If we're below the current max, allow a dispatch
2900 */
2901 return cfqq->dispatched < max_dispatch;
2902}
2903
2904/*
2905 * Dispatch a request from cfqq, moving them to the request queue
2906 * dispatch list.
2907 */
2908static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2909{
2910 struct request *rq;
2911
2912 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2913
2914 if (!cfq_may_dispatch(cfqd, cfqq))
2915 return false;
2916
2917 /*
2918 * follow expired path, else get first next available
2919 */
2920 rq = cfq_check_fifo(cfqq);
2921 if (!rq)
2922 rq = cfqq->next_rq;
2923
2924 /*
2925 * insert request into driver dispatch list
2926 */
2927 cfq_dispatch_insert(cfqd->queue, rq);
2928
2929 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002930 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002931
Tejun Heoc5869802011-12-14 00:33:41 +01002932 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002933 cfqd->active_cic = cic;
2934 }
2935
2936 return true;
2937}
2938
2939/*
2940 * Find the cfqq that we need to service and move a request from that to the
2941 * dispatch list
2942 */
2943static int cfq_dispatch_requests(struct request_queue *q, int force)
2944{
2945 struct cfq_data *cfqd = q->elevator->elevator_data;
2946 struct cfq_queue *cfqq;
2947
2948 if (!cfqd->busy_queues)
2949 return 0;
2950
2951 if (unlikely(force))
2952 return cfq_forced_dispatch(cfqd);
2953
2954 cfqq = cfq_select_queue(cfqd);
2955 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002956 return 0;
2957
Jens Axboe2f5cb732009-04-07 08:51:19 +02002958 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002959 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002960 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002961 if (!cfq_dispatch_request(cfqd, cfqq))
2962 return 0;
2963
Jens Axboe2f5cb732009-04-07 08:51:19 +02002964 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002965 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002966
2967 /*
2968 * expire an async queue immediately if it has used up its slice. idle
2969 * queue always expire after 1 dispatch round.
2970 */
2971 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2972 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2973 cfq_class_idle(cfqq))) {
2974 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002975 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002976 }
2977
Shan Weib217a902009-09-01 10:06:42 +02002978 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982/*
Jens Axboe5e705372006-07-13 12:39:25 +02002983 * task holds one reference to the queue, dropped when task exits. each rq
2984 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002986 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * queue lock must be held here.
2988 */
2989static void cfq_put_queue(struct cfq_queue *cfqq)
2990{
Jens Axboe22e2c502005-06-27 10:55:12 +02002991 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002992 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002993
Shaohua Li30d7b942011-01-07 08:46:59 +01002994 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Shaohua Li30d7b942011-01-07 08:46:59 +01002996 cfqq->ref--;
2997 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return;
2999
Jens Axboe7b679132008-05-30 12:23:07 +02003000 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003002 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003003 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003005 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003006 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003007 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003008 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003009
Vivek Goyalf04a6422009-12-03 12:59:40 -05003010 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003012 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
Shaohua Lid02a2c02010-05-25 10:16:53 +02003015static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003016{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003017 struct cfq_queue *__cfqq, *next;
3018
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003019 /*
3020 * If this queue was scheduled to merge with another queue, be
3021 * sure to drop the reference taken on that queue (and others in
3022 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3023 */
3024 __cfqq = cfqq->new_cfqq;
3025 while (__cfqq) {
3026 if (__cfqq == cfqq) {
3027 WARN(1, "cfqq->new_cfqq loop detected\n");
3028 break;
3029 }
3030 next = __cfqq->new_cfqq;
3031 cfq_put_queue(__cfqq);
3032 __cfqq = next;
3033 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003034}
3035
3036static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3037{
3038 if (unlikely(cfqq == cfqd->active_queue)) {
3039 __cfq_slice_expired(cfqd, cfqq, 0);
3040 cfq_schedule_dispatch(cfqd);
3041 }
3042
3043 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003044
Jens Axboe89850f72006-07-22 16:48:31 +02003045 cfq_put_queue(cfqq);
3046}
3047
Tejun Heo9b84cac2011-12-14 00:33:42 +01003048static void cfq_init_icq(struct io_cq *icq)
3049{
3050 struct cfq_io_cq *cic = icq_to_cic(icq);
3051
3052 cic->ttime.last_end_request = jiffies;
3053}
3054
Tejun Heoc5869802011-12-14 00:33:41 +01003055static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003056{
Tejun Heoc5869802011-12-14 00:33:41 +01003057 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003058 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003059
Jens Axboeff6657c2009-04-08 10:58:57 +02003060 if (cic->cfqq[BLK_RW_ASYNC]) {
3061 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3062 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003063 }
3064
Jens Axboeff6657c2009-04-08 10:58:57 +02003065 if (cic->cfqq[BLK_RW_SYNC]) {
3066 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3067 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003068 }
Jens Axboe89850f72006-07-22 16:48:31 +02003069}
3070
Tejun Heoabede6d2012-03-19 15:10:57 -07003071static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003072{
3073 struct task_struct *tsk = current;
3074 int ioprio_class;
3075
Jens Axboe3b181522005-06-27 10:56:24 +02003076 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003077 return;
3078
Tejun Heo598971b2012-03-19 15:10:58 -07003079 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003080 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003081 default:
3082 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3083 case IOPRIO_CLASS_NONE:
3084 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003085 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003086 */
3087 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003088 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003089 break;
3090 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003091 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003092 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3093 break;
3094 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003095 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003096 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3097 break;
3098 case IOPRIO_CLASS_IDLE:
3099 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3100 cfqq->ioprio = 7;
3101 cfq_clear_cfqq_idle_window(cfqq);
3102 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003103 }
3104
3105 /*
3106 * keep track of original prio settings in case we have to temporarily
3107 * elevate the priority of this queue
3108 */
3109 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003110 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003111}
3112
Tejun Heo598971b2012-03-19 15:10:58 -07003113static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003114{
Tejun Heo598971b2012-03-19 15:10:58 -07003115 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003116 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003117 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003118
Tejun Heo598971b2012-03-19 15:10:58 -07003119 /*
3120 * Check whether ioprio has changed. The condition may trigger
3121 * spuriously on a newly created cic but there's no harm.
3122 */
3123 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003124 return;
3125
Jens Axboeff6657c2009-04-08 10:58:57 +02003126 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003127 if (cfqq) {
3128 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003129 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3130 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003131 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003132 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003133 cfq_put_queue(cfqq);
3134 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003135 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003136
Jens Axboeff6657c2009-04-08 10:58:57 +02003137 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003138 if (cfqq)
3139 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003140
3141 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003142}
3143
Jens Axboed5036d72009-06-26 10:44:34 +02003144static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003145 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003146{
3147 RB_CLEAR_NODE(&cfqq->rb_node);
3148 RB_CLEAR_NODE(&cfqq->p_node);
3149 INIT_LIST_HEAD(&cfqq->fifo);
3150
Shaohua Li30d7b942011-01-07 08:46:59 +01003151 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003152 cfqq->cfqd = cfqd;
3153
3154 cfq_mark_cfqq_prio_changed(cfqq);
3155
3156 if (is_sync) {
3157 if (!cfq_class_idle(cfqq))
3158 cfq_mark_cfqq_idle_window(cfqq);
3159 cfq_mark_cfqq_sync(cfqq);
3160 }
3161 cfqq->pid = pid;
3162}
3163
Vivek Goyal246103332009-12-03 12:59:51 -05003164#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003165static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003166{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003167 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003168 struct cfq_queue *sync_cfqq;
3169 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003170
Tejun Heo598971b2012-03-19 15:10:58 -07003171 rcu_read_lock();
3172 id = bio_blkio_cgroup(bio)->id;
3173 rcu_read_unlock();
3174
3175 /*
3176 * Check whether blkcg has changed. The condition may trigger
3177 * spuriously on a newly created cic but there's no harm.
3178 */
3179 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003180 return;
3181
Tejun Heo598971b2012-03-19 15:10:58 -07003182 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003183 if (sync_cfqq) {
3184 /*
3185 * Drop reference to sync queue. A new sync queue will be
3186 * assigned in new group upon arrival of a fresh request.
3187 */
3188 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3189 cic_set_cfqq(cic, NULL, 1);
3190 cfq_put_queue(sync_cfqq);
3191 }
Tejun Heo598971b2012-03-19 15:10:58 -07003192
3193 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003194}
Tejun Heo598971b2012-03-19 15:10:58 -07003195#else
3196static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003197#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003200cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3201 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08003203 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003205 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003208 rcu_read_lock();
3209
Tejun Heo4f85cb92012-03-05 13:15:28 -08003210 blkcg = bio_blkio_cgroup(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003211 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003212 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Jens Axboe6118b702009-06-30 09:34:12 +02003214 /*
3215 * Always try a new alloc if we fell back to the OOM cfqq
3216 * originally, since it should just be a temporary situation.
3217 */
3218 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3219 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (new_cfqq) {
3221 cfqq = new_cfqq;
3222 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003223 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003226 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003227 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003228 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003230 if (new_cfqq)
3231 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003232 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003233 cfqq = kmem_cache_alloc_node(cfq_pool,
3234 gfp_mask | __GFP_ZERO,
3235 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Jens Axboe6118b702009-06-30 09:34:12 +02003238 if (cfqq) {
3239 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003240 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003241 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003242 cfq_log_cfqq(cfqd, cfqq, "alloced");
3243 } else
3244 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246
3247 if (new_cfqq)
3248 kmem_cache_free(cfq_pool, new_cfqq);
3249
Tejun Heo2a7f1242012-03-05 13:15:01 -08003250 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return cfqq;
3252}
3253
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003254static struct cfq_queue **
3255cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3256{
Jens Axboefe094d92008-01-31 13:08:54 +01003257 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003258 case IOPRIO_CLASS_RT:
3259 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003260 case IOPRIO_CLASS_NONE:
3261 ioprio = IOPRIO_NORM;
3262 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003263 case IOPRIO_CLASS_BE:
3264 return &cfqd->async_cfqq[1][ioprio];
3265 case IOPRIO_CLASS_IDLE:
3266 return &cfqd->async_idle_cfqq;
3267 default:
3268 BUG();
3269 }
3270}
3271
Jens Axboe15c31be2007-07-10 13:43:25 +02003272static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003273cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003274 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003275{
Tejun Heo598971b2012-03-19 15:10:58 -07003276 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3277 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003278 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003279 struct cfq_queue *cfqq = NULL;
3280
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003281 if (!is_sync) {
3282 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3283 cfqq = *async_cfqq;
3284 }
3285
Jens Axboe6118b702009-06-30 09:34:12 +02003286 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003287 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003288
3289 /*
3290 * pin the queue now that it's allocated, scheduler exit will prune it
3291 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003292 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003293 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003294 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003295 }
3296
Shaohua Li30d7b942011-01-07 08:46:59 +01003297 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003298 return cfqq;
3299}
3300
Jens Axboe22e2c502005-06-27 10:55:12 +02003301static void
Shaohua Li383cd722011-07-12 14:24:35 +02003302__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003303{
Shaohua Li383cd722011-07-12 14:24:35 +02003304 unsigned long elapsed = jiffies - ttime->last_end_request;
3305 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003306
Shaohua Li383cd722011-07-12 14:24:35 +02003307 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3308 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3309 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3310}
3311
3312static void
3313cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003314 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003315{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003316 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003317 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003318 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3319 cfqd->cfq_slice_idle);
3320 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003321#ifdef CONFIG_CFQ_GROUP_IOSCHED
3322 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3323#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003324}
3325
Jens Axboe206dc692006-03-28 13:03:44 +02003326static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003327cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003328 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003329{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003330 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003331 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003332 if (cfqq->last_request_pos) {
3333 if (cfqq->last_request_pos < blk_rq_pos(rq))
3334 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3335 else
3336 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3337 }
Jens Axboe206dc692006-03-28 13:03:44 +02003338
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003339 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003340 if (blk_queue_nonrot(cfqd->queue))
3341 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3342 else
3343 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003344}
Jens Axboe22e2c502005-06-27 10:55:12 +02003345
3346/*
3347 * Disable idle window if the process thinks too long or seeks so much that
3348 * it doesn't matter
3349 */
3350static void
3351cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003352 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003353{
Jens Axboe7b679132008-05-30 12:23:07 +02003354 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003355
Jens Axboe08717142008-01-28 11:38:15 +01003356 /*
3357 * Don't idle for async or idle io prio class
3358 */
3359 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003360 return;
3361
Jens Axboec265a7f2008-06-26 13:49:33 +02003362 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003363
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003364 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3365 cfq_mark_cfqq_deep(cfqq);
3366
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003367 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3368 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003369 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003370 !cfqd->cfq_slice_idle ||
3371 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003372 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003373 else if (sample_valid(cic->ttime.ttime_samples)) {
3374 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003375 enable_idle = 0;
3376 else
3377 enable_idle = 1;
3378 }
3379
Jens Axboe7b679132008-05-30 12:23:07 +02003380 if (old_idle != enable_idle) {
3381 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3382 if (enable_idle)
3383 cfq_mark_cfqq_idle_window(cfqq);
3384 else
3385 cfq_clear_cfqq_idle_window(cfqq);
3386 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003387}
3388
Jens Axboe22e2c502005-06-27 10:55:12 +02003389/*
3390 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3391 * no or if we aren't sure, a 1 will cause a preempt.
3392 */
Jens Axboea6151c32009-10-07 20:02:57 +02003393static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003394cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003395 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003396{
Jens Axboe6d048f52007-04-25 12:44:27 +02003397 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003398
Jens Axboe6d048f52007-04-25 12:44:27 +02003399 cfqq = cfqd->active_queue;
3400 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003401 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003402
Jens Axboe6d048f52007-04-25 12:44:27 +02003403 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003404 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003405
3406 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003407 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003408
Jens Axboe22e2c502005-06-27 10:55:12 +02003409 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003410 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3411 */
3412 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3413 return false;
3414
3415 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003416 * if the new request is sync, but the currently running queue is
3417 * not, let the sync request have priority.
3418 */
Jens Axboe5e705372006-07-13 12:39:25 +02003419 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003420 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003421
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003422 if (new_cfqq->cfqg != cfqq->cfqg)
3423 return false;
3424
3425 if (cfq_slice_used(cfqq))
3426 return true;
3427
3428 /* Allow preemption only if we are idling on sync-noidle tree */
3429 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3430 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3431 new_cfqq->service_tree->count == 2 &&
3432 RB_EMPTY_ROOT(&cfqq->sort_list))
3433 return true;
3434
Jens Axboe374f84a2006-07-23 01:42:19 +02003435 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003436 * So both queues are sync. Let the new request get disk time if
3437 * it's a metadata request and the current queue is doing regular IO.
3438 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003439 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003440 return true;
3441
3442 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003443 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3444 */
3445 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003446 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003447
Shaohua Lid2d59e12010-11-08 15:01:03 +01003448 /* An idle queue should not be idle now for some reason */
3449 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3450 return true;
3451
Jens Axboe1e3335d2007-02-14 19:59:49 +01003452 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003453 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003454
3455 /*
3456 * if this request is as-good as one we would expect from the
3457 * current cfqq, let it preempt
3458 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003459 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003460 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003461
Jens Axboea6151c32009-10-07 20:02:57 +02003462 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003463}
3464
3465/*
3466 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3467 * let it have half of its nominal slice.
3468 */
3469static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3470{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003471 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3472
Jens Axboe7b679132008-05-30 12:23:07 +02003473 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003474 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003475
Jens Axboebf572252006-07-19 20:29:12 +02003476 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003477 * workload type is changed, don't save slice, otherwise preempt
3478 * doesn't happen
3479 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003480 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003481 cfqq->cfqg->saved_workload_slice = 0;
3482
3483 /*
Jens Axboebf572252006-07-19 20:29:12 +02003484 * Put the new queue at the front of the of the current list,
3485 * so we know that it will be selected next.
3486 */
3487 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003488
3489 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003490
Justin TerAvest62a37f62011-03-23 08:25:44 +01003491 cfqq->slice_end = 0;
3492 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003493}
3494
3495/*
Jens Axboe5e705372006-07-13 12:39:25 +02003496 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003497 * something we should do about it
3498 */
3499static void
Jens Axboe5e705372006-07-13 12:39:25 +02003500cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3501 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003502{
Tejun Heoc5869802011-12-14 00:33:41 +01003503 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003504
Aaron Carroll45333d52008-08-26 15:52:36 +02003505 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003506 if (rq->cmd_flags & REQ_PRIO)
3507 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003508
Shaohua Li383cd722011-07-12 14:24:35 +02003509 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003510 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003511 cfq_update_idle_window(cfqd, cfqq, cic);
3512
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003513 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003514
3515 if (cfqq == cfqd->active_queue) {
3516 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003517 * Remember that we saw a request from this process, but
3518 * don't start queuing just yet. Otherwise we risk seeing lots
3519 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003520 * and merging. If the request is already larger than a single
3521 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003522 * merging is already done. Ditto for a busy system that
3523 * has other work pending, don't risk delaying until the
3524 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003525 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003526 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003527 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3528 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003529 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003530 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003531 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003532 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003533 cfq_blkiocg_update_idle_time_stats(
Tejun Heoc1768262012-03-05 13:15:17 -08003534 cfqg_to_blkg(cfqq->cfqg),
3535 &blkio_policy_cfq);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003536 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003537 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003538 }
Jens Axboe5e705372006-07-13 12:39:25 +02003539 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003540 /*
3541 * not the active queue - expire current slice if it is
3542 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003543 * has some old slice time left and is of higher priority or
3544 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003545 */
3546 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003547 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003548 }
3549}
3550
Jens Axboe165125e2007-07-24 09:28:11 +02003551static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003552{
Jens Axboeb4878f22005-10-20 16:42:29 +02003553 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003554 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003555
Jens Axboe7b679132008-05-30 12:23:07 +02003556 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003557 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Jens Axboe30996f42009-10-05 11:03:39 +02003559 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003560 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003561 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003562 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08003563 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08003564 cfqg_to_blkg(cfqd->serving_group),
3565 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003566 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
Aaron Carroll45333d52008-08-26 15:52:36 +02003569/*
3570 * Update hw_tag based on peak queue depth over 50 samples under
3571 * sufficient load.
3572 */
3573static void cfq_update_hw_tag(struct cfq_data *cfqd)
3574{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003575 struct cfq_queue *cfqq = cfqd->active_queue;
3576
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003577 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3578 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003579
3580 if (cfqd->hw_tag == 1)
3581 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003582
3583 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003584 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003585 return;
3586
Shaohua Li1a1238a2009-10-27 08:46:23 +01003587 /*
3588 * If active queue hasn't enough requests and can idle, cfq might not
3589 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3590 * case
3591 */
3592 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3593 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003594 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003595 return;
3596
Aaron Carroll45333d52008-08-26 15:52:36 +02003597 if (cfqd->hw_tag_samples++ < 50)
3598 return;
3599
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003600 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003601 cfqd->hw_tag = 1;
3602 else
3603 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003604}
3605
Vivek Goyal7667aa02009-12-08 17:52:58 -05003606static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3607{
Tejun Heoc5869802011-12-14 00:33:41 +01003608 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003609
Justin TerAvest02a8f012011-02-09 14:20:03 +01003610 /* If the queue already has requests, don't wait */
3611 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3612 return false;
3613
Vivek Goyal7667aa02009-12-08 17:52:58 -05003614 /* If there are other queues in the group, don't wait */
3615 if (cfqq->cfqg->nr_cfqq > 1)
3616 return false;
3617
Shaohua Li7700fc42011-07-12 14:24:56 +02003618 /* the only queue in the group, but think time is big */
3619 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3620 return false;
3621
Vivek Goyal7667aa02009-12-08 17:52:58 -05003622 if (cfq_slice_used(cfqq))
3623 return true;
3624
3625 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003626 if (cic && sample_valid(cic->ttime.ttime_samples)
3627 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003628 return true;
3629
3630 /*
3631 * If think times is less than a jiffy than ttime_mean=0 and above
3632 * will not be true. It might happen that slice has not expired yet
3633 * but will expire soon (4-5 ns) during select_queue(). To cover the
3634 * case where think time is less than a jiffy, mark the queue wait
3635 * busy if only 1 jiffy is left in the slice.
3636 */
3637 if (cfqq->slice_end - jiffies == 1)
3638 return true;
3639
3640 return false;
3641}
3642
Jens Axboe165125e2007-07-24 09:28:11 +02003643static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Jens Axboe5e705372006-07-13 12:39:25 +02003645 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003646 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003647 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003648 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Jens Axboeb4878f22005-10-20 16:42:29 +02003650 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003651 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3652 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Aaron Carroll45333d52008-08-26 15:52:36 +02003654 cfq_update_hw_tag(cfqd);
3655
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003656 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003657 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003658 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003659 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003660 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003661 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08003662 &blkio_policy_cfq, rq_start_time_ns(rq),
3663 rq_io_start_time_ns(rq), rq_data_dir(rq),
3664 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003666 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003667
Vivek Goyal365722b2009-10-03 15:21:27 +02003668 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003669 struct cfq_rb_root *service_tree;
3670
Shaohua Li383cd722011-07-12 14:24:35 +02003671 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003672
3673 if (cfq_cfqq_on_rr(cfqq))
3674 service_tree = cfqq->service_tree;
3675 else
3676 service_tree = service_tree_for(cfqq->cfqg,
3677 cfqq_prio(cfqq), cfqq_type(cfqq));
3678 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003679 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3680 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003681 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003682
Shaohua Li7700fc42011-07-12 14:24:56 +02003683#ifdef CONFIG_CFQ_GROUP_IOSCHED
3684 cfqq->cfqg->ttime.last_end_request = now;
3685#endif
3686
Jens Axboecaaa5f92006-06-16 11:23:00 +02003687 /*
3688 * If this is the active queue, check if it needs to be expired,
3689 * or if we want to idle in case it has no pending requests.
3690 */
3691 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003692 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3693
Jens Axboe44f7c162007-01-19 11:51:58 +11003694 if (cfq_cfqq_slice_new(cfqq)) {
3695 cfq_set_prio_slice(cfqd, cfqq);
3696 cfq_clear_cfqq_slice_new(cfqq);
3697 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003698
3699 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003700 * Should we wait for next request to come in before we expire
3701 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003702 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003703 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003704 unsigned long extend_sl = cfqd->cfq_slice_idle;
3705 if (!cfqd->cfq_slice_idle)
3706 extend_sl = cfqd->cfq_group_idle;
3707 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003708 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003709 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003710 }
3711
Jens Axboea36e71f2009-04-15 12:15:11 +02003712 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003713 * Idling is not enabled on:
3714 * - expired queues
3715 * - idle-priority queues
3716 * - async queues
3717 * - queues with still some requests queued
3718 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003719 */
Jens Axboe08717142008-01-28 11:38:15 +01003720 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003721 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003722 else if (sync && cfqq_empty &&
3723 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003724 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003725 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003726 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003727
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003728 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003729 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Jens Axboe89850f72006-07-22 16:48:31 +02003732static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003733{
Jens Axboe1b379d82009-08-11 08:26:11 +02003734 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003735 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003736 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003737 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003738
3739 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003740}
3741
Jens Axboe165125e2007-07-24 09:28:11 +02003742static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003743{
3744 struct cfq_data *cfqd = q->elevator->elevator_data;
3745 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003746 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003747 struct cfq_queue *cfqq;
3748
3749 /*
3750 * don't force setup of a queue from here, as a call to may_queue
3751 * does not necessarily imply that a request actually will be queued.
3752 * so just lookup a possibly existing queue, or return 'may queue'
3753 * if that fails
3754 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003755 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003756 if (!cic)
3757 return ELV_MQUEUE_MAY;
3758
Jens Axboeb0b78f82009-04-08 10:56:08 +02003759 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003760 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003761 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003762
Jens Axboe89850f72006-07-22 16:48:31 +02003763 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003764 }
3765
3766 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769/*
3770 * queue lock held here
3771 */
Jens Axboebb37b942006-12-01 10:42:33 +01003772static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Jens Axboe5e705372006-07-13 12:39:25 +02003774 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Jens Axboe5e705372006-07-13 12:39:25 +02003776 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003777 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Jens Axboe22e2c502005-06-27 10:55:12 +02003779 BUG_ON(!cfqq->allocated[rw]);
3780 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003782 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003783 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003784 rq->elv.priv[0] = NULL;
3785 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 cfq_put_queue(cfqq);
3788 }
3789}
3790
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003791static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003792cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003793 struct cfq_queue *cfqq)
3794{
3795 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3796 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003797 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003798 cfq_put_queue(cfqq);
3799 return cic_to_cfqq(cic, 1);
3800}
3801
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003802/*
3803 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3804 * was the last process referring to said cfqq.
3805 */
3806static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003807split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003808{
3809 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003810 cfqq->pid = current->pid;
3811 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003812 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003813 return cfqq;
3814 }
3815
3816 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003817
3818 cfq_put_cooperator(cfqq);
3819
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003820 cfq_put_queue(cfqq);
3821 return NULL;
3822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003824 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003826static int
Tejun Heo852c7882012-03-05 13:15:27 -08003827cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
3828 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
3830 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003831 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003833 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003834 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 might_sleep_if(gfp_mask & __GFP_WAIT);
3837
Tejun Heo216284c2011-12-14 00:33:38 +01003838 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003839
Tejun Heo598971b2012-03-19 15:10:58 -07003840 check_ioprio_changed(cic, bio);
3841 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003842new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003843 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003844 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003845 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003846 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003847 } else {
3848 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003849 * If the queue was seeky for too long, break it apart.
3850 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003851 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003852 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3853 cfqq = split_cfqq(cic, cfqq);
3854 if (!cfqq)
3855 goto new_queue;
3856 }
3857
3858 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003859 * Check to see if this queue is scheduled to merge with
3860 * another, closely cooperating queue. The merging of
3861 * queues happens here as it must be done in process context.
3862 * The reference on new_cfqq was taken in merge_cfqqs.
3863 */
3864 if (cfqq->new_cfqq)
3865 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003869
Jens Axboe6fae9c22011-03-01 15:04:39 -05003870 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003871 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01003872 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003873 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01003874 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
3877
David Howells65f27f32006-11-22 14:55:48 +00003878static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003879{
David Howells65f27f32006-11-22 14:55:48 +00003880 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003881 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003882 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003883
Jens Axboe40bb54d2009-04-15 12:11:10 +02003884 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003885 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003886 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003887}
3888
3889/*
3890 * Timer running if the active_queue is currently idling inside its time slice
3891 */
3892static void cfq_idle_slice_timer(unsigned long data)
3893{
3894 struct cfq_data *cfqd = (struct cfq_data *) data;
3895 struct cfq_queue *cfqq;
3896 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003897 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003898
Jens Axboe7b679132008-05-30 12:23:07 +02003899 cfq_log(cfqd, "idle timer fired");
3900
Jens Axboe22e2c502005-06-27 10:55:12 +02003901 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3902
Jens Axboefe094d92008-01-31 13:08:54 +01003903 cfqq = cfqd->active_queue;
3904 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003905 timed_out = 0;
3906
Jens Axboe22e2c502005-06-27 10:55:12 +02003907 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003908 * We saw a request before the queue expired, let it through
3909 */
3910 if (cfq_cfqq_must_dispatch(cfqq))
3911 goto out_kick;
3912
3913 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003914 * expired
3915 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003916 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003917 goto expire;
3918
3919 /*
3920 * only expire and reinvoke request handler, if there are
3921 * other queues with pending requests
3922 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003923 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003924 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003925
3926 /*
3927 * not expired and it has a request pending, let it dispatch
3928 */
Jens Axboe75e50982009-04-07 08:56:14 +02003929 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003930 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003931
3932 /*
3933 * Queue depth flag is reset only when the idle didn't succeed
3934 */
3935 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003936 }
3937expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003938 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003939out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003940 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003941out_cont:
3942 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3943}
3944
Jens Axboe3b181522005-06-27 10:56:24 +02003945static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3946{
3947 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003948 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003949}
Jens Axboe22e2c502005-06-27 10:55:12 +02003950
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003951static void cfq_put_async_queues(struct cfq_data *cfqd)
3952{
3953 int i;
3954
3955 for (i = 0; i < IOPRIO_BE_NR; i++) {
3956 if (cfqd->async_cfqq[0][i])
3957 cfq_put_queue(cfqd->async_cfqq[0][i]);
3958 if (cfqd->async_cfqq[1][i])
3959 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003960 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003961
3962 if (cfqd->async_idle_cfqq)
3963 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003964}
3965
Jens Axboeb374d182008-10-31 10:05:07 +01003966static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Jens Axboe22e2c502005-06-27 10:55:12 +02003968 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003969 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003970
Jens Axboe3b181522005-06-27 10:56:24 +02003971 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003972
Al Virod9ff4182006-03-18 13:51:22 -05003973 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003974
Al Virod9ff4182006-03-18 13:51:22 -05003975 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003976 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003977
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003978 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08003979
3980 spin_unlock_irq(q->queue_lock);
3981
Al Viroa90d7422006-03-18 12:05:37 -05003982 cfq_shutdown_timer_wq(cfqd);
3983
Tejun Heof51b8022012-03-05 13:15:05 -08003984#ifndef CONFIG_CFQ_GROUP_IOSCHED
3985 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003986#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -08003987 update_root_blkg_pd(q, BLKIO_POLICY_PROP);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003988 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
Tejun Heob2fab5a2012-03-05 13:14:57 -08003991static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
3993 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003994 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003995 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Christoph Lameter94f60302007-07-17 04:03:29 -07003997 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003998 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003999 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004000
Tejun Heof51b8022012-03-05 13:15:05 -08004001 cfqd->queue = q;
4002 q->elevator->elevator_data = cfqd;
4003
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004004 /* Init root service tree */
4005 cfqd->grp_service_tree = CFQ_RB_ROOT;
4006
Tejun Heof51b8022012-03-05 13:15:05 -08004007 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004008#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08004009 rcu_read_lock();
4010 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004011
Tejun Heoaaec55a2012-04-01 14:38:42 -07004012 blkg = blkg_lookup_create(&blkio_root_cgroup, q, true);
Tejun Heocd1604f2012-03-05 13:15:06 -08004013 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08004014 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004015
4016 spin_unlock_irq(q->queue_lock);
4017 rcu_read_unlock();
4018#else
4019 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4020 GFP_KERNEL, cfqd->queue->node);
4021 if (cfqd->root_group)
4022 cfq_init_cfqg_base(cfqd->root_group);
4023#endif
4024 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004025 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08004026 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004027 }
4028
Tejun Heof51b8022012-03-05 13:15:05 -08004029 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004030
Jens Axboe26a2ac02009-04-23 12:13:27 +02004031 /*
4032 * Not strictly needed (since RB_ROOT just clears the node and we
4033 * zeroed cfqd on alloc), but better be safe in case someone decides
4034 * to add magic to the rb code
4035 */
4036 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4037 cfqd->prio_trees[i] = RB_ROOT;
4038
Jens Axboe6118b702009-06-30 09:34:12 +02004039 /*
4040 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4041 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004042 * will not attempt to free it. oom_cfqq is linked to root_group
4043 * but shouldn't hold a reference as it'll never be unlinked. Lose
4044 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004045 */
4046 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004047 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004048
4049 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004050 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004051 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004052 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Jens Axboe22e2c502005-06-27 10:55:12 +02004054 init_timer(&cfqd->idle_slice_timer);
4055 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4056 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4057
Jens Axboe23e018a2009-10-05 08:52:35 +02004058 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004061 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4062 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 cfqd->cfq_back_max = cfq_back_max;
4064 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004065 cfqd->cfq_slice[0] = cfq_slice_async;
4066 cfqd->cfq_slice[1] = cfq_slice_sync;
4067 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4068 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004069 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004070 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004071 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004072 /*
4073 * we optimistically start assuming sync ops weren't delayed in last
4074 * second, in order to have larger depth for async operations.
4075 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004076 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080/*
4081 * sysfs parts below -->
4082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083static ssize_t
4084cfq_var_show(unsigned int var, char *page)
4085{
4086 return sprintf(page, "%d\n", var);
4087}
4088
4089static ssize_t
4090cfq_var_store(unsigned int *var, const char *page, size_t count)
4091{
4092 char *p = (char *) page;
4093
4094 *var = simple_strtoul(p, &p, 10);
4095 return count;
4096}
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004099static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004101 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 unsigned int __data = __VAR; \
4103 if (__CONV) \
4104 __data = jiffies_to_msecs(__data); \
4105 return cfq_var_show(__data, (page)); \
4106}
4107SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004108SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4109SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004110SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4111SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004112SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004113SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004114SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4115SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4116SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004117SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118#undef SHOW_FUNCTION
4119
4120#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004121static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004123 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 unsigned int __data; \
4125 int ret = cfq_var_store(&__data, (page), count); \
4126 if (__data < (MIN)) \
4127 __data = (MIN); \
4128 else if (__data > (MAX)) \
4129 __data = (MAX); \
4130 if (__CONV) \
4131 *(__PTR) = msecs_to_jiffies(__data); \
4132 else \
4133 *(__PTR) = __data; \
4134 return ret; \
4135}
4136STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004137STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4138 UINT_MAX, 1);
4139STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4140 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004141STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004142STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4143 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004144STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004145STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004146STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4147STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004148STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4149 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004150STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151#undef STORE_FUNCTION
4152
Al Viroe572ec72006-03-18 22:27:18 -05004153#define CFQ_ATTR(name) \
4154 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004155
Al Viroe572ec72006-03-18 22:27:18 -05004156static struct elv_fs_entry cfq_attrs[] = {
4157 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004158 CFQ_ATTR(fifo_expire_sync),
4159 CFQ_ATTR(fifo_expire_async),
4160 CFQ_ATTR(back_seek_max),
4161 CFQ_ATTR(back_seek_penalty),
4162 CFQ_ATTR(slice_sync),
4163 CFQ_ATTR(slice_async),
4164 CFQ_ATTR(slice_async_rq),
4165 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004166 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004167 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004168 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169};
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171static struct elevator_type iosched_cfq = {
4172 .ops = {
4173 .elevator_merge_fn = cfq_merge,
4174 .elevator_merged_fn = cfq_merged_request,
4175 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004176 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004177 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004178 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004180 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004183 .elevator_former_req_fn = elv_rb_former_request,
4184 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004185 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004186 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 .elevator_set_req_fn = cfq_set_request,
4188 .elevator_put_req_fn = cfq_put_request,
4189 .elevator_may_queue_fn = cfq_may_queue,
4190 .elevator_init_fn = cfq_init_queue,
4191 .elevator_exit_fn = cfq_exit_queue,
4192 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004193 .icq_size = sizeof(struct cfq_io_cq),
4194 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004195 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004196 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 .elevator_owner = THIS_MODULE,
4198};
4199
Vivek Goyal3e252062009-12-04 10:36:42 -05004200#ifdef CONFIG_CFQ_GROUP_IOSCHED
4201static struct blkio_policy_type blkio_policy_cfq = {
4202 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08004203 .blkio_init_group_fn = cfq_init_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05004204 },
Vivek Goyal062a6442010-09-15 17:06:33 -04004205 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08004206 .pdata_size = sizeof(struct cfq_group),
Tejun Heo60c2bc22012-04-01 14:38:43 -07004207 .cftypes = cfq_blkcg_files,
Vivek Goyal3e252062009-12-04 10:36:42 -05004208};
Vivek Goyal3e252062009-12-04 10:36:42 -05004209#endif
4210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211static int __init cfq_init(void)
4212{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004213 int ret;
4214
Jens Axboe22e2c502005-06-27 10:55:12 +02004215 /*
4216 * could be 0 on HZ < 1000 setups
4217 */
4218 if (!cfq_slice_async)
4219 cfq_slice_async = 1;
4220 if (!cfq_slice_idle)
4221 cfq_slice_idle = 1;
4222
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004223#ifdef CONFIG_CFQ_GROUP_IOSCHED
4224 if (!cfq_group_idle)
4225 cfq_group_idle = 1;
4226#else
4227 cfq_group_idle = 0;
4228#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01004229 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4230 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return -ENOMEM;
4232
Tejun Heo3d3c2372011-12-14 00:33:42 +01004233 ret = elv_register(&iosched_cfq);
4234 if (ret) {
4235 kmem_cache_destroy(cfq_pool);
4236 return ret;
4237 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01004238
Tejun Heob95ada52012-03-05 13:14:55 -08004239#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004240 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004241#endif
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
4245static void __exit cfq_exit(void)
4246{
Tejun Heob95ada52012-03-05 13:14:55 -08004247#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004248 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004251 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
4254module_init(cfq_init);
4255module_exit(cfq_exit);
4256
4257MODULE_AUTHOR("Jens Axboe");
4258MODULE_LICENSE("GPL");
4259MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");