blob: 9a4eac490e0b9371be01a8293bee6ec9f7de6cdf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010013#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020016#include <linux/blktrace_api.h>
Tejun Heo6e736be2011-12-14 00:33:38 +010017#include "blk.h"
Vivek Goyale98ef892010-06-18 10:39:47 -040018#include "cfq.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Tejun Heo03814112012-03-05 13:15:14 -080020static struct blkio_policy_type blkio_policy_cfq;
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * tunables
24 */
Jens Axboefe094d92008-01-31 13:08:54 +010025/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010026static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010027static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010028/* maximum backwards seek, in KiB */
29static const int cfq_back_max = 16 * 1024;
30/* penalty of a backwards seek */
31static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010032static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020033static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020035static int cfq_slice_idle = HZ / 125;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +020036static int cfq_group_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010037static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
38static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020039
Jens Axboed9e76202007-04-20 14:27:50 +020040/*
Jens Axboe08717142008-01-28 11:38:15 +010041 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020042 */
Jens Axboe08717142008-01-28 11:38:15 +010043#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020044
45/*
46 * below this threshold, we consider thinktime immediate
47 */
48#define CFQ_MIN_TT (2)
49
Jens Axboe22e2c502005-06-27 10:55:12 +020050#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020051#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050052#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020053
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010054#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010055#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010056#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010057#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010058
Tejun Heoa612fdd2011-12-14 00:33:41 +010059#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
60#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
61#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Christoph Lametere18b8902006-12-06 20:33:20 -080063static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define CFQ_PRIO_LISTS IOPRIO_BE_NR
66#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020067#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
68
Jens Axboe206dc692006-03-28 13:03:44 +020069#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050070#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020071
Tejun Heoc5869802011-12-14 00:33:41 +010072struct cfq_ttime {
73 unsigned long last_end_request;
74
75 unsigned long ttime_total;
76 unsigned long ttime_samples;
77 unsigned long ttime_mean;
78};
79
Jens Axboe22e2c502005-06-27 10:55:12 +020080/*
Jens Axboecc09e292007-04-26 12:53:50 +020081 * Most of our rbtree usage is for sorting with min extraction, so
82 * if we cache the leftmost node we don't have to walk down the tree
83 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
84 * move this into the elevator for the rq sorting as well.
85 */
86struct cfq_rb_root {
87 struct rb_root rb;
88 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010089 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010090 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050091 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020092 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020093};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020094#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
95 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020096
97/*
Jens Axboe6118b702009-06-30 09:34:12 +020098 * Per process-grouping structure
99 */
100struct cfq_queue {
101 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100102 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200103 /* various state flags, see below */
104 unsigned int flags;
105 /* parent cfq_data */
106 struct cfq_data *cfqd;
107 /* service_tree member */
108 struct rb_node rb_node;
109 /* service_tree key */
110 unsigned long rb_key;
111 /* prio tree member */
112 struct rb_node p_node;
113 /* prio tree root we belong to, if any */
114 struct rb_root *p_root;
115 /* sorted list of pending requests */
116 struct rb_root sort_list;
117 /* if fifo isn't expired, next request to serve */
118 struct request *next_rq;
119 /* requests queued in sort_list */
120 int queued[2];
121 /* currently allocated requests */
122 int allocated[2];
123 /* fifo list of requests in sort_list */
124 struct list_head fifo;
125
Vivek Goyaldae739e2009-12-03 12:59:45 -0500126 /* time when queue got scheduled in to dispatch first request. */
127 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500128 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100129 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500130 /* time when first request from queue completed and slice started. */
131 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200132 unsigned long slice_end;
133 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200134
Christoph Hellwig65299a32011-08-23 14:50:29 +0200135 /* pending priority requests */
136 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200137 /* number of requests that are on the dispatch list or inside driver */
138 int dispatched;
139
140 /* io prio of this group */
141 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200142 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200143
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100144 pid_t pid;
145
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100146 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400147 sector_t last_request_pos;
148
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100149 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400150 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500151 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200152 /* Number of sectors dispatched from queue in single dispatch round */
153 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200154};
155
156/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100157 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100158 * IDLE is handled separately, so it has negative index
159 */
160enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100161 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500162 RT_WORKLOAD = 1,
163 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200164 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100165};
166
167/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100168 * Second index in the service_trees.
169 */
170enum wl_type_t {
171 ASYNC_WORKLOAD = 0,
172 SYNC_NOIDLE_WORKLOAD = 1,
173 SYNC_WORKLOAD = 2
174};
175
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500176/* This is per cgroup per device grouping structure */
177struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500178 /* group service_tree member */
179 struct rb_node rb_node;
180
181 /* group service_tree key */
182 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500183 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100184 unsigned int new_weight;
185 bool needs_update;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500186
187 /* number of cfqq currently on this group */
188 int nr_cfqq;
189
Jens Axboe22e2c502005-06-27 10:55:12 +0200190 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200191 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200192 * create the array for each prio class but at run time it is used
193 * only for RT and BE class and slot for IDLE class remains unused.
194 * This is primarily done to avoid confusion and a gcc warning.
195 */
196 unsigned int busy_queues_avg[CFQ_PRIO_NR];
197 /*
198 * rr lists of queues with requests. We maintain service trees for
199 * RT and BE classes. These trees are subdivided in subclasses
200 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
201 * class there is no subclassification and all the cfq queues go on
202 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100203 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200204 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100205 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100206 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500207
208 unsigned long saved_workload_slice;
209 enum wl_type_t saved_workload;
210 enum wl_prio_t saved_serving_prio;
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;
221};
222
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500223/*
224 * Per block device queue structure
225 */
226struct cfq_data {
227 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500228 /* Root service tree for cfq_groups */
229 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800230 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500231
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100232 /*
233 * The priority currently being served
234 */
235 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100236 enum wl_type_t serving_type;
237 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500238 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200239
240 /*
241 * Each priority tree is sorted by next_request position. These
242 * trees are used when determining if two or more queues are
243 * interleaving requests (see cfq_close_cooperator).
244 */
245 struct rb_root prio_trees[CFQ_PRIO_LISTS];
246
Jens Axboe22e2c502005-06-27 10:55:12 +0200247 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100248 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200249
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100250 int rq_in_driver;
251 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200252
253 /*
254 * queue-depth detection
255 */
256 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200257 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100258 /*
259 * hw_tag can be
260 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
261 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
262 * 0 => no NCQ
263 */
264 int hw_tag_est_depth;
265 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200266
267 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200268 * idle window management
269 */
270 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200271 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200272
273 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100274 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200275
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200276 /*
277 * async queue for each priority case
278 */
279 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
280 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200281
Jens Axboe6d048f52007-04-25 12:44:27 +0200282 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * tunables, see top of file
286 */
287 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200288 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int cfq_back_penalty;
290 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200291 unsigned int cfq_slice[2];
292 unsigned int cfq_slice_async_rq;
293 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200294 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200295 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500296
Jens Axboe6118b702009-06-30 09:34:12 +0200297 /*
298 * Fallback dummy cfqq for extreme OOM conditions
299 */
300 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200301
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100302 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Tejun Heo03814112012-03-05 13:15:14 -0800305static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
306{
307 return blkg_to_pdata(blkg, &blkio_policy_cfq);
308}
309
310static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
311{
312 return pdata_to_blkg(cfqg, &blkio_policy_cfq);
313}
314
Vivek Goyal25fb5162009-12-03 12:59:46 -0500315static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
316
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500317static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
318 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500319 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100320{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500321 if (!cfqg)
322 return NULL;
323
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100324 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500325 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100326
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500327 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100328}
329
Jens Axboe3b181522005-06-27 10:56:24 +0200330enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100331 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
332 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200333 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100334 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100335 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
336 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
337 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100338 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200339 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400340 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100341 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100342 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500343 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200344};
345
346#define CFQ_CFQQ_FNS(name) \
347static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
348{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100349 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200350} \
351static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
352{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100353 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200354} \
355static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
356{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100357 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200358}
359
360CFQ_CFQQ_FNS(on_rr);
361CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200362CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200363CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200364CFQ_CFQQ_FNS(fifo_expire);
365CFQ_CFQQ_FNS(idle_window);
366CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100367CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200368CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200369CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100370CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100371CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500372CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200373#undef CFQ_CFQQ_FNS
374
Vivek Goyalafc24d42010-04-26 19:27:56 +0200375#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal2868ef72009-12-03 12:59:48 -0500376#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
377 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
378 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
Tejun Heo03814112012-03-05 13:15:14 -0800379 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500380
381#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
382 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800383 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
Vivek Goyal2868ef72009-12-03 12:59:48 -0500384
385#else
Jens Axboe7b679132008-05-30 12:23:07 +0200386#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
387 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200388#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500389#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200390#define cfq_log(cfqd, fmt, args...) \
391 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
392
Vivek Goyal615f0252009-12-03 12:59:39 -0500393/* Traverses through cfq group service trees */
394#define for_each_cfqg_st(cfqg, i, j, st) \
395 for (i = 0; i <= IDLE_WORKLOAD; i++) \
396 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
397 : &cfqg->service_tree_idle; \
398 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
399 (i == IDLE_WORKLOAD && j == 0); \
400 j++, st = i < IDLE_WORKLOAD ? \
401 &cfqg->service_trees[i][j]: NULL) \
402
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200403static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
404 struct cfq_ttime *ttime, bool group_idle)
405{
406 unsigned long slice;
407 if (!sample_valid(ttime->ttime_samples))
408 return false;
409 if (group_idle)
410 slice = cfqd->cfq_group_idle;
411 else
412 slice = cfqd->cfq_slice_idle;
413 return ttime->ttime_mean > slice;
414}
Vivek Goyal615f0252009-12-03 12:59:39 -0500415
Vivek Goyal02b35082010-08-23 12:23:53 +0200416static inline bool iops_mode(struct cfq_data *cfqd)
417{
418 /*
419 * If we are not idling on queues and it is a NCQ drive, parallel
420 * execution of requests is on and measuring time is not possible
421 * in most of the cases until and unless we drive shallower queue
422 * depths and that becomes a performance bottleneck. In such cases
423 * switch to start providing fairness in terms of number of IOs.
424 */
425 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
426 return true;
427 else
428 return false;
429}
430
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100431static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
432{
433 if (cfq_class_idle(cfqq))
434 return IDLE_WORKLOAD;
435 if (cfq_class_rt(cfqq))
436 return RT_WORKLOAD;
437 return BE_WORKLOAD;
438}
439
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100440
441static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
442{
443 if (!cfq_cfqq_sync(cfqq))
444 return ASYNC_WORKLOAD;
445 if (!cfq_cfqq_idle_window(cfqq))
446 return SYNC_NOIDLE_WORKLOAD;
447 return SYNC_WORKLOAD;
448}
449
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500450static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
451 struct cfq_data *cfqd,
452 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100453{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500454 if (wl == IDLE_WORKLOAD)
455 return cfqg->service_tree_idle.count;
456
457 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
458 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
459 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100460}
461
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500462static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
463 struct cfq_group *cfqg)
464{
465 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
466 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
467}
468
Jens Axboe165125e2007-07-24 09:28:11 +0200469static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200470static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100471 struct io_context *, gfp_t);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200472
Tejun Heoc5869802011-12-14 00:33:41 +0100473static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
474{
475 /* cic->icq is the first member, %NULL will convert to %NULL */
476 return container_of(icq, struct cfq_io_cq, icq);
477}
478
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100479static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
480 struct io_context *ioc)
481{
482 if (ioc)
483 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
484 return NULL;
485}
486
Tejun Heoc5869802011-12-14 00:33:41 +0100487static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200488{
Jens Axboea6151c32009-10-07 20:02:57 +0200489 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200490}
491
Tejun Heoc5869802011-12-14 00:33:41 +0100492static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
493 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200494{
Jens Axboea6151c32009-10-07 20:02:57 +0200495 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200496}
497
Tejun Heoc5869802011-12-14 00:33:41 +0100498static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400499{
Tejun Heoc5869802011-12-14 00:33:41 +0100500 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400501}
502
Vasily Tarasov91fac312007-04-25 12:29:51 +0200503/*
504 * We regard a request as SYNC, if it's either a read or has the SYNC bit
505 * set (in which case it could also be direct WRITE).
506 */
Jens Axboea6151c32009-10-07 20:02:57 +0200507static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200508{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200509 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200510}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700513 * scheduler run of queue, if there are requests pending and no one in the
514 * driver that will restart queueing
515 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200516static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700517{
Jens Axboe7b679132008-05-30 12:23:07 +0200518 if (cfqd->busy_queues) {
519 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200520 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200521 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100525 * Scale schedule slice based on io priority. Use the sync time slice only
526 * if a queue is marked sync and has sync io queued. A sync queue with async
527 * io only, should not get full sync slice length.
528 */
Jens Axboea6151c32009-10-07 20:02:57 +0200529static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200530 unsigned short prio)
531{
532 const int base_slice = cfqd->cfq_slice[sync];
533
534 WARN_ON(prio >= IOPRIO_BE_NR);
535
536 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
537}
538
Jens Axboe44f7c162007-01-19 11:51:58 +1100539static inline int
540cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
541{
Jens Axboed9e76202007-04-20 14:27:50 +0200542 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100543}
544
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500545static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
546{
547 u64 d = delta << CFQ_SERVICE_SHIFT;
548
549 d = d * BLKIO_WEIGHT_DEFAULT;
550 do_div(d, cfqg->weight);
551 return d;
552}
553
554static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
555{
556 s64 delta = (s64)(vdisktime - min_vdisktime);
557 if (delta > 0)
558 min_vdisktime = vdisktime;
559
560 return min_vdisktime;
561}
562
563static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
564{
565 s64 delta = (s64)(vdisktime - min_vdisktime);
566 if (delta < 0)
567 min_vdisktime = vdisktime;
568
569 return min_vdisktime;
570}
571
572static void update_min_vdisktime(struct cfq_rb_root *st)
573{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500574 struct cfq_group *cfqg;
575
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500576 if (st->left) {
577 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100578 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
579 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500580 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500581}
582
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100583/*
584 * get averaged number of queues of RT/BE priority.
585 * average is updated, with a formula that gives more weight to higher numbers,
586 * to quickly follows sudden increases and decrease slowly
587 */
588
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500589static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
590 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100591{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100592 unsigned min_q, max_q;
593 unsigned mult = cfq_hist_divisor - 1;
594 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500595 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100596
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500597 min_q = min(cfqg->busy_queues_avg[rt], busy);
598 max_q = max(cfqg->busy_queues_avg[rt], busy);
599 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100600 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500601 return cfqg->busy_queues_avg[rt];
602}
603
604static inline unsigned
605cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
606{
607 struct cfq_rb_root *st = &cfqd->grp_service_tree;
608
609 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100610}
611
Shaohua Lic553f8e2011-01-14 08:41:03 +0100612static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700613cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100614{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100615 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
616 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500617 /*
618 * interested queues (we consider only the ones with the same
619 * priority class in the cfq group)
620 */
621 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
622 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100623 unsigned sync_slice = cfqd->cfq_slice[1];
624 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500625 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
626
627 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100628 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
629 /* scale low_slice according to IO priority
630 * and sync vs async */
631 unsigned low_slice =
632 min(slice, base_low_slice * slice / sync_slice);
633 /* the adapted slice value is scaled to fit all iqs
634 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500635 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100636 low_slice);
637 }
638 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100639 return slice;
640}
641
642static inline void
643cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
644{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700645 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100646
Vivek Goyaldae739e2009-12-03 12:59:45 -0500647 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100648 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500649 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200650 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100651}
652
653/*
654 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
655 * isn't valid until the first request from the dispatch is activated
656 * and the slice time set.
657 */
Jens Axboea6151c32009-10-07 20:02:57 +0200658static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100659{
660 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100661 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100662 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100663 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100664
Shaohua Lic1e44752010-11-08 15:01:02 +0100665 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100666}
667
668/*
Jens Axboe5e705372006-07-13 12:39:25 +0200669 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200671 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Jens Axboe5e705372006-07-13 12:39:25 +0200673static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100674cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100676 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200678#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
679#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
680 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jens Axboe5e705372006-07-13 12:39:25 +0200682 if (rq1 == NULL || rq1 == rq2)
683 return rq2;
684 if (rq2 == NULL)
685 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200686
Namhyung Kim229836b2011-05-24 10:23:21 +0200687 if (rq_is_sync(rq1) != rq_is_sync(rq2))
688 return rq_is_sync(rq1) ? rq1 : rq2;
689
Christoph Hellwig65299a32011-08-23 14:50:29 +0200690 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
691 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +0200692
Tejun Heo83096eb2009-05-07 22:24:39 +0900693 s1 = blk_rq_pos(rq1);
694 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
697 * by definition, 1KiB is 2 sectors
698 */
699 back_max = cfqd->cfq_back_max * 2;
700
701 /*
702 * Strict one way elevator _except_ in the case where we allow
703 * short backward seeks which are biased as twice the cost of a
704 * similar forward seek.
705 */
706 if (s1 >= last)
707 d1 = s1 - last;
708 else if (s1 + back_max >= last)
709 d1 = (last - s1) * cfqd->cfq_back_penalty;
710 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200711 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (s2 >= last)
714 d2 = s2 - last;
715 else if (s2 + back_max >= last)
716 d2 = (last - s2) * cfqd->cfq_back_penalty;
717 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200718 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Andreas Mohre8a99052006-03-28 08:59:49 +0200722 /*
723 * By doing switch() on the bit mask "wrap" we avoid having to
724 * check two variables for all permutations: --> faster!
725 */
726 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200727 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200728 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200729 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200730 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200731 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200732 else {
733 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200734 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200735 else
Jens Axboe5e705372006-07-13 12:39:25 +0200736 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200737 }
738
739 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200740 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200741 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200742 return rq2;
743 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200744 default:
745 /*
746 * Since both rqs are wrapped,
747 * start with the one that's further behind head
748 * (--> only *one* back seek required),
749 * since back seek takes more time than forward.
750 */
751 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200752 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else
Jens Axboe5e705372006-07-13 12:39:25 +0200754 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756}
757
Jens Axboe498d3aa22007-04-26 12:54:48 +0200758/*
759 * The below is leftmost cache rbtree addon
760 */
Jens Axboe08717142008-01-28 11:38:15 +0100761static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200762{
Vivek Goyal615f0252009-12-03 12:59:39 -0500763 /* Service tree is empty */
764 if (!root->count)
765 return NULL;
766
Jens Axboecc09e292007-04-26 12:53:50 +0200767 if (!root->left)
768 root->left = rb_first(&root->rb);
769
Jens Axboe08717142008-01-28 11:38:15 +0100770 if (root->left)
771 return rb_entry(root->left, struct cfq_queue, rb_node);
772
773 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200774}
775
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500776static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
777{
778 if (!root->left)
779 root->left = rb_first(&root->rb);
780
781 if (root->left)
782 return rb_entry_cfqg(root->left);
783
784 return NULL;
785}
786
Jens Axboea36e71f2009-04-15 12:15:11 +0200787static void rb_erase_init(struct rb_node *n, struct rb_root *root)
788{
789 rb_erase(n, root);
790 RB_CLEAR_NODE(n);
791}
792
Jens Axboecc09e292007-04-26 12:53:50 +0200793static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
794{
795 if (root->left == n)
796 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200797 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100798 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * would be nice to take fifo expire time into account as well
803 */
Jens Axboe5e705372006-07-13 12:39:25 +0200804static struct request *
805cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
806 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Jens Axboe21183b02006-07-13 12:33:14 +0200808 struct rb_node *rbnext = rb_next(&last->rb_node);
809 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200810 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jens Axboe21183b02006-07-13 12:33:14 +0200812 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200815 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200818 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200819 else {
820 rbnext = rb_first(&cfqq->sort_list);
821 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200822 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100825 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Jens Axboed9e76202007-04-20 14:27:50 +0200828static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
829 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Jens Axboed9e76202007-04-20 14:27:50 +0200831 /*
832 * just an approximation, should be ok.
833 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500834 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100835 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500838static inline s64
839cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
840{
841 return cfqg->vdisktime - st->min_vdisktime;
842}
843
844static void
845__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
846{
847 struct rb_node **node = &st->rb.rb_node;
848 struct rb_node *parent = NULL;
849 struct cfq_group *__cfqg;
850 s64 key = cfqg_key(st, cfqg);
851 int left = 1;
852
853 while (*node != NULL) {
854 parent = *node;
855 __cfqg = rb_entry_cfqg(parent);
856
857 if (key < cfqg_key(st, __cfqg))
858 node = &parent->rb_left;
859 else {
860 node = &parent->rb_right;
861 left = 0;
862 }
863 }
864
865 if (left)
866 st->left = &cfqg->rb_node;
867
868 rb_link_node(&cfqg->rb_node, parent, node);
869 rb_insert_color(&cfqg->rb_node, &st->rb);
870}
871
872static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100873cfq_update_group_weight(struct cfq_group *cfqg)
874{
875 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
876 if (cfqg->needs_update) {
877 cfqg->weight = cfqg->new_weight;
878 cfqg->needs_update = false;
879 }
880}
881
882static void
883cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
884{
885 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
886
887 cfq_update_group_weight(cfqg);
888 __cfq_group_service_tree_add(st, cfqg);
889 st->total_weight += cfqg->weight;
890}
891
892static void
893cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500894{
895 struct cfq_rb_root *st = &cfqd->grp_service_tree;
896 struct cfq_group *__cfqg;
897 struct rb_node *n;
898
899 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +0100900 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500901 return;
902
903 /*
904 * Currently put the group at the end. Later implement something
905 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300906 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500907 */
908 n = rb_last(&st->rb);
909 if (n) {
910 __cfqg = rb_entry_cfqg(n);
911 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
912 } else
913 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +0100914 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500915}
916
917static void
Justin TerAvest8184f932011-03-17 16:12:36 +0100918cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
919{
920 st->total_weight -= cfqg->weight;
921 if (!RB_EMPTY_NODE(&cfqg->rb_node))
922 cfq_rb_erase(&cfqg->rb_node, st);
923}
924
925static void
926cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500927{
928 struct cfq_rb_root *st = &cfqd->grp_service_tree;
929
930 BUG_ON(cfqg->nr_cfqq < 1);
931 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500932
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500933 /* If there are other cfq queues under this group, don't delete it */
934 if (cfqg->nr_cfqq)
935 return;
936
Vivek Goyal2868ef72009-12-03 12:59:48 -0500937 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +0100938 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500939 cfqg->saved_workload_slice = 0;
Tejun Heoc1768262012-03-05 13:15:17 -0800940 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg),
941 &blkio_policy_cfq, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500942}
943
Justin TerAvest167400d2011-03-12 16:54:00 +0100944static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
945 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500946{
Vivek Goyalf75edf22009-12-03 12:59:53 -0500947 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500948
949 /*
950 * Queue got expired before even a single request completed or
951 * got expired immediately after first request completion.
952 */
953 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
954 /*
955 * Also charge the seek time incurred to the group, otherwise
956 * if there are mutiple queues in the group, each can dispatch
957 * a single request on seeky media and cause lots of seek time
958 * and group will never know it.
959 */
960 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
961 1);
962 } else {
963 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +0100964 if (slice_used > cfqq->allocated_slice) {
965 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500966 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +0100967 }
968 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
969 *unaccounted_time += cfqq->slice_start -
970 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500971 }
972
Vivek Goyaldae739e2009-12-03 12:59:45 -0500973 return slice_used;
974}
975
976static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +0200977 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500978{
979 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +0100980 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500981 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
982 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500983
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500984 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +0100985 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500986
Vivek Goyal02b35082010-08-23 12:23:53 +0200987 if (iops_mode(cfqd))
988 charge = cfqq->slice_dispatch;
989 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
990 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500991
992 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +0100993 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +0200994 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +0100995 /* If a new weight was requested, update now, off tree */
996 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500997
998 /* This group is being expired. Save the context */
999 if (time_after(cfqd->workload_expires, jiffies)) {
1000 cfqg->saved_workload_slice = cfqd->workload_expires
1001 - jiffies;
1002 cfqg->saved_workload = cfqd->serving_type;
1003 cfqg->saved_serving_prio = cfqd->serving_prio;
1004 } else
1005 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001006
1007 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1008 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001009 cfq_log_cfqq(cfqq->cfqd, cfqq,
1010 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1011 used_sl, cfqq->slice_dispatch, charge,
1012 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heoc1768262012-03-05 13:15:17 -08001013 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), &blkio_policy_cfq,
1014 used_sl, unaccounted_sl);
1015 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg), &blkio_policy_cfq);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001016}
1017
Tejun Heof51b8022012-03-05 13:15:05 -08001018/**
1019 * cfq_init_cfqg_base - initialize base part of a cfq_group
1020 * @cfqg: cfq_group to initialize
1021 *
1022 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1023 * is enabled or not.
1024 */
1025static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1026{
1027 struct cfq_rb_root *st;
1028 int i, j;
1029
1030 for_each_cfqg_st(cfqg, i, j, st)
1031 *st = CFQ_RB_ROOT;
1032 RB_CLEAR_NODE(&cfqg->rb_node);
1033
1034 cfqg->ttime.last_end_request = jiffies;
1035}
1036
Vivek Goyal25fb5162009-12-03 12:59:46 -05001037#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoca32aef2012-03-05 13:15:03 -08001038static void cfq_update_blkio_group_weight(struct request_queue *q,
1039 struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001040 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001041{
Tejun Heo03814112012-03-05 13:15:14 -08001042 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1043
Justin TerAvest8184f932011-03-17 16:12:36 +01001044 cfqg->new_weight = weight;
1045 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001046}
1047
Tejun Heo03814112012-03-05 13:15:14 -08001048static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001049{
Tejun Heo03814112012-03-05 13:15:14 -08001050 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001051
Tejun Heof51b8022012-03-05 13:15:05 -08001052 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001053 cfqg->weight = blkg->blkcg->weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001054}
1055
1056/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001057 * Search for the cfq group current task belongs to. request_queue lock must
1058 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001059 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001060static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1061 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001062{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001063 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001064 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001065
Tejun Heocd1604f2012-03-05 13:15:06 -08001066 /* avoid lookup for the common case where there's no blkio cgroup */
1067 if (blkcg == &blkio_root_cgroup) {
1068 cfqg = cfqd->root_group;
1069 } else {
1070 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001071
Tejun Heocd1604f2012-03-05 13:15:06 -08001072 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_PROP, false);
1073 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001074 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001075 }
1076
Vivek Goyal25fb5162009-12-03 12:59:46 -05001077 return cfqg;
1078}
1079
1080static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1081{
1082 /* Currently, all async queues are mapped to root group */
1083 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001084 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001085
1086 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001087 /* cfqq reference on cfqg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -08001088 blkg_get(cfqg_to_blkg(cfqg));
Vivek Goyalb1c35762009-12-03 12:59:47 -05001089}
1090
Vivek Goyal25fb5162009-12-03 12:59:46 -05001091#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001092static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1093 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001094{
Tejun Heof51b8022012-03-05 13:15:05 -08001095 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001096}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001097
Vivek Goyal25fb5162009-12-03 12:59:46 -05001098static inline void
1099cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1100 cfqq->cfqg = cfqg;
1101}
1102
1103#endif /* GROUP_IOSCHED */
1104
Jens Axboe498d3aa22007-04-26 12:54:48 +02001105/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001106 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001107 * requests waiting to be processed. It is sorted in the order that
1108 * we will service the queues.
1109 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001110static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001111 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001112{
Jens Axboe08717142008-01-28 11:38:15 +01001113 struct rb_node **p, *parent;
1114 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001115 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001116 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001117 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001118 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001119
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001120 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001121 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001122 if (cfq_class_idle(cfqq)) {
1123 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001124 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001125 if (parent && parent != &cfqq->rb_node) {
1126 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1127 rb_key += __cfqq->rb_key;
1128 } else
1129 rb_key += jiffies;
1130 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001131 /*
1132 * Get our rb key offset. Subtract any residual slice
1133 * value carried from last service. A negative resid
1134 * count indicates slice overrun, and this should position
1135 * the next service time further away in the tree.
1136 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001137 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001138 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001139 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001140 } else {
1141 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001142 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001143 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1144 }
Jens Axboed9e76202007-04-20 14:27:50 +02001145
1146 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001147 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001148 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001149 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001150 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001151 if (rb_key == cfqq->rb_key &&
1152 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001153 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001154
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001155 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1156 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001157 }
Jens Axboed9e76202007-04-20 14:27:50 +02001158
Jens Axboe498d3aa22007-04-26 12:54:48 +02001159 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001160 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001161 cfqq->service_tree = service_tree;
1162 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001163 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001164 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001165
Jens Axboed9e76202007-04-20 14:27:50 +02001166 parent = *p;
1167 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1168
Jens Axboe0c534e02007-04-18 20:01:57 +02001169 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001170 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001171 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001172 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001173 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001174 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001175 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001176 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001177 }
Jens Axboe67060e32007-04-18 20:13:32 +02001178
1179 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001180 }
1181
Jens Axboecc09e292007-04-26 12:53:50 +02001182 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001183 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001184
Jens Axboed9e76202007-04-20 14:27:50 +02001185 cfqq->rb_key = rb_key;
1186 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001187 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1188 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001189 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001190 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001191 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Jens Axboea36e71f2009-04-15 12:15:11 +02001194static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001195cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1196 sector_t sector, struct rb_node **ret_parent,
1197 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001198{
Jens Axboea36e71f2009-04-15 12:15:11 +02001199 struct rb_node **p, *parent;
1200 struct cfq_queue *cfqq = NULL;
1201
1202 parent = NULL;
1203 p = &root->rb_node;
1204 while (*p) {
1205 struct rb_node **n;
1206
1207 parent = *p;
1208 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1209
1210 /*
1211 * Sort strictly based on sector. Smallest to the left,
1212 * largest to the right.
1213 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001214 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001215 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001216 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001217 n = &(*p)->rb_left;
1218 else
1219 break;
1220 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001221 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001222 }
1223
1224 *ret_parent = parent;
1225 if (rb_link)
1226 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001227 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001228}
1229
1230static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1231{
Jens Axboea36e71f2009-04-15 12:15:11 +02001232 struct rb_node **p, *parent;
1233 struct cfq_queue *__cfqq;
1234
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001235 if (cfqq->p_root) {
1236 rb_erase(&cfqq->p_node, cfqq->p_root);
1237 cfqq->p_root = NULL;
1238 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001239
1240 if (cfq_class_idle(cfqq))
1241 return;
1242 if (!cfqq->next_rq)
1243 return;
1244
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001245 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001246 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1247 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001248 if (!__cfqq) {
1249 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001250 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1251 } else
1252 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001253}
1254
Jens Axboe498d3aa22007-04-26 12:54:48 +02001255/*
1256 * Update cfqq's position in the service tree.
1257 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001258static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001259{
Jens Axboe6d048f52007-04-25 12:44:27 +02001260 /*
1261 * Resorting requires the cfqq to be on the RR list already.
1262 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001263 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001264 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001265 cfq_prio_tree_add(cfqd, cfqq);
1266 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001271 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Jens Axboefebffd62008-01-28 13:19:43 +01001273static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Jens Axboe7b679132008-05-30 12:23:07 +02001275 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001276 BUG_ON(cfq_cfqq_on_rr(cfqq));
1277 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001279 if (cfq_cfqq_sync(cfqq))
1280 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jens Axboeedd75ff2007-04-19 12:03:34 +02001282 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Jens Axboe498d3aa22007-04-26 12:54:48 +02001285/*
1286 * Called when the cfqq no longer has requests pending, remove it from
1287 * the service tree.
1288 */
Jens Axboefebffd62008-01-28 13:19:43 +01001289static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Jens Axboe7b679132008-05-30 12:23:07 +02001291 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001292 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1293 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001295 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1296 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1297 cfqq->service_tree = NULL;
1298 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001299 if (cfqq->p_root) {
1300 rb_erase(&cfqq->p_node, cfqq->p_root);
1301 cfqq->p_root = NULL;
1302 }
Jens Axboed9e76202007-04-20 14:27:50 +02001303
Justin TerAvest8184f932011-03-17 16:12:36 +01001304 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 BUG_ON(!cfqd->busy_queues);
1306 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001307 if (cfq_cfqq_sync(cfqq))
1308 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/*
1312 * rb tree support functions
1313 */
Jens Axboefebffd62008-01-28 13:19:43 +01001314static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Jens Axboe5e705372006-07-13 12:39:25 +02001316 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001317 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Jens Axboeb4878f22005-10-20 16:42:29 +02001319 BUG_ON(!cfqq->queued[sync]);
1320 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jens Axboe5e705372006-07-13 12:39:25 +02001322 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Vivek Goyalf04a6422009-12-03 12:59:40 -05001324 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1325 /*
1326 * Queue will be deleted from service tree when we actually
1327 * expire it later. Right now just remove it from prio tree
1328 * as it is empty.
1329 */
1330 if (cfqq->p_root) {
1331 rb_erase(&cfqq->p_node, cfqq->p_root);
1332 cfqq->p_root = NULL;
1333 }
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Jens Axboe5e705372006-07-13 12:39:25 +02001337static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Jens Axboe5e705372006-07-13 12:39:25 +02001339 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001341 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jens Axboe5380a102006-07-13 12:37:56 +02001343 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Jeff Moyer796d5112011-06-02 21:19:05 +02001345 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001346
1347 if (!cfq_cfqq_on_rr(cfqq))
1348 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001349
1350 /*
1351 * check if this request is a better next-serve candidate
1352 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001353 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001354 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001355
1356 /*
1357 * adjust priority tree position, if ->next_rq changes
1358 */
1359 if (prev != cfqq->next_rq)
1360 cfq_prio_tree_add(cfqd, cfqq);
1361
Jens Axboe5044eed2007-04-25 11:53:48 +02001362 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Jens Axboefebffd62008-01-28 13:19:43 +01001365static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jens Axboe5380a102006-07-13 12:37:56 +02001367 elv_rb_del(&cfqq->sort_list, rq);
1368 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001369 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001370 &blkio_policy_cfq, rq_data_dir(rq),
1371 rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001372 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001373 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001374 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08001375 cfqg_to_blkg(cfqq->cfqd->serving_group),
1376 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Jens Axboe206dc692006-03-28 13:03:44 +02001379static struct request *
1380cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Jens Axboe206dc692006-03-28 13:03:44 +02001382 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001383 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001384 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Jens Axboe4ac845a2008-01-24 08:44:49 +01001386 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001387 if (!cic)
1388 return NULL;
1389
1390 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001391 if (cfqq) {
1392 sector_t sector = bio->bi_sector + bio_sectors(bio);
1393
Jens Axboe21183b02006-07-13 12:33:14 +02001394 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return NULL;
1398}
1399
Jens Axboe165125e2007-07-24 09:28:11 +02001400static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001401{
1402 struct cfq_data *cfqd = q->elevator->elevator_data;
1403
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001404 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001405 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001406 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001407
Tejun Heo5b936292009-05-07 22:24:38 +09001408 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001409}
1410
Jens Axboe165125e2007-07-24 09:28:11 +02001411static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Jens Axboe22e2c502005-06-27 10:55:12 +02001413 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001415 WARN_ON(!cfqd->rq_in_driver);
1416 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001417 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001418 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Jens Axboeb4878f22005-10-20 16:42:29 +02001421static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Jens Axboe5e705372006-07-13 12:39:25 +02001423 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001424
Jens Axboe5e705372006-07-13 12:39:25 +02001425 if (cfqq->next_rq == rq)
1426 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Jens Axboeb4878f22005-10-20 16:42:29 +02001428 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001429 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001430
Aaron Carroll45333d52008-08-26 15:52:36 +02001431 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001432 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001433 &blkio_policy_cfq, rq_data_dir(rq),
1434 rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001435 if (rq->cmd_flags & REQ_PRIO) {
1436 WARN_ON(!cfqq->prio_pending);
1437 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Jens Axboe165125e2007-07-24 09:28:11 +02001441static int cfq_merge(struct request_queue *q, struct request **req,
1442 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 struct cfq_data *cfqd = q->elevator->elevator_data;
1445 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Jens Axboe206dc692006-03-28 13:03:44 +02001447 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001448 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001449 *req = __rq;
1450 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452
1453 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Jens Axboe165125e2007-07-24 09:28:11 +02001456static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001457 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Jens Axboe21183b02006-07-13 12:33:14 +02001459 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001460 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jens Axboe5e705372006-07-13 12:39:25 +02001462 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Divyesh Shah812d4022010-04-08 21:14:23 -07001466static void cfq_bio_merged(struct request_queue *q, struct request *req,
1467 struct bio *bio)
1468{
Tejun Heo03814112012-03-05 13:15:14 -08001469 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Tejun Heoc1768262012-03-05 13:15:17 -08001470 &blkio_policy_cfq, bio_data_dir(bio),
1471 cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474static void
Jens Axboe165125e2007-07-24 09:28:11 +02001475cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct request *next)
1477{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001478 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001479 struct cfq_data *cfqd = q->elevator->elevator_data;
1480
Jens Axboe22e2c502005-06-27 10:55:12 +02001481 /*
1482 * reposition in fifo if next is older than rq
1483 */
1484 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001485 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001486 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001487 rq_set_fifo_time(rq, rq_fifo_time(next));
1488 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001489
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001490 if (cfqq->next_rq == next)
1491 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001492 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08001493 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001494 &blkio_policy_cfq, rq_data_dir(next),
1495 rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001496
1497 cfqq = RQ_CFQQ(next);
1498 /*
1499 * all requests of this queue are merged to other queues, delete it
1500 * from the service tree. If it's the active_queue,
1501 * cfq_dispatch_requests() will choose to expire it or do idle
1502 */
1503 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1504 cfqq != cfqd->active_queue)
1505 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001506}
1507
Jens Axboe165125e2007-07-24 09:28:11 +02001508static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001509 struct bio *bio)
1510{
1511 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01001512 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001513 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001514
1515 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001516 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001517 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001518 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001519 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001520
1521 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01001522 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01001523 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001524 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01001525 cic = cfq_cic_lookup(cfqd, current->io_context);
1526 if (!cic)
1527 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001528
Vasily Tarasov91fac312007-04-25 12:29:51 +02001529 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001530 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001531}
1532
Divyesh Shah812df482010-04-08 21:15:35 -07001533static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1534{
1535 del_timer(&cfqd->idle_slice_timer);
Tejun Heoc1768262012-03-05 13:15:17 -08001536 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
1537 &blkio_policy_cfq);
Divyesh Shah812df482010-04-08 21:15:35 -07001538}
1539
Jens Axboefebffd62008-01-28 13:19:43 +01001540static void __cfq_set_active_queue(struct cfq_data *cfqd,
1541 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001542{
1543 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001544 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1545 cfqd->serving_prio, cfqd->serving_type);
Tejun Heoc1768262012-03-05 13:15:17 -08001546 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg),
1547 &blkio_policy_cfq);
Justin TerAvest62a37f62011-03-23 08:25:44 +01001548 cfqq->slice_start = 0;
1549 cfqq->dispatch_start = jiffies;
1550 cfqq->allocated_slice = 0;
1551 cfqq->slice_end = 0;
1552 cfqq->slice_dispatch = 0;
1553 cfqq->nr_sectors = 0;
1554
1555 cfq_clear_cfqq_wait_request(cfqq);
1556 cfq_clear_cfqq_must_dispatch(cfqq);
1557 cfq_clear_cfqq_must_alloc_slice(cfqq);
1558 cfq_clear_cfqq_fifo_expire(cfqq);
1559 cfq_mark_cfqq_slice_new(cfqq);
1560
1561 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001562 }
1563
1564 cfqd->active_queue = cfqq;
1565}
1566
1567/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001568 * current cfqq expired its slice (or was too idle), select new one
1569 */
1570static void
1571__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001572 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001573{
Jens Axboe7b679132008-05-30 12:23:07 +02001574 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1575
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001576 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07001577 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001578
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001579 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001580 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001581
1582 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01001583 * If this cfqq is shared between multiple processes, check to
1584 * make sure that those processes are still issuing I/Os within
1585 * the mean seek distance. If not, it may be time to break the
1586 * queues apart again.
1587 */
1588 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1589 cfq_mark_cfqq_split_coop(cfqq);
1590
1591 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001592 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001593 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01001594 if (timed_out) {
1595 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07001596 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001597 else
1598 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001599 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1600 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001601
Vivek Goyale5ff0822010-04-26 19:25:11 +02001602 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001603
Vivek Goyalf04a6422009-12-03 12:59:40 -05001604 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1605 cfq_del_cfqq_rr(cfqd, cfqq);
1606
Jens Axboeedd75ff2007-04-19 12:03:34 +02001607 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001608
1609 if (cfqq == cfqd->active_queue)
1610 cfqd->active_queue = NULL;
1611
1612 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01001613 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001614 cfqd->active_cic = NULL;
1615 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001616}
1617
Vivek Goyale5ff0822010-04-26 19:25:11 +02001618static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001619{
1620 struct cfq_queue *cfqq = cfqd->active_queue;
1621
1622 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02001623 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001624}
1625
Jens Axboe498d3aa22007-04-26 12:54:48 +02001626/*
1627 * Get next queue for service. Unless we have a queue preemption,
1628 * we'll simply select the first cfqq in the service tree.
1629 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001630static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001631{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001632 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001633 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05001634 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001635
Vivek Goyalf04a6422009-12-03 12:59:40 -05001636 if (!cfqd->rq_queued)
1637 return NULL;
1638
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001639 /* There is nothing to dispatch */
1640 if (!service_tree)
1641 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001642 if (RB_EMPTY_ROOT(&service_tree->rb))
1643 return NULL;
1644 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001645}
1646
Vivek Goyalf04a6422009-12-03 12:59:40 -05001647static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1648{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001649 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001650 struct cfq_queue *cfqq;
1651 int i, j;
1652 struct cfq_rb_root *st;
1653
1654 if (!cfqd->rq_queued)
1655 return NULL;
1656
Vivek Goyal25fb5162009-12-03 12:59:46 -05001657 cfqg = cfq_get_next_cfqg(cfqd);
1658 if (!cfqg)
1659 return NULL;
1660
Vivek Goyalf04a6422009-12-03 12:59:40 -05001661 for_each_cfqg_st(cfqg, i, j, st)
1662 if ((cfqq = cfq_rb_first(st)) != NULL)
1663 return cfqq;
1664 return NULL;
1665}
1666
Jens Axboe498d3aa22007-04-26 12:54:48 +02001667/*
1668 * Get and set a new active queue for service.
1669 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001670static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1671 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001672{
Jens Axboee00ef792009-11-04 08:54:55 +01001673 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001674 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001675
Jens Axboe22e2c502005-06-27 10:55:12 +02001676 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001677 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001678}
1679
Jens Axboed9e76202007-04-20 14:27:50 +02001680static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1681 struct request *rq)
1682{
Tejun Heo83096eb2009-05-07 22:24:39 +09001683 if (blk_rq_pos(rq) >= cfqd->last_position)
1684 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001685 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001686 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001687}
1688
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001689static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01001690 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001691{
Shaohua Lie9ce3352010-03-19 08:03:04 +01001692 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001693}
1694
Jens Axboea36e71f2009-04-15 12:15:11 +02001695static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1696 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001697{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001698 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001699 struct rb_node *parent, *node;
1700 struct cfq_queue *__cfqq;
1701 sector_t sector = cfqd->last_position;
1702
1703 if (RB_EMPTY_ROOT(root))
1704 return NULL;
1705
1706 /*
1707 * First, if we find a request starting at the end of the last
1708 * request, choose it.
1709 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001710 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001711 if (__cfqq)
1712 return __cfqq;
1713
1714 /*
1715 * If the exact sector wasn't found, the parent of the NULL leaf
1716 * will contain the closest sector.
1717 */
1718 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001719 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001720 return __cfqq;
1721
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001722 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001723 node = rb_next(&__cfqq->p_node);
1724 else
1725 node = rb_prev(&__cfqq->p_node);
1726 if (!node)
1727 return NULL;
1728
1729 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001730 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001731 return __cfqq;
1732
1733 return NULL;
1734}
1735
1736/*
1737 * cfqd - obvious
1738 * cur_cfqq - passed in so that we don't decide that the current queue is
1739 * closely cooperating with itself.
1740 *
1741 * So, basically we're assuming that that cur_cfqq has dispatched at least
1742 * one request, and that cfqd->last_position reflects a position on the disk
1743 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1744 * assumption.
1745 */
1746static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001747 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001748{
1749 struct cfq_queue *cfqq;
1750
Divyesh Shah39c01b22010-03-25 15:45:57 +01001751 if (cfq_class_idle(cur_cfqq))
1752 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001753 if (!cfq_cfqq_sync(cur_cfqq))
1754 return NULL;
1755 if (CFQQ_SEEKY(cur_cfqq))
1756 return NULL;
1757
Jens Axboea36e71f2009-04-15 12:15:11 +02001758 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01001759 * Don't search priority tree if it's the only queue in the group.
1760 */
1761 if (cur_cfqq->cfqg->nr_cfqq == 1)
1762 return NULL;
1763
1764 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001765 * We should notice if some of the queues are cooperating, eg
1766 * working closely on the same area of the disk. In that case,
1767 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001768 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001769 cfqq = cfqq_close(cfqd, cur_cfqq);
1770 if (!cfqq)
1771 return NULL;
1772
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001773 /* If new queue belongs to different cfq_group, don't choose it */
1774 if (cur_cfqq->cfqg != cfqq->cfqg)
1775 return NULL;
1776
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001777 /*
1778 * It only makes sense to merge sync queues.
1779 */
1780 if (!cfq_cfqq_sync(cfqq))
1781 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001782 if (CFQQ_SEEKY(cfqq))
1783 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001784
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001785 /*
1786 * Do not merge queues of different priority classes
1787 */
1788 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1789 return NULL;
1790
Jens Axboea36e71f2009-04-15 12:15:11 +02001791 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001792}
1793
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001794/*
1795 * Determine whether we should enforce idle window for this queue.
1796 */
1797
1798static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1799{
1800 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001801 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001802
Vivek Goyalf04a6422009-12-03 12:59:40 -05001803 BUG_ON(!service_tree);
1804 BUG_ON(!service_tree->count);
1805
Vivek Goyalb6508c12010-08-23 12:23:33 +02001806 if (!cfqd->cfq_slice_idle)
1807 return false;
1808
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001809 /* We never do for idle class queues. */
1810 if (prio == IDLE_WORKLOAD)
1811 return false;
1812
1813 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01001814 if (cfq_cfqq_idle_window(cfqq) &&
1815 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001816 return true;
1817
1818 /*
1819 * Otherwise, we do only if they are the last ones
1820 * in their service tree.
1821 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02001822 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
1823 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01001824 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001825 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1826 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01001827 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001828}
1829
Jens Axboe6d048f52007-04-25 12:44:27 +02001830static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001831{
Jens Axboe17926692007-01-19 11:59:30 +11001832 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01001833 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001834 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001835
Jens Axboea68bbdd2008-09-24 13:03:33 +02001836 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001837 * SSD device without seek penalty, disable idling. But only do so
1838 * for devices that support queuing, otherwise we still have a problem
1839 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001840 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001841 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001842 return;
1843
Jens Axboedd67d052006-06-21 09:36:18 +02001844 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001845 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001846
1847 /*
1848 * idle is disabled, either manually or by past process history
1849 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001850 if (!cfq_should_idle(cfqd, cfqq)) {
1851 /* no queue idling. Check for group idling */
1852 if (cfqd->cfq_group_idle)
1853 group_idle = cfqd->cfq_group_idle;
1854 else
1855 return;
1856 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001857
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001859 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001860 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001861 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001862 return;
1863
1864 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001865 * task has exited, don't wait
1866 */
Jens Axboe206dc692006-03-28 13:03:44 +02001867 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08001868 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02001869 return;
1870
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001871 /*
1872 * If our average think time is larger than the remaining time
1873 * slice, then don't idle. This avoids overrunning the allotted
1874 * time slice.
1875 */
Shaohua Li383cd722011-07-12 14:24:35 +02001876 if (sample_valid(cic->ttime.ttime_samples) &&
1877 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02001878 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02001879 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001880 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001881 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001882
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001883 /* There are other queues in the group, don't do group idle */
1884 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
1885 return;
1886
Jens Axboe3b181522005-06-27 10:56:24 +02001887 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001888
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001889 if (group_idle)
1890 sl = cfqd->cfq_group_idle;
1891 else
1892 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001893
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001894 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heoc1768262012-03-05 13:15:17 -08001895 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
1896 &blkio_policy_cfq);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001897 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
1898 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Jens Axboe498d3aa22007-04-26 12:54:48 +02001901/*
1902 * Move request from internal lists to the request queue dispatch list.
1903 */
Jens Axboe165125e2007-07-24 09:28:11 +02001904static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001906 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001907 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001908
Jens Axboe7b679132008-05-30 12:23:07 +02001909 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1910
Jeff Moyer06d21882009-09-11 17:08:59 +02001911 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001912 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001913 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02001914 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001915 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001916
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001917 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02001918 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001919 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08001920 &blkio_policy_cfq, blk_rq_bytes(rq),
1921 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924/*
1925 * return expired entry, or NULL to just start from scratch in rbtree
1926 */
Jens Axboefebffd62008-01-28 13:19:43 +01001927static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Jens Axboe30996f42009-10-05 11:03:39 +02001929 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Jens Axboe3b181522005-06-27 10:56:24 +02001931 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001933
1934 cfq_mark_cfqq_fifo_expire(cfqq);
1935
Jens Axboe89850f72006-07-22 16:48:31 +02001936 if (list_empty(&cfqq->fifo))
1937 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jens Axboe89850f72006-07-22 16:48:31 +02001939 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001940 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001941 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Jens Axboe30996f42009-10-05 11:03:39 +02001943 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001944 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Jens Axboe22e2c502005-06-27 10:55:12 +02001947static inline int
1948cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1949{
1950 const int base_rq = cfqd->cfq_slice_async_rq;
1951
1952 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1953
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02001954 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001955}
1956
1957/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001958 * Must be called with the queue_lock held.
1959 */
1960static int cfqq_process_refs(struct cfq_queue *cfqq)
1961{
1962 int process_refs, io_refs;
1963
1964 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01001965 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001966 BUG_ON(process_refs < 0);
1967 return process_refs;
1968}
1969
1970static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1971{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001972 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001973 struct cfq_queue *__cfqq;
1974
Jeff Moyerc10b61f2010-06-17 10:19:11 -04001975 /*
1976 * If there are no process references on the new_cfqq, then it is
1977 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
1978 * chain may have dropped their last reference (not just their
1979 * last process reference).
1980 */
1981 if (!cfqq_process_refs(new_cfqq))
1982 return;
1983
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001984 /* Avoid a circular list and skip interim queue merges */
1985 while ((__cfqq = new_cfqq->new_cfqq)) {
1986 if (__cfqq == cfqq)
1987 return;
1988 new_cfqq = __cfqq;
1989 }
1990
1991 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04001992 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001993 /*
1994 * If the process for the cfqq has gone away, there is no
1995 * sense in merging the queues.
1996 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04001997 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001998 return;
1999
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002000 /*
2001 * Merge in the direction of the lesser amount of work.
2002 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002003 if (new_process_refs >= process_refs) {
2004 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002005 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002006 } else {
2007 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002008 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002009 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002010}
2011
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002012static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002013 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002014{
2015 struct cfq_queue *queue;
2016 int i;
2017 bool key_valid = false;
2018 unsigned long lowest_key = 0;
2019 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2020
Vivek Goyal65b32a52009-12-16 17:52:59 -05002021 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2022 /* select the one with lowest rb_key */
2023 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002024 if (queue &&
2025 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2026 lowest_key = queue->rb_key;
2027 cur_best = i;
2028 key_valid = true;
2029 }
2030 }
2031
2032 return cur_best;
2033}
2034
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002035static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002036{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002037 unsigned slice;
2038 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002039 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002040 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002041 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002042
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002043 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002044 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002045 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002046 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002047 cfqd->serving_prio = BE_WORKLOAD;
2048 else {
2049 cfqd->serving_prio = IDLE_WORKLOAD;
2050 cfqd->workload_expires = jiffies + 1;
2051 return;
2052 }
2053
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002054 if (original_prio != cfqd->serving_prio)
2055 goto new_workload;
2056
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002057 /*
2058 * For RT and BE, we have to choose also the type
2059 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2060 * expiration time
2061 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002062 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002063 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002064
2065 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002066 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002067 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002068 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002069 return;
2070
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002071new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002072 /* otherwise select new workload type */
2073 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002074 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2075 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002076 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002077
2078 /*
2079 * the workload slice is computed as a fraction of target latency
2080 * proportional to the number of queues in that workload, over
2081 * all the queues in the same priority class
2082 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002083 group_slice = cfq_group_slice(cfqd, cfqg);
2084
2085 slice = group_slice * count /
2086 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2087 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002088
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002089 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2090 unsigned int tmp;
2091
2092 /*
2093 * Async queues are currently system wide. Just taking
2094 * proportion of queues with-in same group will lead to higher
2095 * async ratio system wide as generally root group is going
2096 * to have higher weight. A more accurate thing would be to
2097 * calculate system wide asnc/sync ratio.
2098 */
2099 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2100 tmp = tmp/cfqd->busy_queues;
2101 slice = min_t(unsigned, slice, tmp);
2102
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002103 /* async workload slice is scaled down according to
2104 * the sync/async slice ratio. */
2105 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002106 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002107 /* sync workload slice is at least 2 * cfq_slice_idle */
2108 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2109
2110 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002111 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002112 cfqd->workload_expires = jiffies + slice;
2113}
2114
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002115static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2116{
2117 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002118 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002119
2120 if (RB_EMPTY_ROOT(&st->rb))
2121 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002122 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002123 update_min_vdisktime(st);
2124 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002125}
2126
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002127static void cfq_choose_cfqg(struct cfq_data *cfqd)
2128{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002129 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2130
2131 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002132
2133 /* Restore the workload type data */
2134 if (cfqg->saved_workload_slice) {
2135 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2136 cfqd->serving_type = cfqg->saved_workload;
2137 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002138 } else
2139 cfqd->workload_expires = jiffies - 1;
2140
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002141 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002142}
2143
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002144/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002145 * Select a queue for service. If we have a current active queue,
2146 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002147 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002148static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002149{
Jens Axboea36e71f2009-04-15 12:15:11 +02002150 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002151
2152 cfqq = cfqd->active_queue;
2153 if (!cfqq)
2154 goto new_queue;
2155
Vivek Goyalf04a6422009-12-03 12:59:40 -05002156 if (!cfqd->rq_queued)
2157 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002158
2159 /*
2160 * We were waiting for group to get backlogged. Expire the queue
2161 */
2162 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2163 goto expire;
2164
Jens Axboe22e2c502005-06-27 10:55:12 +02002165 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002166 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002167 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002168 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2169 /*
2170 * If slice had not expired at the completion of last request
2171 * we might not have turned on wait_busy flag. Don't expire
2172 * the queue yet. Allow the group to get backlogged.
2173 *
2174 * The very fact that we have used the slice, that means we
2175 * have been idling all along on this queue and it should be
2176 * ok to wait for this request to complete.
2177 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002178 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2179 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2180 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002181 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002182 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002183 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002184 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002185
2186 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002187 * The active queue has requests and isn't expired, allow it to
2188 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002189 */
Jens Axboedd67d052006-06-21 09:36:18 +02002190 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002191 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002192
2193 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002194 * If another queue has a request waiting within our mean seek
2195 * distance, let it run. The expire code will check for close
2196 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002197 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002198 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002199 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002200 if (new_cfqq) {
2201 if (!cfqq->new_cfqq)
2202 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002203 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002204 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002205
2206 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002207 * No requests pending. If the active queue still has requests in
2208 * flight or is idling for a new request, allow either of these
2209 * conditions to happen (or time out) before selecting a new queue.
2210 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002211 if (timer_pending(&cfqd->idle_slice_timer)) {
2212 cfqq = NULL;
2213 goto keep_queue;
2214 }
2215
Shaohua Li8e1ac662010-11-08 15:01:04 +01002216 /*
2217 * This is a deep seek queue, but the device is much faster than
2218 * the queue can deliver, don't idle
2219 **/
2220 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2221 (cfq_cfqq_slice_new(cfqq) ||
2222 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2223 cfq_clear_cfqq_deep(cfqq);
2224 cfq_clear_cfqq_idle_window(cfqq);
2225 }
2226
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002227 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2228 cfqq = NULL;
2229 goto keep_queue;
2230 }
2231
2232 /*
2233 * If group idle is enabled and there are requests dispatched from
2234 * this group, wait for requests to complete.
2235 */
2236check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002237 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2238 cfqq->cfqg->dispatched &&
2239 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002240 cfqq = NULL;
2241 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002242 }
2243
Jens Axboe3b181522005-06-27 10:56:24 +02002244expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002245 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002246new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002247 /*
2248 * Current queue expired. Check if we have to switch to a new
2249 * service tree
2250 */
2251 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002252 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002253
Jens Axboea36e71f2009-04-15 12:15:11 +02002254 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002255keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002256 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002257}
2258
Jens Axboefebffd62008-01-28 13:19:43 +01002259static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002260{
2261 int dispatched = 0;
2262
2263 while (cfqq->next_rq) {
2264 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2265 dispatched++;
2266 }
2267
2268 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002269
2270 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002271 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002272 return dispatched;
2273}
2274
Jens Axboe498d3aa22007-04-26 12:54:48 +02002275/*
2276 * Drain our current requests. Used for barriers and when switching
2277 * io schedulers on-the-fly.
2278 */
Jens Axboed9e76202007-04-20 14:27:50 +02002279static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002280{
Jens Axboe08717142008-01-28 11:38:15 +01002281 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002282 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002283
Divyesh Shah3440c492010-04-09 09:29:57 +02002284 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002285 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002286 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2287 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002288 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002289 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002290
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002291 BUG_ON(cfqd->busy_queues);
2292
Jeff Moyer69237152009-06-12 15:29:30 +02002293 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002294 return dispatched;
2295}
2296
Shaohua Liabc3c742010-03-01 09:20:54 +01002297static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2298 struct cfq_queue *cfqq)
2299{
2300 /* the queue hasn't finished any request, can't estimate */
2301 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002302 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002303 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2304 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002305 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002306
Shaohua Lic1e44752010-11-08 15:01:02 +01002307 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002308}
2309
Jens Axboe0b182d62009-10-06 20:49:37 +02002310static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002311{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002312 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Jens Axboe2f5cb732009-04-07 08:51:19 +02002314 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002315 * Drain async requests before we start sync IO
2316 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002317 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002318 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002319
2320 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002321 * If this is an async queue and we have sync IO in flight, let it wait
2322 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002323 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002324 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002325
Shaohua Liabc3c742010-03-01 09:20:54 +01002326 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002327 if (cfq_class_idle(cfqq))
2328 max_dispatch = 1;
2329
2330 /*
2331 * Does this cfqq already have too much IO in flight?
2332 */
2333 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002334 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002335 /*
2336 * idle queue must always only have a single IO in flight
2337 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002338 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002339 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002340
Jens Axboe2f5cb732009-04-07 08:51:19 +02002341 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002342 * If there is only one sync queue
2343 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002344 * queue no dispatch limit. The reason is a sync queue can
2345 * preempt async queue, limiting the sync queue doesn't make
2346 * sense. This is useful for aiostress test.
2347 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002348 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2349 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002350
2351 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002352 * We have other queues, don't allow more IO from this one
2353 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002354 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2355 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002356 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002357
Jens Axboe2f5cb732009-04-07 08:51:19 +02002358 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002359 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002360 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002361 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002362 max_dispatch = -1;
2363 else
2364 /*
2365 * Normally we start throttling cfqq when cfq_quantum/2
2366 * requests have been dispatched. But we can drive
2367 * deeper queue depths at the beginning of slice
2368 * subjected to upper limit of cfq_quantum.
2369 * */
2370 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002371 }
2372
2373 /*
2374 * Async queues must wait a bit before being allowed dispatch.
2375 * We also ramp up the dispatch depth gradually for async IO,
2376 * based on the last sync IO we serviced
2377 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002378 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002379 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002380 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002381
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002382 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002383 if (!depth && !cfqq->dispatched)
2384 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002385 if (depth < max_dispatch)
2386 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388
Jens Axboe0b182d62009-10-06 20:49:37 +02002389 /*
2390 * If we're below the current max, allow a dispatch
2391 */
2392 return cfqq->dispatched < max_dispatch;
2393}
2394
2395/*
2396 * Dispatch a request from cfqq, moving them to the request queue
2397 * dispatch list.
2398 */
2399static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2400{
2401 struct request *rq;
2402
2403 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2404
2405 if (!cfq_may_dispatch(cfqd, cfqq))
2406 return false;
2407
2408 /*
2409 * follow expired path, else get first next available
2410 */
2411 rq = cfq_check_fifo(cfqq);
2412 if (!rq)
2413 rq = cfqq->next_rq;
2414
2415 /*
2416 * insert request into driver dispatch list
2417 */
2418 cfq_dispatch_insert(cfqd->queue, rq);
2419
2420 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002421 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002422
Tejun Heoc5869802011-12-14 00:33:41 +01002423 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002424 cfqd->active_cic = cic;
2425 }
2426
2427 return true;
2428}
2429
2430/*
2431 * Find the cfqq that we need to service and move a request from that to the
2432 * dispatch list
2433 */
2434static int cfq_dispatch_requests(struct request_queue *q, int force)
2435{
2436 struct cfq_data *cfqd = q->elevator->elevator_data;
2437 struct cfq_queue *cfqq;
2438
2439 if (!cfqd->busy_queues)
2440 return 0;
2441
2442 if (unlikely(force))
2443 return cfq_forced_dispatch(cfqd);
2444
2445 cfqq = cfq_select_queue(cfqd);
2446 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002447 return 0;
2448
Jens Axboe2f5cb732009-04-07 08:51:19 +02002449 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002450 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002451 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002452 if (!cfq_dispatch_request(cfqd, cfqq))
2453 return 0;
2454
Jens Axboe2f5cb732009-04-07 08:51:19 +02002455 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002456 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002457
2458 /*
2459 * expire an async queue immediately if it has used up its slice. idle
2460 * queue always expire after 1 dispatch round.
2461 */
2462 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2463 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2464 cfq_class_idle(cfqq))) {
2465 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002466 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002467 }
2468
Shan Weib217a902009-09-01 10:06:42 +02002469 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002470 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473/*
Jens Axboe5e705372006-07-13 12:39:25 +02002474 * task holds one reference to the queue, dropped when task exits. each rq
2475 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002477 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * queue lock must be held here.
2479 */
2480static void cfq_put_queue(struct cfq_queue *cfqq)
2481{
Jens Axboe22e2c502005-06-27 10:55:12 +02002482 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002483 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002484
Shaohua Li30d7b942011-01-07 08:46:59 +01002485 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Shaohua Li30d7b942011-01-07 08:46:59 +01002487 cfqq->ref--;
2488 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return;
2490
Jens Axboe7b679132008-05-30 12:23:07 +02002491 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002493 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002494 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002496 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002497 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002498 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002499 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002500
Vivek Goyalf04a6422009-12-03 12:59:40 -05002501 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 kmem_cache_free(cfq_pool, cfqq);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08002503 blkg_put(cfqg_to_blkg(cfqg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Shaohua Lid02a2c02010-05-25 10:16:53 +02002506static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002507{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002508 struct cfq_queue *__cfqq, *next;
2509
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002510 /*
2511 * If this queue was scheduled to merge with another queue, be
2512 * sure to drop the reference taken on that queue (and others in
2513 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2514 */
2515 __cfqq = cfqq->new_cfqq;
2516 while (__cfqq) {
2517 if (__cfqq == cfqq) {
2518 WARN(1, "cfqq->new_cfqq loop detected\n");
2519 break;
2520 }
2521 next = __cfqq->new_cfqq;
2522 cfq_put_queue(__cfqq);
2523 __cfqq = next;
2524 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02002525}
2526
2527static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2528{
2529 if (unlikely(cfqq == cfqd->active_queue)) {
2530 __cfq_slice_expired(cfqd, cfqq, 0);
2531 cfq_schedule_dispatch(cfqd);
2532 }
2533
2534 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002535
Jens Axboe89850f72006-07-22 16:48:31 +02002536 cfq_put_queue(cfqq);
2537}
2538
Tejun Heo9b84cac2011-12-14 00:33:42 +01002539static void cfq_init_icq(struct io_cq *icq)
2540{
2541 struct cfq_io_cq *cic = icq_to_cic(icq);
2542
2543 cic->ttime.last_end_request = jiffies;
2544}
2545
Tejun Heoc5869802011-12-14 00:33:41 +01002546static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02002547{
Tejun Heoc5869802011-12-14 00:33:41 +01002548 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01002549 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002550
Jens Axboeff6657c2009-04-08 10:58:57 +02002551 if (cic->cfqq[BLK_RW_ASYNC]) {
2552 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2553 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002554 }
2555
Jens Axboeff6657c2009-04-08 10:58:57 +02002556 if (cic->cfqq[BLK_RW_SYNC]) {
2557 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2558 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002559 }
Jens Axboe89850f72006-07-22 16:48:31 +02002560}
2561
Jens Axboefd0928d2008-01-24 08:52:45 +01002562static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002563{
2564 struct task_struct *tsk = current;
2565 int ioprio_class;
2566
Jens Axboe3b181522005-06-27 10:56:24 +02002567 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002568 return;
2569
Jens Axboefd0928d2008-01-24 08:52:45 +01002570 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002571 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002572 default:
2573 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2574 case IOPRIO_CLASS_NONE:
2575 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002576 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002577 */
2578 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002579 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002580 break;
2581 case IOPRIO_CLASS_RT:
2582 cfqq->ioprio = task_ioprio(ioc);
2583 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2584 break;
2585 case IOPRIO_CLASS_BE:
2586 cfqq->ioprio = task_ioprio(ioc);
2587 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2588 break;
2589 case IOPRIO_CLASS_IDLE:
2590 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2591 cfqq->ioprio = 7;
2592 cfq_clear_cfqq_idle_window(cfqq);
2593 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002594 }
2595
2596 /*
2597 * keep track of original prio settings in case we have to temporarily
2598 * elevate the priority of this queue
2599 */
2600 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02002601 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002602}
2603
Tejun Heoc5869802011-12-14 00:33:41 +01002604static void changed_ioprio(struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002605{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002606 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05002607 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02002608
Jens Axboecaaa5f92006-06-16 11:23:00 +02002609 if (unlikely(!cfqd))
2610 return;
2611
Jens Axboeff6657c2009-04-08 10:58:57 +02002612 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002613 if (cfqq) {
2614 struct cfq_queue *new_cfqq;
Tejun Heoc5869802011-12-14 00:33:41 +01002615 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->icq.ioc,
Jens Axboeff6657c2009-04-08 10:58:57 +02002616 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002617 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002618 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002619 cfq_put_queue(cfqq);
2620 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002621 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002622
Jens Axboeff6657c2009-04-08 10:58:57 +02002623 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002624 if (cfqq)
2625 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002626}
2627
Jens Axboed5036d72009-06-26 10:44:34 +02002628static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002629 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002630{
2631 RB_CLEAR_NODE(&cfqq->rb_node);
2632 RB_CLEAR_NODE(&cfqq->p_node);
2633 INIT_LIST_HEAD(&cfqq->fifo);
2634
Shaohua Li30d7b942011-01-07 08:46:59 +01002635 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02002636 cfqq->cfqd = cfqd;
2637
2638 cfq_mark_cfqq_prio_changed(cfqq);
2639
2640 if (is_sync) {
2641 if (!cfq_class_idle(cfqq))
2642 cfq_mark_cfqq_idle_window(cfqq);
2643 cfq_mark_cfqq_sync(cfqq);
2644 }
2645 cfqq->pid = pid;
2646}
2647
Vivek Goyal246103332009-12-03 12:59:51 -05002648#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heoc5869802011-12-14 00:33:41 +01002649static void changed_cgroup(struct cfq_io_cq *cic)
Vivek Goyal246103332009-12-03 12:59:51 -05002650{
2651 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002652 struct cfq_data *cfqd = cic_to_cfqd(cic);
Vivek Goyal246103332009-12-03 12:59:51 -05002653 struct request_queue *q;
2654
2655 if (unlikely(!cfqd))
2656 return;
2657
2658 q = cfqd->queue;
2659
Vivek Goyal246103332009-12-03 12:59:51 -05002660 if (sync_cfqq) {
2661 /*
2662 * Drop reference to sync queue. A new sync queue will be
2663 * assigned in new group upon arrival of a fresh request.
2664 */
2665 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2666 cic_set_cfqq(cic, NULL, 1);
2667 cfq_put_queue(sync_cfqq);
2668 }
Vivek Goyal246103332009-12-03 12:59:51 -05002669}
Vivek Goyal246103332009-12-03 12:59:51 -05002670#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002673cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002674 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002676 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct cfq_queue *cfqq, *new_cfqq = NULL;
Tejun Heoc5869802011-12-14 00:33:41 +01002678 struct cfq_io_cq *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002679 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08002682 rcu_read_lock();
2683
Tejun Heo0a5a7d02012-03-05 13:15:02 -08002684 blkcg = task_blkio_cgroup(current);
2685
Tejun Heocd1604f2012-03-05 13:15:06 -08002686 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
2687
Jens Axboe4ac845a2008-01-24 08:44:49 +01002688 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002689 /* cic always exists here */
2690 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Jens Axboe6118b702009-06-30 09:34:12 +02002692 /*
2693 * Always try a new alloc if we fell back to the OOM cfqq
2694 * originally, since it should just be a temporary situation.
2695 */
2696 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2697 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (new_cfqq) {
2699 cfqq = new_cfqq;
2700 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002701 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08002702 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002704 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002705 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002706 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002708 if (new_cfqq)
2709 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002710 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002711 cfqq = kmem_cache_alloc_node(cfq_pool,
2712 gfp_mask | __GFP_ZERO,
2713 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Jens Axboe6118b702009-06-30 09:34:12 +02002716 if (cfqq) {
2717 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2718 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002719 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002720 cfq_log_cfqq(cfqd, cfqq, "alloced");
2721 } else
2722 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724
2725 if (new_cfqq)
2726 kmem_cache_free(cfq_pool, new_cfqq);
2727
Tejun Heo2a7f1242012-03-05 13:15:01 -08002728 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return cfqq;
2730}
2731
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002732static struct cfq_queue **
2733cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2734{
Jens Axboefe094d92008-01-31 13:08:54 +01002735 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002736 case IOPRIO_CLASS_RT:
2737 return &cfqd->async_cfqq[0][ioprio];
2738 case IOPRIO_CLASS_BE:
2739 return &cfqd->async_cfqq[1][ioprio];
2740 case IOPRIO_CLASS_IDLE:
2741 return &cfqd->async_idle_cfqq;
2742 default:
2743 BUG();
2744 }
2745}
2746
Jens Axboe15c31be2007-07-10 13:43:25 +02002747static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002748cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002749 gfp_t gfp_mask)
2750{
Jens Axboefd0928d2008-01-24 08:52:45 +01002751 const int ioprio = task_ioprio(ioc);
2752 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002753 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002754 struct cfq_queue *cfqq = NULL;
2755
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002756 if (!is_sync) {
2757 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2758 cfqq = *async_cfqq;
2759 }
2760
Jens Axboe6118b702009-06-30 09:34:12 +02002761 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002762 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002763
2764 /*
2765 * pin the queue now that it's allocated, scheduler exit will prune it
2766 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002767 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01002768 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002769 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002770 }
2771
Shaohua Li30d7b942011-01-07 08:46:59 +01002772 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02002773 return cfqq;
2774}
2775
Jens Axboe22e2c502005-06-27 10:55:12 +02002776static void
Shaohua Li383cd722011-07-12 14:24:35 +02002777__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002778{
Shaohua Li383cd722011-07-12 14:24:35 +02002779 unsigned long elapsed = jiffies - ttime->last_end_request;
2780 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002781
Shaohua Li383cd722011-07-12 14:24:35 +02002782 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
2783 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
2784 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
2785}
2786
2787static void
2788cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002789 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02002790{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002791 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02002792 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002793 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
2794 cfqd->cfq_slice_idle);
2795 }
Shaohua Li7700fc42011-07-12 14:24:56 +02002796#ifdef CONFIG_CFQ_GROUP_IOSCHED
2797 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
2798#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02002799}
2800
Jens Axboe206dc692006-03-28 13:03:44 +02002801static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002802cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002803 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002804{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002805 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002806 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002807 if (cfqq->last_request_pos) {
2808 if (cfqq->last_request_pos < blk_rq_pos(rq))
2809 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
2810 else
2811 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
2812 }
Jens Axboe206dc692006-03-28 13:03:44 +02002813
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01002814 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01002815 if (blk_queue_nonrot(cfqd->queue))
2816 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
2817 else
2818 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02002819}
Jens Axboe22e2c502005-06-27 10:55:12 +02002820
2821/*
2822 * Disable idle window if the process thinks too long or seeks so much that
2823 * it doesn't matter
2824 */
2825static void
2826cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01002827 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002828{
Jens Axboe7b679132008-05-30 12:23:07 +02002829 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002830
Jens Axboe08717142008-01-28 11:38:15 +01002831 /*
2832 * Don't idle for async or idle io prio class
2833 */
2834 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002835 return;
2836
Jens Axboec265a7f2008-06-26 13:49:33 +02002837 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002838
Corrado Zoccolo76280af2009-11-26 10:02:58 +01002839 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
2840 cfq_mark_cfqq_deep(cfqq);
2841
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02002842 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
2843 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08002844 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01002845 !cfqd->cfq_slice_idle ||
2846 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002847 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02002848 else if (sample_valid(cic->ttime.ttime_samples)) {
2849 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002850 enable_idle = 0;
2851 else
2852 enable_idle = 1;
2853 }
2854
Jens Axboe7b679132008-05-30 12:23:07 +02002855 if (old_idle != enable_idle) {
2856 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2857 if (enable_idle)
2858 cfq_mark_cfqq_idle_window(cfqq);
2859 else
2860 cfq_clear_cfqq_idle_window(cfqq);
2861 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002862}
2863
Jens Axboe22e2c502005-06-27 10:55:12 +02002864/*
2865 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2866 * no or if we aren't sure, a 1 will cause a preempt.
2867 */
Jens Axboea6151c32009-10-07 20:02:57 +02002868static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002869cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002870 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002871{
Jens Axboe6d048f52007-04-25 12:44:27 +02002872 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002873
Jens Axboe6d048f52007-04-25 12:44:27 +02002874 cfqq = cfqd->active_queue;
2875 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002876 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002877
Jens Axboe6d048f52007-04-25 12:44:27 +02002878 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002879 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002880
2881 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002882 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002883
Jens Axboe22e2c502005-06-27 10:55:12 +02002884 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08002885 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
2886 */
2887 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
2888 return false;
2889
2890 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002891 * if the new request is sync, but the currently running queue is
2892 * not, let the sync request have priority.
2893 */
Jens Axboe5e705372006-07-13 12:39:25 +02002894 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002895 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002896
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002897 if (new_cfqq->cfqg != cfqq->cfqg)
2898 return false;
2899
2900 if (cfq_slice_used(cfqq))
2901 return true;
2902
2903 /* Allow preemption only if we are idling on sync-noidle tree */
2904 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2905 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
2906 new_cfqq->service_tree->count == 2 &&
2907 RB_EMPTY_ROOT(&cfqq->sort_list))
2908 return true;
2909
Jens Axboe374f84a2006-07-23 01:42:19 +02002910 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002911 * So both queues are sync. Let the new request get disk time if
2912 * it's a metadata request and the current queue is doing regular IO.
2913 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02002914 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002915 return true;
2916
2917 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002918 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2919 */
2920 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002921 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002922
Shaohua Lid2d59e12010-11-08 15:01:03 +01002923 /* An idle queue should not be idle now for some reason */
2924 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
2925 return true;
2926
Jens Axboe1e3335d2007-02-14 19:59:49 +01002927 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002928 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002929
2930 /*
2931 * if this request is as-good as one we would expect from the
2932 * current cfqq, let it preempt
2933 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01002934 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002935 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002936
Jens Axboea6151c32009-10-07 20:02:57 +02002937 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002938}
2939
2940/*
2941 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2942 * let it have half of its nominal slice.
2943 */
2944static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2945{
Shaohua Lidf0793a2012-01-19 09:20:09 +01002946 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
2947
Jens Axboe7b679132008-05-30 12:23:07 +02002948 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01002949 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02002950
Jens Axboebf572252006-07-19 20:29:12 +02002951 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01002952 * workload type is changed, don't save slice, otherwise preempt
2953 * doesn't happen
2954 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01002955 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e32011-01-14 08:41:02 +01002956 cfqq->cfqg->saved_workload_slice = 0;
2957
2958 /*
Jens Axboebf572252006-07-19 20:29:12 +02002959 * Put the new queue at the front of the of the current list,
2960 * so we know that it will be selected next.
2961 */
2962 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02002963
2964 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01002965
Justin TerAvest62a37f62011-03-23 08:25:44 +01002966 cfqq->slice_end = 0;
2967 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002968}
2969
2970/*
Jens Axboe5e705372006-07-13 12:39:25 +02002971 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02002972 * something we should do about it
2973 */
2974static void
Jens Axboe5e705372006-07-13 12:39:25 +02002975cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2976 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002977{
Tejun Heoc5869802011-12-14 00:33:41 +01002978 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02002979
Aaron Carroll45333d52008-08-26 15:52:36 +02002980 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02002981 if (rq->cmd_flags & REQ_PRIO)
2982 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02002983
Shaohua Li383cd722011-07-12 14:24:35 +02002984 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002985 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02002986 cfq_update_idle_window(cfqd, cfqq, cic);
2987
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002988 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002989
2990 if (cfqq == cfqd->active_queue) {
2991 /*
Jens Axboeb0291952009-04-07 11:38:31 +02002992 * Remember that we saw a request from this process, but
2993 * don't start queuing just yet. Otherwise we risk seeing lots
2994 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02002995 * and merging. If the request is already larger than a single
2996 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02002997 * merging is already done. Ditto for a busy system that
2998 * has other work pending, don't risk delaying until the
2999 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003000 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003001 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003002 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3003 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003004 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003005 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003006 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003007 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003008 cfq_blkiocg_update_idle_time_stats(
Tejun Heoc1768262012-03-05 13:15:17 -08003009 cfqg_to_blkg(cfqq->cfqg),
3010 &blkio_policy_cfq);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003011 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003012 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003013 }
Jens Axboe5e705372006-07-13 12:39:25 +02003014 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003015 /*
3016 * not the active queue - expire current slice if it is
3017 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003018 * has some old slice time left and is of higher priority or
3019 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003020 */
3021 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003022 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003023 }
3024}
3025
Jens Axboe165125e2007-07-24 09:28:11 +02003026static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003027{
Jens Axboeb4878f22005-10-20 16:42:29 +02003028 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003029 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003030
Jens Axboe7b679132008-05-30 12:23:07 +02003031 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoc5869802011-12-14 00:33:41 +01003032 cfq_init_prio_data(cfqq, RQ_CIC(rq)->icq.ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Jens Axboe30996f42009-10-05 11:03:39 +02003034 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003035 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003036 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003037 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08003038 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08003039 cfqg_to_blkg(cfqd->serving_group),
3040 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003041 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Aaron Carroll45333d52008-08-26 15:52:36 +02003044/*
3045 * Update hw_tag based on peak queue depth over 50 samples under
3046 * sufficient load.
3047 */
3048static void cfq_update_hw_tag(struct cfq_data *cfqd)
3049{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003050 struct cfq_queue *cfqq = cfqd->active_queue;
3051
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003052 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3053 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003054
3055 if (cfqd->hw_tag == 1)
3056 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003057
3058 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003059 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003060 return;
3061
Shaohua Li1a1238a2009-10-27 08:46:23 +01003062 /*
3063 * If active queue hasn't enough requests and can idle, cfq might not
3064 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3065 * case
3066 */
3067 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3068 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003069 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003070 return;
3071
Aaron Carroll45333d52008-08-26 15:52:36 +02003072 if (cfqd->hw_tag_samples++ < 50)
3073 return;
3074
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003075 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003076 cfqd->hw_tag = 1;
3077 else
3078 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003079}
3080
Vivek Goyal7667aa02009-12-08 17:52:58 -05003081static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3082{
Tejun Heoc5869802011-12-14 00:33:41 +01003083 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003084
Justin TerAvest02a8f012011-02-09 14:20:03 +01003085 /* If the queue already has requests, don't wait */
3086 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3087 return false;
3088
Vivek Goyal7667aa02009-12-08 17:52:58 -05003089 /* If there are other queues in the group, don't wait */
3090 if (cfqq->cfqg->nr_cfqq > 1)
3091 return false;
3092
Shaohua Li7700fc42011-07-12 14:24:56 +02003093 /* the only queue in the group, but think time is big */
3094 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3095 return false;
3096
Vivek Goyal7667aa02009-12-08 17:52:58 -05003097 if (cfq_slice_used(cfqq))
3098 return true;
3099
3100 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003101 if (cic && sample_valid(cic->ttime.ttime_samples)
3102 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003103 return true;
3104
3105 /*
3106 * If think times is less than a jiffy than ttime_mean=0 and above
3107 * will not be true. It might happen that slice has not expired yet
3108 * but will expire soon (4-5 ns) during select_queue(). To cover the
3109 * case where think time is less than a jiffy, mark the queue wait
3110 * busy if only 1 jiffy is left in the slice.
3111 */
3112 if (cfqq->slice_end - jiffies == 1)
3113 return true;
3114
3115 return false;
3116}
3117
Jens Axboe165125e2007-07-24 09:28:11 +02003118static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Jens Axboe5e705372006-07-13 12:39:25 +02003120 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003121 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003122 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003123 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Jens Axboeb4878f22005-10-20 16:42:29 +02003125 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003126 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3127 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Aaron Carroll45333d52008-08-26 15:52:36 +02003129 cfq_update_hw_tag(cfqd);
3130
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003131 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003132 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003133 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003134 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003135 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003136 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08003137 &blkio_policy_cfq, rq_start_time_ns(rq),
3138 rq_io_start_time_ns(rq), rq_data_dir(rq),
3139 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003141 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003142
Vivek Goyal365722b2009-10-03 15:21:27 +02003143 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003144 struct cfq_rb_root *service_tree;
3145
Shaohua Li383cd722011-07-12 14:24:35 +02003146 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003147
3148 if (cfq_cfqq_on_rr(cfqq))
3149 service_tree = cfqq->service_tree;
3150 else
3151 service_tree = service_tree_for(cfqq->cfqg,
3152 cfqq_prio(cfqq), cfqq_type(cfqq));
3153 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003154 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3155 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003156 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003157
Shaohua Li7700fc42011-07-12 14:24:56 +02003158#ifdef CONFIG_CFQ_GROUP_IOSCHED
3159 cfqq->cfqg->ttime.last_end_request = now;
3160#endif
3161
Jens Axboecaaa5f92006-06-16 11:23:00 +02003162 /*
3163 * If this is the active queue, check if it needs to be expired,
3164 * or if we want to idle in case it has no pending requests.
3165 */
3166 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003167 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3168
Jens Axboe44f7c162007-01-19 11:51:58 +11003169 if (cfq_cfqq_slice_new(cfqq)) {
3170 cfq_set_prio_slice(cfqd, cfqq);
3171 cfq_clear_cfqq_slice_new(cfqq);
3172 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003173
3174 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003175 * Should we wait for next request to come in before we expire
3176 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003177 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003178 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003179 unsigned long extend_sl = cfqd->cfq_slice_idle;
3180 if (!cfqd->cfq_slice_idle)
3181 extend_sl = cfqd->cfq_group_idle;
3182 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003183 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003184 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003185 }
3186
Jens Axboea36e71f2009-04-15 12:15:11 +02003187 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003188 * Idling is not enabled on:
3189 * - expired queues
3190 * - idle-priority queues
3191 * - async queues
3192 * - queues with still some requests queued
3193 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003194 */
Jens Axboe08717142008-01-28 11:38:15 +01003195 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003196 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003197 else if (sync && cfqq_empty &&
3198 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003199 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003200 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003201 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003202
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003203 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003204 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Jens Axboe89850f72006-07-22 16:48:31 +02003207static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003208{
Jens Axboe1b379d82009-08-11 08:26:11 +02003209 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003210 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003211 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003212 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003213
3214 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003215}
3216
Jens Axboe165125e2007-07-24 09:28:11 +02003217static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003218{
3219 struct cfq_data *cfqd = q->elevator->elevator_data;
3220 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003221 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003222 struct cfq_queue *cfqq;
3223
3224 /*
3225 * don't force setup of a queue from here, as a call to may_queue
3226 * does not necessarily imply that a request actually will be queued.
3227 * so just lookup a possibly existing queue, or return 'may queue'
3228 * if that fails
3229 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003230 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003231 if (!cic)
3232 return ELV_MQUEUE_MAY;
3233
Jens Axboeb0b78f82009-04-08 10:56:08 +02003234 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003235 if (cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003236 cfq_init_prio_data(cfqq, cic->icq.ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003237
Jens Axboe89850f72006-07-22 16:48:31 +02003238 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003239 }
3240
3241 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244/*
3245 * queue lock held here
3246 */
Jens Axboebb37b942006-12-01 10:42:33 +01003247static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Jens Axboe5e705372006-07-13 12:39:25 +02003249 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Jens Axboe5e705372006-07-13 12:39:25 +02003251 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003252 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Jens Axboe22e2c502005-06-27 10:55:12 +02003254 BUG_ON(!cfqq->allocated[rw]);
3255 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003257 /* Put down rq reference on cfqg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003258 blkg_put(cfqg_to_blkg(RQ_CFQG(rq)));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003259 rq->elv.priv[0] = NULL;
3260 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 cfq_put_queue(cfqq);
3263 }
3264}
3265
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003266static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003267cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003268 struct cfq_queue *cfqq)
3269{
3270 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3271 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003272 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003273 cfq_put_queue(cfqq);
3274 return cic_to_cfqq(cic, 1);
3275}
3276
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003277/*
3278 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3279 * was the last process referring to said cfqq.
3280 */
3281static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003282split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003283{
3284 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003285 cfqq->pid = current->pid;
3286 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003287 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003288 return cfqq;
3289 }
3290
3291 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003292
3293 cfq_put_cooperator(cfqq);
3294
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003295 cfq_put_queue(cfqq);
3296 return NULL;
3297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003299 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003301static int
Jens Axboe165125e2007-07-24 09:28:11 +02003302cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
3304 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003305 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003307 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003308 struct cfq_queue *cfqq;
Tejun Heod705ae62012-02-15 09:45:49 +01003309 unsigned int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 might_sleep_if(gfp_mask & __GFP_WAIT);
3312
Tejun Heo216284c2011-12-14 00:33:38 +01003313 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003314
3315 /* handle changed notifications */
Tejun Heod705ae62012-02-15 09:45:49 +01003316 changed = icq_get_changed(&cic->icq);
3317 if (unlikely(changed & ICQ_IOPRIO_CHANGED))
3318 changed_ioprio(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003319#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heod705ae62012-02-15 09:45:49 +01003320 if (unlikely(changed & ICQ_CGROUP_CHANGED))
3321 changed_cgroup(cic);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003324new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003325 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003326 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoc5869802011-12-14 00:33:41 +01003327 cfqq = cfq_get_queue(cfqd, is_sync, cic->icq.ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003328 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003329 } else {
3330 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003331 * If the queue was seeky for too long, break it apart.
3332 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003333 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003334 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3335 cfqq = split_cfqq(cic, cfqq);
3336 if (!cfqq)
3337 goto new_queue;
3338 }
3339
3340 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003341 * Check to see if this queue is scheduled to merge with
3342 * another, closely cooperating queue. The merging of
3343 * queues happens here as it must be done in process context.
3344 * The reference on new_cfqq was taken in merge_cfqqs.
3345 */
3346 if (cfqq->new_cfqq)
3347 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003351
Jens Axboe6fae9c22011-03-01 15:04:39 -05003352 cfqq->ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003353 blkg_get(cfqg_to_blkg(cfqq->cfqg));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003354 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003355 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01003356 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
David Howells65f27f32006-11-22 14:55:48 +00003360static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003361{
David Howells65f27f32006-11-22 14:55:48 +00003362 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003363 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003364 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003365
Jens Axboe40bb54d2009-04-15 12:11:10 +02003366 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003367 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003368 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003369}
3370
3371/*
3372 * Timer running if the active_queue is currently idling inside its time slice
3373 */
3374static void cfq_idle_slice_timer(unsigned long data)
3375{
3376 struct cfq_data *cfqd = (struct cfq_data *) data;
3377 struct cfq_queue *cfqq;
3378 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003379 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003380
Jens Axboe7b679132008-05-30 12:23:07 +02003381 cfq_log(cfqd, "idle timer fired");
3382
Jens Axboe22e2c502005-06-27 10:55:12 +02003383 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3384
Jens Axboefe094d92008-01-31 13:08:54 +01003385 cfqq = cfqd->active_queue;
3386 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003387 timed_out = 0;
3388
Jens Axboe22e2c502005-06-27 10:55:12 +02003389 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003390 * We saw a request before the queue expired, let it through
3391 */
3392 if (cfq_cfqq_must_dispatch(cfqq))
3393 goto out_kick;
3394
3395 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003396 * expired
3397 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003398 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003399 goto expire;
3400
3401 /*
3402 * only expire and reinvoke request handler, if there are
3403 * other queues with pending requests
3404 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003405 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003406 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003407
3408 /*
3409 * not expired and it has a request pending, let it dispatch
3410 */
Jens Axboe75e50982009-04-07 08:56:14 +02003411 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003412 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003413
3414 /*
3415 * Queue depth flag is reset only when the idle didn't succeed
3416 */
3417 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003418 }
3419expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003420 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003421out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003422 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003423out_cont:
3424 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3425}
3426
Jens Axboe3b181522005-06-27 10:56:24 +02003427static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3428{
3429 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003430 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003431}
Jens Axboe22e2c502005-06-27 10:55:12 +02003432
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003433static void cfq_put_async_queues(struct cfq_data *cfqd)
3434{
3435 int i;
3436
3437 for (i = 0; i < IOPRIO_BE_NR; i++) {
3438 if (cfqd->async_cfqq[0][i])
3439 cfq_put_queue(cfqd->async_cfqq[0][i]);
3440 if (cfqd->async_cfqq[1][i])
3441 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003442 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003443
3444 if (cfqd->async_idle_cfqq)
3445 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003446}
3447
Jens Axboeb374d182008-10-31 10:05:07 +01003448static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Jens Axboe22e2c502005-06-27 10:55:12 +02003450 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003451 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003452
Jens Axboe3b181522005-06-27 10:56:24 +02003453 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003454
Al Virod9ff4182006-03-18 13:51:22 -05003455 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003456
Al Virod9ff4182006-03-18 13:51:22 -05003457 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003458 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003459
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003460 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08003461
3462 spin_unlock_irq(q->queue_lock);
3463
Al Viroa90d7422006-03-18 12:05:37 -05003464 cfq_shutdown_timer_wq(cfqd);
3465
Tejun Heof51b8022012-03-05 13:15:05 -08003466#ifndef CONFIG_CFQ_GROUP_IOSCHED
3467 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003468#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -08003469 update_root_blkg_pd(q, BLKIO_POLICY_PROP);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003470 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Tejun Heob2fab5a2012-03-05 13:14:57 -08003473static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003476 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003477 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Christoph Lameter94f60302007-07-17 04:03:29 -07003479 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003480 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003481 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003482
Tejun Heof51b8022012-03-05 13:15:05 -08003483 cfqd->queue = q;
3484 q->elevator->elevator_data = cfqd;
3485
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003486 /* Init root service tree */
3487 cfqd->grp_service_tree = CFQ_RB_ROOT;
3488
Tejun Heof51b8022012-03-05 13:15:05 -08003489 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05003490#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08003491 rcu_read_lock();
3492 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003493
Tejun Heocd1604f2012-03-05 13:15:06 -08003494 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_PROP,
3495 true);
3496 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08003497 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08003498
3499 spin_unlock_irq(q->queue_lock);
3500 rcu_read_unlock();
3501#else
3502 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
3503 GFP_KERNEL, cfqd->queue->node);
3504 if (cfqd->root_group)
3505 cfq_init_cfqg_base(cfqd->root_group);
3506#endif
3507 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003508 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08003509 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003510 }
3511
Tejun Heof51b8022012-03-05 13:15:05 -08003512 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003513
Jens Axboe26a2ac02009-04-23 12:13:27 +02003514 /*
3515 * Not strictly needed (since RB_ROOT just clears the node and we
3516 * zeroed cfqd on alloc), but better be safe in case someone decides
3517 * to add magic to the rb code
3518 */
3519 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3520 cfqd->prio_trees[i] = RB_ROOT;
3521
Jens Axboe6118b702009-06-30 09:34:12 +02003522 /*
3523 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3524 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08003525 * will not attempt to free it. oom_cfqq is linked to root_group
3526 * but shouldn't hold a reference as it'll never be unlinked. Lose
3527 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02003528 */
3529 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01003530 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003531
3532 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08003533 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003534 blkg_put(cfqg_to_blkg(cfqd->root_group));
3535 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Jens Axboe22e2c502005-06-27 10:55:12 +02003537 init_timer(&cfqd->idle_slice_timer);
3538 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3539 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3540
Jens Axboe23e018a2009-10-05 08:52:35 +02003541 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003544 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3545 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 cfqd->cfq_back_max = cfq_back_max;
3547 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003548 cfqd->cfq_slice[0] = cfq_slice_async;
3549 cfqd->cfq_slice[1] = cfq_slice_sync;
3550 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3551 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003552 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003553 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003554 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01003555 /*
3556 * we optimistically start assuming sync ops weren't delayed in last
3557 * second, in order to have larger depth for async operations.
3558 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003559 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08003560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563/*
3564 * sysfs parts below -->
3565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566static ssize_t
3567cfq_var_show(unsigned int var, char *page)
3568{
3569 return sprintf(page, "%d\n", var);
3570}
3571
3572static ssize_t
3573cfq_var_store(unsigned int *var, const char *page, size_t count)
3574{
3575 char *p = (char *) page;
3576
3577 *var = simple_strtoul(p, &p, 10);
3578 return count;
3579}
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003582static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003584 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 unsigned int __data = __VAR; \
3586 if (__CONV) \
3587 __data = jiffies_to_msecs(__data); \
3588 return cfq_var_show(__data, (page)); \
3589}
3590SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003591SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3592SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003593SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3594SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003595SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003596SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003597SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3598SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3599SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003600SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#undef SHOW_FUNCTION
3602
3603#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003604static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003606 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 unsigned int __data; \
3608 int ret = cfq_var_store(&__data, (page), count); \
3609 if (__data < (MIN)) \
3610 __data = (MIN); \
3611 else if (__data > (MAX)) \
3612 __data = (MAX); \
3613 if (__CONV) \
3614 *(__PTR) = msecs_to_jiffies(__data); \
3615 else \
3616 *(__PTR) = __data; \
3617 return ret; \
3618}
3619STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003620STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3621 UINT_MAX, 1);
3622STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3623 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003624STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003625STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3626 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003627STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003628STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003629STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3630STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003631STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3632 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003633STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634#undef STORE_FUNCTION
3635
Al Viroe572ec72006-03-18 22:27:18 -05003636#define CFQ_ATTR(name) \
3637 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003638
Al Viroe572ec72006-03-18 22:27:18 -05003639static struct elv_fs_entry cfq_attrs[] = {
3640 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003641 CFQ_ATTR(fifo_expire_sync),
3642 CFQ_ATTR(fifo_expire_async),
3643 CFQ_ATTR(back_seek_max),
3644 CFQ_ATTR(back_seek_penalty),
3645 CFQ_ATTR(slice_sync),
3646 CFQ_ATTR(slice_async),
3647 CFQ_ATTR(slice_async_rq),
3648 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003649 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003650 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003651 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652};
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654static struct elevator_type iosched_cfq = {
3655 .ops = {
3656 .elevator_merge_fn = cfq_merge,
3657 .elevator_merged_fn = cfq_merged_request,
3658 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003659 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07003660 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02003661 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003663 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003666 .elevator_former_req_fn = elv_rb_former_request,
3667 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01003668 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01003669 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 .elevator_set_req_fn = cfq_set_request,
3671 .elevator_put_req_fn = cfq_put_request,
3672 .elevator_may_queue_fn = cfq_may_queue,
3673 .elevator_init_fn = cfq_init_queue,
3674 .elevator_exit_fn = cfq_exit_queue,
3675 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01003676 .icq_size = sizeof(struct cfq_io_cq),
3677 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05003678 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01003679 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 .elevator_owner = THIS_MODULE,
3681};
3682
Vivek Goyal3e252062009-12-04 10:36:42 -05003683#ifdef CONFIG_CFQ_GROUP_IOSCHED
3684static struct blkio_policy_type blkio_policy_cfq = {
3685 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08003686 .blkio_init_group_fn = cfq_init_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05003687 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
3688 },
Vivek Goyal062a6442010-09-15 17:06:33 -04003689 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08003690 .pdata_size = sizeof(struct cfq_group),
Vivek Goyal3e252062009-12-04 10:36:42 -05003691};
Vivek Goyal3e252062009-12-04 10:36:42 -05003692#endif
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694static int __init cfq_init(void)
3695{
Tejun Heo3d3c2372011-12-14 00:33:42 +01003696 int ret;
3697
Jens Axboe22e2c502005-06-27 10:55:12 +02003698 /*
3699 * could be 0 on HZ < 1000 setups
3700 */
3701 if (!cfq_slice_async)
3702 cfq_slice_async = 1;
3703 if (!cfq_slice_idle)
3704 cfq_slice_idle = 1;
3705
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003706#ifdef CONFIG_CFQ_GROUP_IOSCHED
3707 if (!cfq_group_idle)
3708 cfq_group_idle = 1;
3709#else
3710 cfq_group_idle = 0;
3711#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01003712 cfq_pool = KMEM_CACHE(cfq_queue, 0);
3713 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return -ENOMEM;
3715
Tejun Heo3d3c2372011-12-14 00:33:42 +01003716 ret = elv_register(&iosched_cfq);
3717 if (ret) {
3718 kmem_cache_destroy(cfq_pool);
3719 return ret;
3720 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01003721
Tejun Heob95ada52012-03-05 13:14:55 -08003722#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003723 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003724#endif
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
3728static void __exit cfq_exit(void)
3729{
Tejun Heob95ada52012-03-05 13:14:55 -08003730#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05003731 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08003732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01003734 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
3737module_init(cfq_init);
3738module_exit(cfq_exit);
3739
3740MODULE_AUTHOR("Jens Axboe");
3741MODULE_LICENSE("GPL");
3742MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");