cfq-iosched: implement cfq_group->nr_active and ->children_weight
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / block / cfq-iosched.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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 *
0fe23479 7 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
1da177e4 8 */
1da177e4 9#include <linux/module.h>
5a0e3ad6 10#include <linux/slab.h>
1cc9be68
AV
11#include <linux/blkdev.h>
12#include <linux/elevator.h>
ad5ebd2f 13#include <linux/jiffies.h>
1da177e4 14#include <linux/rbtree.h>
22e2c507 15#include <linux/ioprio.h>
7b679138 16#include <linux/blktrace_api.h>
6e736be7 17#include "blk.h"
629ed0b1 18#include "blk-cgroup.h"
1da177e4
LT
19
20/*
21 * tunables
22 */
fe094d98 23/* max queue in one round of service */
abc3c744 24static const int cfq_quantum = 8;
64100099 25static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
fe094d98
JA
26/* maximum backwards seek, in KiB */
27static const int cfq_back_max = 16 * 1024;
28/* penalty of a backwards seek */
29static const int cfq_back_penalty = 2;
64100099 30static const int cfq_slice_sync = HZ / 10;
3b18152c 31static int cfq_slice_async = HZ / 25;
64100099 32static const int cfq_slice_async_rq = 2;
caaa5f9f 33static int cfq_slice_idle = HZ / 125;
80bdf0c7 34static int cfq_group_idle = HZ / 125;
5db5d642
CZ
35static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
36static const int cfq_hist_divisor = 4;
22e2c507 37
d9e7620e 38/*
0871714e 39 * offset from end of service tree
d9e7620e 40 */
0871714e 41#define CFQ_IDLE_DELAY (HZ / 5)
d9e7620e
JA
42
43/*
44 * below this threshold, we consider thinktime immediate
45 */
46#define CFQ_MIN_TT (2)
47
22e2c507 48#define CFQ_SLICE_SCALE (5)
45333d5a 49#define CFQ_HW_QUEUE_MIN (5)
25bc6b07 50#define CFQ_SERVICE_SHIFT 12
22e2c507 51
3dde36dd 52#define CFQQ_SEEK_THR (sector_t)(8 * 100)
e9ce335d 53#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
41647e7a 54#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
3dde36dd 55#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
ae54abed 56
a612fddf
TH
57#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
58#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
59#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
1da177e4 60
e18b890b 61static struct kmem_cache *cfq_pool;
1da177e4 62
22e2c507
JA
63#define CFQ_PRIO_LISTS IOPRIO_BE_NR
64#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
22e2c507
JA
65#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
66
206dc69b 67#define sample_valid(samples) ((samples) > 80)
1fa8f6d6 68#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
206dc69b 69
c5869807
TH
70struct cfq_ttime {
71 unsigned long last_end_request;
72
73 unsigned long ttime_total;
74 unsigned long ttime_samples;
75 unsigned long ttime_mean;
76};
77
cc09e299
JA
78/*
79 * Most of our rbtree usage is for sorting with min extraction, so
80 * if we cache the leftmost node we don't have to walk down the tree
81 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
82 * move this into the elevator for the rq sorting as well.
83 */
84struct cfq_rb_root {
85 struct rb_root rb;
86 struct rb_node *left;
aa6f6a3d 87 unsigned count;
73e9ffdd 88 unsigned total_weight;
1fa8f6d6 89 u64 min_vdisktime;
f5f2b6ce 90 struct cfq_ttime ttime;
cc09e299 91};
f5f2b6ce
SL
92#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
93 .ttime = {.last_end_request = jiffies,},}
cc09e299 94
6118b70b
JA
95/*
96 * Per process-grouping structure
97 */
98struct cfq_queue {
99 /* reference count */
30d7b944 100 int ref;
6118b70b
JA
101 /* various state flags, see below */
102 unsigned int flags;
103 /* parent cfq_data */
104 struct cfq_data *cfqd;
105 /* service_tree member */
106 struct rb_node rb_node;
107 /* service_tree key */
108 unsigned long rb_key;
109 /* prio tree member */
110 struct rb_node p_node;
111 /* prio tree root we belong to, if any */
112 struct rb_root *p_root;
113 /* sorted list of pending requests */
114 struct rb_root sort_list;
115 /* if fifo isn't expired, next request to serve */
116 struct request *next_rq;
117 /* requests queued in sort_list */
118 int queued[2];
119 /* currently allocated requests */
120 int allocated[2];
121 /* fifo list of requests in sort_list */
122 struct list_head fifo;
123
dae739eb
VG
124 /* time when queue got scheduled in to dispatch first request. */
125 unsigned long dispatch_start;
f75edf2d 126 unsigned int allocated_slice;
c4081ba5 127 unsigned int slice_dispatch;
dae739eb
VG
128 /* time when first request from queue completed and slice started. */
129 unsigned long slice_start;
6118b70b
JA
130 unsigned long slice_end;
131 long slice_resid;
6118b70b 132
65299a3b
CH
133 /* pending priority requests */
134 int prio_pending;
6118b70b
JA
135 /* number of requests that are on the dispatch list or inside driver */
136 int dispatched;
137
138 /* io prio of this group */
139 unsigned short ioprio, org_ioprio;
4aede84b 140 unsigned short ioprio_class;
6118b70b 141
c4081ba5
RK
142 pid_t pid;
143
3dde36dd 144 u32 seek_history;
b2c18e1e
JM
145 sector_t last_request_pos;
146
aa6f6a3d 147 struct cfq_rb_root *service_tree;
df5fe3e8 148 struct cfq_queue *new_cfqq;
cdb16e8f 149 struct cfq_group *cfqg;
c4e7893e
VG
150 /* Number of sectors dispatched from queue in single dispatch round */
151 unsigned long nr_sectors;
6118b70b
JA
152};
153
c0324a02 154/*
718eee05 155 * First index in the service_trees.
c0324a02
CZ
156 * IDLE is handled separately, so it has negative index
157 */
3bf10fea 158enum wl_class_t {
c0324a02 159 BE_WORKLOAD = 0,
615f0259
VG
160 RT_WORKLOAD = 1,
161 IDLE_WORKLOAD = 2,
b4627321 162 CFQ_PRIO_NR,
c0324a02
CZ
163};
164
718eee05
CZ
165/*
166 * Second index in the service_trees.
167 */
168enum wl_type_t {
169 ASYNC_WORKLOAD = 0,
170 SYNC_NOIDLE_WORKLOAD = 1,
171 SYNC_WORKLOAD = 2
172};
173
155fead9
TH
174struct cfqg_stats {
175#ifdef CONFIG_CFQ_GROUP_IOSCHED
176 /* total bytes transferred */
177 struct blkg_rwstat service_bytes;
178 /* total IOs serviced, post merge */
179 struct blkg_rwstat serviced;
180 /* number of ios merged */
181 struct blkg_rwstat merged;
182 /* total time spent on device in ns, may not be accurate w/ queueing */
183 struct blkg_rwstat service_time;
184 /* total time spent waiting in scheduler queue in ns */
185 struct blkg_rwstat wait_time;
186 /* number of IOs queued up */
187 struct blkg_rwstat queued;
188 /* total sectors transferred */
189 struct blkg_stat sectors;
190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time;
192#ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time;
3c798398 203 /* time spent idling for this blkcg_gq */
155fead9
TH
204 struct blkg_stat idle_time;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time;
209 uint64_t start_idle_time;
210 uint64_t start_empty_time;
211 uint16_t flags;
212#endif /* CONFIG_DEBUG_BLK_CGROUP */
213#endif /* CONFIG_CFQ_GROUP_IOSCHED */
214};
215
cdb16e8f
VG
216/* This is per cgroup per device grouping structure */
217struct cfq_group {
f95a04af
TH
218 /* must be the first member */
219 struct blkg_policy_data pd;
220
1fa8f6d6
VG
221 /* group service_tree member */
222 struct rb_node rb_node;
223
224 /* group service_tree key */
225 u64 vdisktime;
e71357e1 226
7918ffb5
TH
227 /*
228 * The number of active cfqgs and sum of their weights under this
229 * cfqg. This covers this cfqg's leaf_weight and all children's
230 * weights, but does not cover weights of further descendants.
231 *
232 * If a cfqg is on the service tree, it's active. An active cfqg
233 * also activates its parent and contributes to the children_weight
234 * of the parent.
235 */
236 int nr_active;
237 unsigned int children_weight;
238
e71357e1
TH
239 /*
240 * There are two weights - (internal) weight is the weight of this
241 * cfqg against the sibling cfqgs. leaf_weight is the wight of
242 * this cfqg against the child cfqgs. For the root cfqg, both
243 * weights are kept in sync for backward compatibility.
244 */
25bc6b07 245 unsigned int weight;
8184f93e 246 unsigned int new_weight;
3381cb8d 247 unsigned int dev_weight;
1fa8f6d6 248
e71357e1
TH
249 unsigned int leaf_weight;
250 unsigned int new_leaf_weight;
251 unsigned int dev_leaf_weight;
252
1fa8f6d6
VG
253 /* number of cfqq currently on this group */
254 int nr_cfqq;
255
cdb16e8f 256 /*
4495a7d4 257 * Per group busy queues average. Useful for workload slice calc. We
b4627321
VG
258 * create the array for each prio class but at run time it is used
259 * only for RT and BE class and slot for IDLE class remains unused.
260 * This is primarily done to avoid confusion and a gcc warning.
261 */
262 unsigned int busy_queues_avg[CFQ_PRIO_NR];
263 /*
264 * rr lists of queues with requests. We maintain service trees for
265 * RT and BE classes. These trees are subdivided in subclasses
266 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
267 * class there is no subclassification and all the cfq queues go on
268 * a single tree service_tree_idle.
cdb16e8f
VG
269 * Counts are embedded in the cfq_rb_root
270 */
271 struct cfq_rb_root service_trees[2][3];
272 struct cfq_rb_root service_tree_idle;
dae739eb 273
4d2ceea4
VG
274 unsigned long saved_wl_slice;
275 enum wl_type_t saved_wl_type;
276 enum wl_class_t saved_wl_class;
4eef3049 277
80bdf0c7
VG
278 /* number of requests that are on the dispatch list or inside driver */
279 int dispatched;
7700fc4f 280 struct cfq_ttime ttime;
155fead9 281 struct cfqg_stats stats;
cdb16e8f 282};
718eee05 283
c5869807
TH
284struct cfq_io_cq {
285 struct io_cq icq; /* must be the first member */
286 struct cfq_queue *cfqq[2];
287 struct cfq_ttime ttime;
598971bf
TH
288 int ioprio; /* the current ioprio */
289#ifdef CONFIG_CFQ_GROUP_IOSCHED
290 uint64_t blkcg_id; /* the current blkcg ID */
291#endif
c5869807
TH
292};
293
22e2c507
JA
294/*
295 * Per block device queue structure
296 */
1da177e4 297struct cfq_data {
165125e1 298 struct request_queue *queue;
1fa8f6d6
VG
299 /* Root service tree for cfq_groups */
300 struct cfq_rb_root grp_service_tree;
f51b802c 301 struct cfq_group *root_group;
22e2c507 302
c0324a02
CZ
303 /*
304 * The priority currently being served
22e2c507 305 */
4d2ceea4
VG
306 enum wl_class_t serving_wl_class;
307 enum wl_type_t serving_wl_type;
718eee05 308 unsigned long workload_expires;
cdb16e8f 309 struct cfq_group *serving_group;
a36e71f9
JA
310
311 /*
312 * Each priority tree is sorted by next_request position. These
313 * trees are used when determining if two or more queues are
314 * interleaving requests (see cfq_close_cooperator).
315 */
316 struct rb_root prio_trees[CFQ_PRIO_LISTS];
317
22e2c507 318 unsigned int busy_queues;
ef8a41df 319 unsigned int busy_sync_queues;
22e2c507 320
53c583d2
CZ
321 int rq_in_driver;
322 int rq_in_flight[2];
45333d5a
AC
323
324 /*
325 * queue-depth detection
326 */
327 int rq_queued;
25776e35 328 int hw_tag;
e459dd08
CZ
329 /*
330 * hw_tag can be
331 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
332 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
333 * 0 => no NCQ
334 */
335 int hw_tag_est_depth;
336 unsigned int hw_tag_samples;
1da177e4 337
22e2c507
JA
338 /*
339 * idle window management
340 */
341 struct timer_list idle_slice_timer;
23e018a1 342 struct work_struct unplug_work;
1da177e4 343
22e2c507 344 struct cfq_queue *active_queue;
c5869807 345 struct cfq_io_cq *active_cic;
22e2c507 346
c2dea2d1
VT
347 /*
348 * async queue for each priority case
349 */
350 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
351 struct cfq_queue *async_idle_cfqq;
15c31be4 352
6d048f53 353 sector_t last_position;
1da177e4 354
1da177e4
LT
355 /*
356 * tunables, see top of file
357 */
358 unsigned int cfq_quantum;
22e2c507 359 unsigned int cfq_fifo_expire[2];
1da177e4
LT
360 unsigned int cfq_back_penalty;
361 unsigned int cfq_back_max;
22e2c507
JA
362 unsigned int cfq_slice[2];
363 unsigned int cfq_slice_async_rq;
364 unsigned int cfq_slice_idle;
80bdf0c7 365 unsigned int cfq_group_idle;
963b72fc 366 unsigned int cfq_latency;
5bf14c07 367 unsigned int cfq_target_latency;
d9ff4187 368
6118b70b
JA
369 /*
370 * Fallback dummy cfqq for extreme OOM conditions
371 */
372 struct cfq_queue oom_cfqq;
365722bb 373
573412b2 374 unsigned long last_delayed_sync;
1da177e4
LT
375};
376
25fb5169
VG
377static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
378
34b98d03 379static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
3bf10fea 380 enum wl_class_t class,
65b32a57 381 enum wl_type_t type)
c0324a02 382{
1fa8f6d6
VG
383 if (!cfqg)
384 return NULL;
385
3bf10fea 386 if (class == IDLE_WORKLOAD)
cdb16e8f 387 return &cfqg->service_tree_idle;
c0324a02 388
3bf10fea 389 return &cfqg->service_trees[class][type];
c0324a02
CZ
390}
391
3b18152c 392enum cfqq_state_flags {
b0b8d749
JA
393 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
394 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
b029195d 395 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
b0b8d749 396 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
b0b8d749
JA
397 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
398 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
399 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
44f7c160 400 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
91fac317 401 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
b3b6d040 402 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
ae54abed 403 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
76280aff 404 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
f75edf2d 405 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
3b18152c
JA
406};
407
408#define CFQ_CFQQ_FNS(name) \
409static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
410{ \
fe094d98 411 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
3b18152c
JA
412} \
413static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
414{ \
fe094d98 415 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
3b18152c
JA
416} \
417static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
418{ \
fe094d98 419 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
3b18152c
JA
420}
421
422CFQ_CFQQ_FNS(on_rr);
423CFQ_CFQQ_FNS(wait_request);
b029195d 424CFQ_CFQQ_FNS(must_dispatch);
3b18152c 425CFQ_CFQQ_FNS(must_alloc_slice);
3b18152c
JA
426CFQ_CFQQ_FNS(fifo_expire);
427CFQ_CFQQ_FNS(idle_window);
428CFQ_CFQQ_FNS(prio_changed);
44f7c160 429CFQ_CFQQ_FNS(slice_new);
91fac317 430CFQ_CFQQ_FNS(sync);
a36e71f9 431CFQ_CFQQ_FNS(coop);
ae54abed 432CFQ_CFQQ_FNS(split_coop);
76280aff 433CFQ_CFQQ_FNS(deep);
f75edf2d 434CFQ_CFQQ_FNS(wait_busy);
3b18152c
JA
435#undef CFQ_CFQQ_FNS
436
f95a04af
TH
437static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
438{
439 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
440}
441
f95a04af
TH
442static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
443{
444 return pd_to_blkg(&cfqg->pd);
445}
446
629ed0b1 447#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
2ce4d50f 448
155fead9
TH
449/* cfqg stats flags */
450enum cfqg_stats_flags {
451 CFQG_stats_waiting = 0,
452 CFQG_stats_idling,
453 CFQG_stats_empty,
629ed0b1
TH
454};
455
155fead9
TH
456#define CFQG_FLAG_FNS(name) \
457static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
629ed0b1 458{ \
155fead9 459 stats->flags |= (1 << CFQG_stats_##name); \
629ed0b1 460} \
155fead9 461static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
629ed0b1 462{ \
155fead9 463 stats->flags &= ~(1 << CFQG_stats_##name); \
629ed0b1 464} \
155fead9 465static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
629ed0b1 466{ \
155fead9 467 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
629ed0b1
TH
468} \
469
155fead9
TH
470CFQG_FLAG_FNS(waiting)
471CFQG_FLAG_FNS(idling)
472CFQG_FLAG_FNS(empty)
473#undef CFQG_FLAG_FNS
629ed0b1
TH
474
475/* This should be called with the queue_lock held. */
155fead9 476static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
629ed0b1
TH
477{
478 unsigned long long now;
479
155fead9 480 if (!cfqg_stats_waiting(stats))
629ed0b1
TH
481 return;
482
483 now = sched_clock();
484 if (time_after64(now, stats->start_group_wait_time))
485 blkg_stat_add(&stats->group_wait_time,
486 now - stats->start_group_wait_time);
155fead9 487 cfqg_stats_clear_waiting(stats);
629ed0b1
TH
488}
489
490/* This should be called with the queue_lock held. */
155fead9
TH
491static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
492 struct cfq_group *curr_cfqg)
629ed0b1 493{
155fead9 494 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1 495
155fead9 496 if (cfqg_stats_waiting(stats))
629ed0b1 497 return;
155fead9 498 if (cfqg == curr_cfqg)
629ed0b1 499 return;
155fead9
TH
500 stats->start_group_wait_time = sched_clock();
501 cfqg_stats_mark_waiting(stats);
629ed0b1
TH
502}
503
504/* This should be called with the queue_lock held. */
155fead9 505static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
629ed0b1
TH
506{
507 unsigned long long now;
508
155fead9 509 if (!cfqg_stats_empty(stats))
629ed0b1
TH
510 return;
511
512 now = sched_clock();
513 if (time_after64(now, stats->start_empty_time))
514 blkg_stat_add(&stats->empty_time,
515 now - stats->start_empty_time);
155fead9 516 cfqg_stats_clear_empty(stats);
629ed0b1
TH
517}
518
155fead9 519static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
629ed0b1 520{
155fead9 521 blkg_stat_add(&cfqg->stats.dequeue, 1);
629ed0b1
TH
522}
523
155fead9 524static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
629ed0b1 525{
155fead9 526 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1
TH
527
528 if (blkg_rwstat_sum(&stats->queued))
529 return;
530
531 /*
532 * group is already marked empty. This can happen if cfqq got new
533 * request in parent group and moved to this group while being added
534 * to service tree. Just ignore the event and move on.
535 */
155fead9 536 if (cfqg_stats_empty(stats))
629ed0b1
TH
537 return;
538
539 stats->start_empty_time = sched_clock();
155fead9 540 cfqg_stats_mark_empty(stats);
629ed0b1
TH
541}
542
155fead9 543static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
629ed0b1 544{
155fead9 545 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1 546
155fead9 547 if (cfqg_stats_idling(stats)) {
629ed0b1
TH
548 unsigned long long now = sched_clock();
549
550 if (time_after64(now, stats->start_idle_time))
551 blkg_stat_add(&stats->idle_time,
552 now - stats->start_idle_time);
155fead9 553 cfqg_stats_clear_idling(stats);
629ed0b1
TH
554 }
555}
556
155fead9 557static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
629ed0b1 558{
155fead9 559 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1 560
155fead9 561 BUG_ON(cfqg_stats_idling(stats));
629ed0b1
TH
562
563 stats->start_idle_time = sched_clock();
155fead9 564 cfqg_stats_mark_idling(stats);
629ed0b1
TH
565}
566
155fead9 567static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
629ed0b1 568{
155fead9 569 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1
TH
570
571 blkg_stat_add(&stats->avg_queue_size_sum,
572 blkg_rwstat_sum(&stats->queued));
573 blkg_stat_add(&stats->avg_queue_size_samples, 1);
155fead9 574 cfqg_stats_update_group_wait_time(stats);
629ed0b1
TH
575}
576
577#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
578
f48ec1d7
TH
579static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
580static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
581static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
582static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
583static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
584static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
585static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
629ed0b1
TH
586
587#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
588
589#ifdef CONFIG_CFQ_GROUP_IOSCHED
2ce4d50f 590
ffea73fc
TH
591static struct blkcg_policy blkcg_policy_cfq;
592
593static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
594{
595 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
596}
597
7918ffb5
TH
598/*
599 * Determine the parent cfqg for weight calculation. Currently, cfqg
600 * scheduling is flat and the root is the parent of everyone else.
601 */
602static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg)
603{
604 struct blkcg_gq *blkg = cfqg_to_blkg(cfqg);
605 struct cfq_group *root;
606
607 while (blkg->parent)
608 blkg = blkg->parent;
609 root = blkg_to_cfqg(blkg);
610
611 return root != cfqg ? root : NULL;
612}
613
eb7d8c07
TH
614static inline void cfqg_get(struct cfq_group *cfqg)
615{
616 return blkg_get(cfqg_to_blkg(cfqg));
617}
618
619static inline void cfqg_put(struct cfq_group *cfqg)
620{
621 return blkg_put(cfqg_to_blkg(cfqg));
622}
623
54e7ed12
TH
624#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
625 char __pbuf[128]; \
626 \
627 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
b226e5c4
VG
628 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
629 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
630 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
54e7ed12
TH
631 __pbuf, ##args); \
632} while (0)
633
634#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
635 char __pbuf[128]; \
636 \
637 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
638 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
639} while (0)
2868ef7b 640
155fead9
TH
641static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
642 struct cfq_group *curr_cfqg, int rw)
2ce4d50f 643{
155fead9
TH
644 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
645 cfqg_stats_end_empty_time(&cfqg->stats);
646 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
2ce4d50f
TH
647}
648
155fead9
TH
649static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
650 unsigned long time, unsigned long unaccounted_time)
2ce4d50f 651{
155fead9 652 blkg_stat_add(&cfqg->stats.time, time);
629ed0b1 653#ifdef CONFIG_DEBUG_BLK_CGROUP
155fead9 654 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
629ed0b1 655#endif
2ce4d50f
TH
656}
657
155fead9 658static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
2ce4d50f 659{
155fead9 660 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
2ce4d50f
TH
661}
662
155fead9 663static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
2ce4d50f 664{
155fead9 665 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
2ce4d50f
TH
666}
667
155fead9
TH
668static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
669 uint64_t bytes, int rw)
2ce4d50f 670{
155fead9
TH
671 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
672 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
673 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
2ce4d50f
TH
674}
675
155fead9
TH
676static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
677 uint64_t start_time, uint64_t io_start_time, int rw)
2ce4d50f 678{
155fead9 679 struct cfqg_stats *stats = &cfqg->stats;
629ed0b1 680 unsigned long long now = sched_clock();
629ed0b1
TH
681
682 if (time_after64(now, io_start_time))
683 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
684 if (time_after64(io_start_time, start_time))
685 blkg_rwstat_add(&stats->wait_time, rw,
686 io_start_time - start_time);
2ce4d50f
TH
687}
688
3c798398 689static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
155fead9
TH
690{
691 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
692 struct cfqg_stats *stats = &cfqg->stats;
693
694 /* queued stats shouldn't be cleared */
695 blkg_rwstat_reset(&stats->service_bytes);
696 blkg_rwstat_reset(&stats->serviced);
697 blkg_rwstat_reset(&stats->merged);
698 blkg_rwstat_reset(&stats->service_time);
699 blkg_rwstat_reset(&stats->wait_time);
700 blkg_stat_reset(&stats->time);
701#ifdef CONFIG_DEBUG_BLK_CGROUP
702 blkg_stat_reset(&stats->unaccounted_time);
703 blkg_stat_reset(&stats->avg_queue_size_sum);
704 blkg_stat_reset(&stats->avg_queue_size_samples);
705 blkg_stat_reset(&stats->dequeue);
706 blkg_stat_reset(&stats->group_wait_time);
707 blkg_stat_reset(&stats->idle_time);
708 blkg_stat_reset(&stats->empty_time);
709#endif
710}
711
eb7d8c07
TH
712#else /* CONFIG_CFQ_GROUP_IOSCHED */
713
7918ffb5 714static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg) { return NULL; }
eb7d8c07
TH
715static inline void cfqg_get(struct cfq_group *cfqg) { }
716static inline void cfqg_put(struct cfq_group *cfqg) { }
717
7b679138 718#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
b226e5c4
VG
719 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
720 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
721 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
722 ##args)
4495a7d4 723#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
eb7d8c07 724
155fead9
TH
725static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
726 struct cfq_group *curr_cfqg, int rw) { }
727static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
728 unsigned long time, unsigned long unaccounted_time) { }
729static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
730static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
731static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
732 uint64_t bytes, int rw) { }
733static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
734 uint64_t start_time, uint64_t io_start_time, int rw) { }
2ce4d50f 735
eb7d8c07
TH
736#endif /* CONFIG_CFQ_GROUP_IOSCHED */
737
7b679138
JA
738#define cfq_log(cfqd, fmt, args...) \
739 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
740
615f0259
VG
741/* Traverses through cfq group service trees */
742#define for_each_cfqg_st(cfqg, i, j, st) \
743 for (i = 0; i <= IDLE_WORKLOAD; i++) \
744 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
745 : &cfqg->service_tree_idle; \
746 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
747 (i == IDLE_WORKLOAD && j == 0); \
748 j++, st = i < IDLE_WORKLOAD ? \
749 &cfqg->service_trees[i][j]: NULL) \
750
f5f2b6ce
SL
751static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
752 struct cfq_ttime *ttime, bool group_idle)
753{
754 unsigned long slice;
755 if (!sample_valid(ttime->ttime_samples))
756 return false;
757 if (group_idle)
758 slice = cfqd->cfq_group_idle;
759 else
760 slice = cfqd->cfq_slice_idle;
761 return ttime->ttime_mean > slice;
762}
615f0259 763
02b35081
VG
764static inline bool iops_mode(struct cfq_data *cfqd)
765{
766 /*
767 * If we are not idling on queues and it is a NCQ drive, parallel
768 * execution of requests is on and measuring time is not possible
769 * in most of the cases until and unless we drive shallower queue
770 * depths and that becomes a performance bottleneck. In such cases
771 * switch to start providing fairness in terms of number of IOs.
772 */
773 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
774 return true;
775 else
776 return false;
777}
778
3bf10fea 779static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
c0324a02
CZ
780{
781 if (cfq_class_idle(cfqq))
782 return IDLE_WORKLOAD;
783 if (cfq_class_rt(cfqq))
784 return RT_WORKLOAD;
785 return BE_WORKLOAD;
786}
787
718eee05
CZ
788
789static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
790{
791 if (!cfq_cfqq_sync(cfqq))
792 return ASYNC_WORKLOAD;
793 if (!cfq_cfqq_idle_window(cfqq))
794 return SYNC_NOIDLE_WORKLOAD;
795 return SYNC_WORKLOAD;
796}
797
3bf10fea 798static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
58ff82f3
VG
799 struct cfq_data *cfqd,
800 struct cfq_group *cfqg)
c0324a02 801{
3bf10fea 802 if (wl_class == IDLE_WORKLOAD)
cdb16e8f 803 return cfqg->service_tree_idle.count;
c0324a02 804
34b98d03
VG
805 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
806 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
807 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
c0324a02
CZ
808}
809
f26bd1f0
VG
810static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
811 struct cfq_group *cfqg)
812{
34b98d03
VG
813 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
814 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
f26bd1f0
VG
815}
816
165125e1 817static void cfq_dispatch_insert(struct request_queue *, struct request *);
4f85cb96 818static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
abede6da 819 struct cfq_io_cq *cic, struct bio *bio,
4f85cb96 820 gfp_t gfp_mask);
91fac317 821
c5869807
TH
822static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
823{
824 /* cic->icq is the first member, %NULL will convert to %NULL */
825 return container_of(icq, struct cfq_io_cq, icq);
826}
827
47fdd4ca
TH
828static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
829 struct io_context *ioc)
830{
831 if (ioc)
832 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
833 return NULL;
834}
835
c5869807 836static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
91fac317 837{
a6151c3a 838 return cic->cfqq[is_sync];
91fac317
VT
839}
840
c5869807
TH
841static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
842 bool is_sync)
91fac317 843{
a6151c3a 844 cic->cfqq[is_sync] = cfqq;
91fac317
VT
845}
846
c5869807 847static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
bca4b914 848{
c5869807 849 return cic->icq.q->elevator->elevator_data;
bca4b914
KK
850}
851
91fac317
VT
852/*
853 * We regard a request as SYNC, if it's either a read or has the SYNC bit
854 * set (in which case it could also be direct WRITE).
855 */
a6151c3a 856static inline bool cfq_bio_sync(struct bio *bio)
91fac317 857{
7b6d91da 858 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
91fac317 859}
1da177e4 860
99f95e52
AM
861/*
862 * scheduler run of queue, if there are requests pending and no one in the
863 * driver that will restart queueing
864 */
23e018a1 865static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
99f95e52 866{
7b679138
JA
867 if (cfqd->busy_queues) {
868 cfq_log(cfqd, "schedule dispatch");
23e018a1 869 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
7b679138 870 }
99f95e52
AM
871}
872
44f7c160
JA
873/*
874 * Scale schedule slice based on io priority. Use the sync time slice only
875 * if a queue is marked sync and has sync io queued. A sync queue with async
876 * io only, should not get full sync slice length.
877 */
a6151c3a 878static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
d9e7620e 879 unsigned short prio)
44f7c160 880{
d9e7620e 881 const int base_slice = cfqd->cfq_slice[sync];
44f7c160 882
d9e7620e
JA
883 WARN_ON(prio >= IOPRIO_BE_NR);
884
885 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
886}
44f7c160 887
d9e7620e
JA
888static inline int
889cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
890{
891 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
44f7c160
JA
892}
893
25bc6b07
VG
894static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
895{
896 u64 d = delta << CFQ_SERVICE_SHIFT;
897
3381cb8d 898 d = d * CFQ_WEIGHT_DEFAULT;
25bc6b07
VG
899 do_div(d, cfqg->weight);
900 return d;
901}
902
903static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
904{
905 s64 delta = (s64)(vdisktime - min_vdisktime);
906 if (delta > 0)
907 min_vdisktime = vdisktime;
908
909 return min_vdisktime;
910}
911
912static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
913{
914 s64 delta = (s64)(vdisktime - min_vdisktime);
915 if (delta < 0)
916 min_vdisktime = vdisktime;
917
918 return min_vdisktime;
919}
920
921static void update_min_vdisktime(struct cfq_rb_root *st)
922{
25bc6b07
VG
923 struct cfq_group *cfqg;
924
25bc6b07
VG
925 if (st->left) {
926 cfqg = rb_entry_cfqg(st->left);
a6032710
GJ
927 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
928 cfqg->vdisktime);
25bc6b07 929 }
25bc6b07
VG
930}
931
5db5d642
CZ
932/*
933 * get averaged number of queues of RT/BE priority.
934 * average is updated, with a formula that gives more weight to higher numbers,
935 * to quickly follows sudden increases and decrease slowly
936 */
937
58ff82f3
VG
938static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
939 struct cfq_group *cfqg, bool rt)
5869619c 940{
5db5d642
CZ
941 unsigned min_q, max_q;
942 unsigned mult = cfq_hist_divisor - 1;
943 unsigned round = cfq_hist_divisor / 2;
58ff82f3 944 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
5db5d642 945
58ff82f3
VG
946 min_q = min(cfqg->busy_queues_avg[rt], busy);
947 max_q = max(cfqg->busy_queues_avg[rt], busy);
948 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
5db5d642 949 cfq_hist_divisor;
58ff82f3
VG
950 return cfqg->busy_queues_avg[rt];
951}
952
953static inline unsigned
954cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
955{
956 struct cfq_rb_root *st = &cfqd->grp_service_tree;
957
5bf14c07 958 return cfqd->cfq_target_latency * cfqg->weight / st->total_weight;
5db5d642
CZ
959}
960
c553f8e3 961static inline unsigned
ba5bd520 962cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
44f7c160 963{
5db5d642
CZ
964 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
965 if (cfqd->cfq_latency) {
58ff82f3
VG
966 /*
967 * interested queues (we consider only the ones with the same
968 * priority class in the cfq group)
969 */
970 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
971 cfq_class_rt(cfqq));
5db5d642
CZ
972 unsigned sync_slice = cfqd->cfq_slice[1];
973 unsigned expect_latency = sync_slice * iq;
58ff82f3
VG
974 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
975
976 if (expect_latency > group_slice) {
5db5d642
CZ
977 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
978 /* scale low_slice according to IO priority
979 * and sync vs async */
980 unsigned low_slice =
981 min(slice, base_low_slice * slice / sync_slice);
982 /* the adapted slice value is scaled to fit all iqs
983 * into the target latency */
58ff82f3 984 slice = max(slice * group_slice / expect_latency,
5db5d642
CZ
985 low_slice);
986 }
987 }
c553f8e3
SL
988 return slice;
989}
990
991static inline void
992cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
993{
ba5bd520 994 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e3 995
dae739eb 996 cfqq->slice_start = jiffies;
5db5d642 997 cfqq->slice_end = jiffies + slice;
f75edf2d 998 cfqq->allocated_slice = slice;
7b679138 999 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
44f7c160
JA
1000}
1001
1002/*
1003 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1004 * isn't valid until the first request from the dispatch is activated
1005 * and the slice time set.
1006 */
a6151c3a 1007static inline bool cfq_slice_used(struct cfq_queue *cfqq)
44f7c160
JA
1008{
1009 if (cfq_cfqq_slice_new(cfqq))
c1e44756 1010 return false;
44f7c160 1011 if (time_before(jiffies, cfqq->slice_end))
c1e44756 1012 return false;
44f7c160 1013
c1e44756 1014 return true;
44f7c160
JA
1015}
1016
1da177e4 1017/*
5e705374 1018 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
1da177e4 1019 * We choose the request that is closest to the head right now. Distance
e8a99053 1020 * behind the head is penalized and only allowed to a certain extent.
1da177e4 1021 */
5e705374 1022static struct request *
cf7c25cf 1023cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
1da177e4 1024{
cf7c25cf 1025 sector_t s1, s2, d1 = 0, d2 = 0;
1da177e4 1026 unsigned long back_max;
e8a99053
AM
1027#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1028#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1029 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
1da177e4 1030
5e705374
JA
1031 if (rq1 == NULL || rq1 == rq2)
1032 return rq2;
1033 if (rq2 == NULL)
1034 return rq1;
9c2c38a1 1035
229836bd
NK
1036 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1037 return rq_is_sync(rq1) ? rq1 : rq2;
1038
65299a3b
CH
1039 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1040 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
b53d1ed7 1041
83096ebf
TH
1042 s1 = blk_rq_pos(rq1);
1043 s2 = blk_rq_pos(rq2);
1da177e4 1044
1da177e4
LT
1045 /*
1046 * by definition, 1KiB is 2 sectors
1047 */
1048 back_max = cfqd->cfq_back_max * 2;
1049
1050 /*
1051 * Strict one way elevator _except_ in the case where we allow
1052 * short backward seeks which are biased as twice the cost of a
1053 * similar forward seek.
1054 */
1055 if (s1 >= last)
1056 d1 = s1 - last;
1057 else if (s1 + back_max >= last)
1058 d1 = (last - s1) * cfqd->cfq_back_penalty;
1059 else
e8a99053 1060 wrap |= CFQ_RQ1_WRAP;
1da177e4
LT
1061
1062 if (s2 >= last)
1063 d2 = s2 - last;
1064 else if (s2 + back_max >= last)
1065 d2 = (last - s2) * cfqd->cfq_back_penalty;
1066 else
e8a99053 1067 wrap |= CFQ_RQ2_WRAP;
1da177e4
LT
1068
1069 /* Found required data */
e8a99053
AM
1070
1071 /*
1072 * By doing switch() on the bit mask "wrap" we avoid having to
1073 * check two variables for all permutations: --> faster!
1074 */
1075 switch (wrap) {
5e705374 1076 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
e8a99053 1077 if (d1 < d2)
5e705374 1078 return rq1;
e8a99053 1079 else if (d2 < d1)
5e705374 1080 return rq2;
e8a99053
AM
1081 else {
1082 if (s1 >= s2)
5e705374 1083 return rq1;
e8a99053 1084 else
5e705374 1085 return rq2;
e8a99053 1086 }
1da177e4 1087
e8a99053 1088 case CFQ_RQ2_WRAP:
5e705374 1089 return rq1;
e8a99053 1090 case CFQ_RQ1_WRAP:
5e705374
JA
1091 return rq2;
1092 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
e8a99053
AM
1093 default:
1094 /*
1095 * Since both rqs are wrapped,
1096 * start with the one that's further behind head
1097 * (--> only *one* back seek required),
1098 * since back seek takes more time than forward.
1099 */
1100 if (s1 <= s2)
5e705374 1101 return rq1;
1da177e4 1102 else
5e705374 1103 return rq2;
1da177e4
LT
1104 }
1105}
1106
498d3aa2
JA
1107/*
1108 * The below is leftmost cache rbtree addon
1109 */
0871714e 1110static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
cc09e299 1111{
615f0259
VG
1112 /* Service tree is empty */
1113 if (!root->count)
1114 return NULL;
1115
cc09e299
JA
1116 if (!root->left)
1117 root->left = rb_first(&root->rb);
1118
0871714e
JA
1119 if (root->left)
1120 return rb_entry(root->left, struct cfq_queue, rb_node);
1121
1122 return NULL;
cc09e299
JA
1123}
1124
1fa8f6d6
VG
1125static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1126{
1127 if (!root->left)
1128 root->left = rb_first(&root->rb);
1129
1130 if (root->left)
1131 return rb_entry_cfqg(root->left);
1132
1133 return NULL;
1134}
1135
a36e71f9
JA
1136static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1137{
1138 rb_erase(n, root);
1139 RB_CLEAR_NODE(n);
1140}
1141
cc09e299
JA
1142static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1143{
1144 if (root->left == n)
1145 root->left = NULL;
a36e71f9 1146 rb_erase_init(n, &root->rb);
aa6f6a3d 1147 --root->count;
cc09e299
JA
1148}
1149
1da177e4
LT
1150/*
1151 * would be nice to take fifo expire time into account as well
1152 */
5e705374
JA
1153static struct request *
1154cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1155 struct request *last)
1da177e4 1156{
21183b07
JA
1157 struct rb_node *rbnext = rb_next(&last->rb_node);
1158 struct rb_node *rbprev = rb_prev(&last->rb_node);
5e705374 1159 struct request *next = NULL, *prev = NULL;
1da177e4 1160
21183b07 1161 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
1da177e4
LT
1162
1163 if (rbprev)
5e705374 1164 prev = rb_entry_rq(rbprev);
1da177e4 1165
21183b07 1166 if (rbnext)
5e705374 1167 next = rb_entry_rq(rbnext);
21183b07
JA
1168 else {
1169 rbnext = rb_first(&cfqq->sort_list);
1170 if (rbnext && rbnext != &last->rb_node)
5e705374 1171 next = rb_entry_rq(rbnext);
21183b07 1172 }
1da177e4 1173
cf7c25cf 1174 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
1da177e4
LT
1175}
1176
d9e7620e
JA
1177static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1178 struct cfq_queue *cfqq)
1da177e4 1179{
d9e7620e
JA
1180 /*
1181 * just an approximation, should be ok.
1182 */
cdb16e8f 1183 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
464191c6 1184 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
d9e7620e
JA
1185}
1186
1fa8f6d6
VG
1187static inline s64
1188cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1189{
1190 return cfqg->vdisktime - st->min_vdisktime;
1191}
1192
1193static void
1194__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1195{
1196 struct rb_node **node = &st->rb.rb_node;
1197 struct rb_node *parent = NULL;
1198 struct cfq_group *__cfqg;
1199 s64 key = cfqg_key(st, cfqg);
1200 int left = 1;
1201
1202 while (*node != NULL) {
1203 parent = *node;
1204 __cfqg = rb_entry_cfqg(parent);
1205
1206 if (key < cfqg_key(st, __cfqg))
1207 node = &parent->rb_left;
1208 else {
1209 node = &parent->rb_right;
1210 left = 0;
1211 }
1212 }
1213
1214 if (left)
1215 st->left = &cfqg->rb_node;
1216
1217 rb_link_node(&cfqg->rb_node, parent, node);
1218 rb_insert_color(&cfqg->rb_node, &st->rb);
1219}
1220
1221static void
8184f93e
JT
1222cfq_update_group_weight(struct cfq_group *cfqg)
1223{
1224 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
e71357e1 1225
3381cb8d 1226 if (cfqg->new_weight) {
8184f93e 1227 cfqg->weight = cfqg->new_weight;
3381cb8d 1228 cfqg->new_weight = 0;
8184f93e 1229 }
e71357e1
TH
1230
1231 if (cfqg->new_leaf_weight) {
1232 cfqg->leaf_weight = cfqg->new_leaf_weight;
1233 cfqg->new_leaf_weight = 0;
1234 }
8184f93e
JT
1235}
1236
1237static void
1238cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1239{
7918ffb5
TH
1240 struct cfq_group *pos = cfqg;
1241 bool propagate;
1242
1243 /* add to the service tree */
8184f93e
JT
1244 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1245
1246 cfq_update_group_weight(cfqg);
1247 __cfq_group_service_tree_add(st, cfqg);
1248 st->total_weight += cfqg->weight;
7918ffb5
TH
1249
1250 /*
1251 * Activate @cfqg and propagate activation upwards until we meet an
1252 * already activated node or reach root.
1253 */
1254 propagate = !pos->nr_active++;
1255 pos->children_weight += pos->leaf_weight;
1256
1257 while (propagate) {
1258 struct cfq_group *parent = cfqg_flat_parent(pos);
1259
1260 if (!parent)
1261 break;
1262
1263 propagate = !parent->nr_active++;
1264 parent->children_weight += pos->weight;
1265 pos = parent;
1266 }
8184f93e
JT
1267}
1268
1269static void
1270cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d6
VG
1271{
1272 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1273 struct cfq_group *__cfqg;
1274 struct rb_node *n;
1275
1276 cfqg->nr_cfqq++;
760701bf 1277 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1fa8f6d6
VG
1278 return;
1279
1280 /*
1281 * Currently put the group at the end. Later implement something
1282 * so that groups get lesser vtime based on their weights, so that
25985edc 1283 * if group does not loose all if it was not continuously backlogged.
1fa8f6d6
VG
1284 */
1285 n = rb_last(&st->rb);
1286 if (n) {
1287 __cfqg = rb_entry_cfqg(n);
1288 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1289 } else
1290 cfqg->vdisktime = st->min_vdisktime;
8184f93e
JT
1291 cfq_group_service_tree_add(st, cfqg);
1292}
1fa8f6d6 1293
8184f93e
JT
1294static void
1295cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1296{
7918ffb5
TH
1297 struct cfq_group *pos = cfqg;
1298 bool propagate;
1299
1300 /*
1301 * Undo activation from cfq_group_service_tree_add(). Deactivate
1302 * @cfqg and propagate deactivation upwards.
1303 */
1304 propagate = !--pos->nr_active;
1305 pos->children_weight -= pos->leaf_weight;
1306
1307 while (propagate) {
1308 struct cfq_group *parent = cfqg_flat_parent(pos);
1309
1310 /* @pos has 0 nr_active at this point */
1311 WARN_ON_ONCE(pos->children_weight);
1312
1313 if (!parent)
1314 break;
1315
1316 propagate = !--parent->nr_active;
1317 parent->children_weight -= pos->weight;
1318 pos = parent;
1319 }
1320
1321 /* remove from the service tree */
8184f93e
JT
1322 st->total_weight -= cfqg->weight;
1323 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1324 cfq_rb_erase(&cfqg->rb_node, st);
1fa8f6d6
VG
1325}
1326
1327static void
8184f93e 1328cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
1fa8f6d6
VG
1329{
1330 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1331
1332 BUG_ON(cfqg->nr_cfqq < 1);
1333 cfqg->nr_cfqq--;
25bc6b07 1334
1fa8f6d6
VG
1335 /* If there are other cfq queues under this group, don't delete it */
1336 if (cfqg->nr_cfqq)
1337 return;
1338
2868ef7b 1339 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
8184f93e 1340 cfq_group_service_tree_del(st, cfqg);
4d2ceea4 1341 cfqg->saved_wl_slice = 0;
155fead9 1342 cfqg_stats_update_dequeue(cfqg);
dae739eb
VG
1343}
1344
167400d3
JT
1345static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1346 unsigned int *unaccounted_time)
dae739eb 1347{
f75edf2d 1348 unsigned int slice_used;
dae739eb
VG
1349
1350 /*
1351 * Queue got expired before even a single request completed or
1352 * got expired immediately after first request completion.
1353 */
1354 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1355 /*
1356 * Also charge the seek time incurred to the group, otherwise
1357 * if there are mutiple queues in the group, each can dispatch
1358 * a single request on seeky media and cause lots of seek time
1359 * and group will never know it.
1360 */
1361 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1362 1);
1363 } else {
1364 slice_used = jiffies - cfqq->slice_start;
167400d3
JT
1365 if (slice_used > cfqq->allocated_slice) {
1366 *unaccounted_time = slice_used - cfqq->allocated_slice;
f75edf2d 1367 slice_used = cfqq->allocated_slice;
167400d3
JT
1368 }
1369 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1370 *unaccounted_time += cfqq->slice_start -
1371 cfqq->dispatch_start;
dae739eb
VG
1372 }
1373
dae739eb
VG
1374 return slice_used;
1375}
1376
1377static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
e5ff082e 1378 struct cfq_queue *cfqq)
dae739eb
VG
1379{
1380 struct cfq_rb_root *st = &cfqd->grp_service_tree;
167400d3 1381 unsigned int used_sl, charge, unaccounted_sl = 0;
f26bd1f0
VG
1382 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1383 - cfqg->service_tree_idle.count;
1384
1385 BUG_ON(nr_sync < 0);
167400d3 1386 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
dae739eb 1387
02b35081
VG
1388 if (iops_mode(cfqd))
1389 charge = cfqq->slice_dispatch;
1390 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1391 charge = cfqq->allocated_slice;
dae739eb
VG
1392
1393 /* Can't update vdisktime while group is on service tree */
8184f93e 1394 cfq_group_service_tree_del(st, cfqg);
02b35081 1395 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
8184f93e
JT
1396 /* If a new weight was requested, update now, off tree */
1397 cfq_group_service_tree_add(st, cfqg);
dae739eb
VG
1398
1399 /* This group is being expired. Save the context */
1400 if (time_after(cfqd->workload_expires, jiffies)) {
4d2ceea4 1401 cfqg->saved_wl_slice = cfqd->workload_expires
dae739eb 1402 - jiffies;
4d2ceea4
VG
1403 cfqg->saved_wl_type = cfqd->serving_wl_type;
1404 cfqg->saved_wl_class = cfqd->serving_wl_class;
dae739eb 1405 } else
4d2ceea4 1406 cfqg->saved_wl_slice = 0;
2868ef7b
VG
1407
1408 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1409 st->min_vdisktime);
fd16d263
JP
1410 cfq_log_cfqq(cfqq->cfqd, cfqq,
1411 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1412 used_sl, cfqq->slice_dispatch, charge,
1413 iops_mode(cfqd), cfqq->nr_sectors);
155fead9
TH
1414 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1415 cfqg_stats_set_start_empty_time(cfqg);
1fa8f6d6
VG
1416}
1417
f51b802c
TH
1418/**
1419 * cfq_init_cfqg_base - initialize base part of a cfq_group
1420 * @cfqg: cfq_group to initialize
1421 *
1422 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1423 * is enabled or not.
1424 */
1425static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1426{
1427 struct cfq_rb_root *st;
1428 int i, j;
1429
1430 for_each_cfqg_st(cfqg, i, j, st)
1431 *st = CFQ_RB_ROOT;
1432 RB_CLEAR_NODE(&cfqg->rb_node);
1433
1434 cfqg->ttime.last_end_request = jiffies;
1435}
1436
25fb5169 1437#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 1438static void cfq_pd_init(struct blkcg_gq *blkg)
f469a7b4 1439{
0381411e 1440 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
25fb5169 1441
f51b802c 1442 cfq_init_cfqg_base(cfqg);
3381cb8d 1443 cfqg->weight = blkg->blkcg->cfq_weight;
e71357e1 1444 cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
25fb5169
VG
1445}
1446
1447/*
3e59cf9d
VG
1448 * Search for the cfq group current task belongs to. request_queue lock must
1449 * be held.
25fb5169 1450 */
cd1604fa 1451static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
3c798398 1452 struct blkcg *blkcg)
25fb5169 1453{
f469a7b4 1454 struct request_queue *q = cfqd->queue;
cd1604fa 1455 struct cfq_group *cfqg = NULL;
25fb5169 1456
3c798398
TH
1457 /* avoid lookup for the common case where there's no blkcg */
1458 if (blkcg == &blkcg_root) {
cd1604fa
TH
1459 cfqg = cfqd->root_group;
1460 } else {
3c798398 1461 struct blkcg_gq *blkg;
f469a7b4 1462
3c96cb32 1463 blkg = blkg_lookup_create(blkcg, q);
cd1604fa 1464 if (!IS_ERR(blkg))
0381411e 1465 cfqg = blkg_to_cfqg(blkg);
cd1604fa 1466 }
f469a7b4 1467
25fb5169
VG
1468 return cfqg;
1469}
1470
1471static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1472{
1473 /* Currently, all async queues are mapped to root group */
1474 if (!cfq_cfqq_sync(cfqq))
f51b802c 1475 cfqg = cfqq->cfqd->root_group;
25fb5169
VG
1476
1477 cfqq->cfqg = cfqg;
b1c35769 1478 /* cfqq reference on cfqg */
eb7d8c07 1479 cfqg_get(cfqg);
b1c35769
VG
1480}
1481
f95a04af
TH
1482static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1483 struct blkg_policy_data *pd, int off)
60c2bc2d 1484{
f95a04af 1485 struct cfq_group *cfqg = pd_to_cfqg(pd);
3381cb8d
TH
1486
1487 if (!cfqg->dev_weight)
60c2bc2d 1488 return 0;
f95a04af 1489 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
60c2bc2d
TH
1490}
1491
3381cb8d
TH
1492static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1493 struct seq_file *sf)
60c2bc2d 1494{
3c798398
TH
1495 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1496 cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
60c2bc2d
TH
1497 false);
1498 return 0;
1499}
1500
e71357e1
TH
1501static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1502 struct blkg_policy_data *pd, int off)
1503{
1504 struct cfq_group *cfqg = pd_to_cfqg(pd);
1505
1506 if (!cfqg->dev_leaf_weight)
1507 return 0;
1508 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1509}
1510
1511static int cfqg_print_leaf_weight_device(struct cgroup *cgrp,
1512 struct cftype *cft,
1513 struct seq_file *sf)
1514{
1515 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1516 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0,
1517 false);
1518 return 0;
1519}
1520
3381cb8d
TH
1521static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
1522 struct seq_file *sf)
60c2bc2d 1523{
3c798398 1524 seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
60c2bc2d
TH
1525 return 0;
1526}
1527
e71357e1
TH
1528static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft,
1529 struct seq_file *sf)
1530{
1531 seq_printf(sf, "%u\n",
1532 cgroup_to_blkcg(cgrp)->cfq_leaf_weight);
1533 return 0;
1534}
1535
1536static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1537 const char *buf, bool is_leaf_weight)
60c2bc2d 1538{
3c798398 1539 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
60c2bc2d 1540 struct blkg_conf_ctx ctx;
3381cb8d 1541 struct cfq_group *cfqg;
60c2bc2d
TH
1542 int ret;
1543
3c798398 1544 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
60c2bc2d
TH
1545 if (ret)
1546 return ret;
1547
1548 ret = -EINVAL;
3381cb8d 1549 cfqg = blkg_to_cfqg(ctx.blkg);
a2b1693b 1550 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
e71357e1
TH
1551 if (!is_leaf_weight) {
1552 cfqg->dev_weight = ctx.v;
1553 cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
1554 } else {
1555 cfqg->dev_leaf_weight = ctx.v;
1556 cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
1557 }
60c2bc2d
TH
1558 ret = 0;
1559 }
1560
1561 blkg_conf_finish(&ctx);
1562 return ret;
1563}
1564
e71357e1
TH
1565static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1566 const char *buf)
1567{
1568 return __cfqg_set_weight_device(cgrp, cft, buf, false);
1569}
1570
1571static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft,
1572 const char *buf)
1573{
1574 return __cfqg_set_weight_device(cgrp, cft, buf, true);
1575}
1576
1577static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val,
1578 bool is_leaf_weight)
60c2bc2d 1579{
3c798398
TH
1580 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
1581 struct blkcg_gq *blkg;
60c2bc2d
TH
1582 struct hlist_node *n;
1583
3381cb8d 1584 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
60c2bc2d
TH
1585 return -EINVAL;
1586
1587 spin_lock_irq(&blkcg->lock);
e71357e1
TH
1588
1589 if (!is_leaf_weight)
1590 blkcg->cfq_weight = val;
1591 else
1592 blkcg->cfq_leaf_weight = val;
60c2bc2d
TH
1593
1594 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
3381cb8d 1595 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
60c2bc2d 1596
e71357e1
TH
1597 if (!cfqg)
1598 continue;
1599
1600 if (!is_leaf_weight) {
1601 if (!cfqg->dev_weight)
1602 cfqg->new_weight = blkcg->cfq_weight;
1603 } else {
1604 if (!cfqg->dev_leaf_weight)
1605 cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
1606 }
60c2bc2d
TH
1607 }
1608
1609 spin_unlock_irq(&blkcg->lock);
1610 return 0;
1611}
1612
e71357e1
TH
1613static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1614{
1615 return __cfq_set_weight(cgrp, cft, val, false);
1616}
1617
1618static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1619{
1620 return __cfq_set_weight(cgrp, cft, val, true);
1621}
1622
5bc4afb1
TH
1623static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
1624 struct seq_file *sf)
1625{
3c798398 1626 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
5bc4afb1 1627
3c798398 1628 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
5bc4afb1
TH
1629 cft->private, false);
1630 return 0;
1631}
1632
1633static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
1634 struct seq_file *sf)
1635{
3c798398 1636 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
5bc4afb1 1637
3c798398 1638 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
5bc4afb1
TH
1639 cft->private, true);
1640 return 0;
1641}
1642
60c2bc2d 1643#ifdef CONFIG_DEBUG_BLK_CGROUP
f95a04af
TH
1644static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1645 struct blkg_policy_data *pd, int off)
60c2bc2d 1646{
f95a04af 1647 struct cfq_group *cfqg = pd_to_cfqg(pd);
155fead9 1648 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
60c2bc2d
TH
1649 u64 v = 0;
1650
1651 if (samples) {
155fead9 1652 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
60c2bc2d
TH
1653 do_div(v, samples);
1654 }
f95a04af 1655 __blkg_prfill_u64(sf, pd, v);
60c2bc2d
TH
1656 return 0;
1657}
1658
1659/* print avg_queue_size */
155fead9
TH
1660static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1661 struct seq_file *sf)
60c2bc2d 1662{
3c798398 1663 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
60c2bc2d 1664
155fead9 1665 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
3c798398 1666 &blkcg_policy_cfq, 0, false);
60c2bc2d
TH
1667 return 0;
1668}
1669#endif /* CONFIG_DEBUG_BLK_CGROUP */
1670
1671static struct cftype cfq_blkcg_files[] = {
1672 {
1673 .name = "weight_device",
3381cb8d
TH
1674 .read_seq_string = cfqg_print_weight_device,
1675 .write_string = cfqg_set_weight_device,
60c2bc2d
TH
1676 .max_write_len = 256,
1677 },
1678 {
1679 .name = "weight",
3381cb8d
TH
1680 .read_seq_string = cfq_print_weight,
1681 .write_u64 = cfq_set_weight,
60c2bc2d 1682 },
e71357e1
TH
1683
1684 /* on root, leaf_weight is mapped to weight */
1685 {
1686 .name = "leaf_weight_device",
1687 .flags = CFTYPE_ONLY_ON_ROOT,
1688 .read_seq_string = cfqg_print_weight_device,
1689 .write_string = cfqg_set_weight_device,
1690 .max_write_len = 256,
1691 },
1692 {
1693 .name = "leaf_weight",
1694 .flags = CFTYPE_ONLY_ON_ROOT,
1695 .read_seq_string = cfq_print_weight,
1696 .write_u64 = cfq_set_weight,
1697 },
1698
1699 /* no such mapping necessary for !roots */
1700 {
1701 .name = "leaf_weight_device",
1702 .flags = CFTYPE_NOT_ON_ROOT,
1703 .read_seq_string = cfqg_print_leaf_weight_device,
1704 .write_string = cfqg_set_leaf_weight_device,
1705 .max_write_len = 256,
1706 },
1707 {
1708 .name = "leaf_weight",
1709 .flags = CFTYPE_NOT_ON_ROOT,
1710 .read_seq_string = cfq_print_leaf_weight,
1711 .write_u64 = cfq_set_leaf_weight,
1712 },
1713
60c2bc2d
TH
1714 {
1715 .name = "time",
5bc4afb1
TH
1716 .private = offsetof(struct cfq_group, stats.time),
1717 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1718 },
1719 {
1720 .name = "sectors",
5bc4afb1
TH
1721 .private = offsetof(struct cfq_group, stats.sectors),
1722 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1723 },
1724 {
1725 .name = "io_service_bytes",
5bc4afb1
TH
1726 .private = offsetof(struct cfq_group, stats.service_bytes),
1727 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1728 },
1729 {
1730 .name = "io_serviced",
5bc4afb1
TH
1731 .private = offsetof(struct cfq_group, stats.serviced),
1732 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1733 },
1734 {
1735 .name = "io_service_time",
5bc4afb1
TH
1736 .private = offsetof(struct cfq_group, stats.service_time),
1737 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1738 },
1739 {
1740 .name = "io_wait_time",
5bc4afb1
TH
1741 .private = offsetof(struct cfq_group, stats.wait_time),
1742 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1743 },
1744 {
1745 .name = "io_merged",
5bc4afb1
TH
1746 .private = offsetof(struct cfq_group, stats.merged),
1747 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1748 },
1749 {
1750 .name = "io_queued",
5bc4afb1
TH
1751 .private = offsetof(struct cfq_group, stats.queued),
1752 .read_seq_string = cfqg_print_rwstat,
60c2bc2d
TH
1753 },
1754#ifdef CONFIG_DEBUG_BLK_CGROUP
1755 {
1756 .name = "avg_queue_size",
155fead9 1757 .read_seq_string = cfqg_print_avg_queue_size,
60c2bc2d
TH
1758 },
1759 {
1760 .name = "group_wait_time",
5bc4afb1
TH
1761 .private = offsetof(struct cfq_group, stats.group_wait_time),
1762 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1763 },
1764 {
1765 .name = "idle_time",
5bc4afb1
TH
1766 .private = offsetof(struct cfq_group, stats.idle_time),
1767 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1768 },
1769 {
1770 .name = "empty_time",
5bc4afb1
TH
1771 .private = offsetof(struct cfq_group, stats.empty_time),
1772 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1773 },
1774 {
1775 .name = "dequeue",
5bc4afb1
TH
1776 .private = offsetof(struct cfq_group, stats.dequeue),
1777 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1778 },
1779 {
1780 .name = "unaccounted_time",
5bc4afb1
TH
1781 .private = offsetof(struct cfq_group, stats.unaccounted_time),
1782 .read_seq_string = cfqg_print_stat,
60c2bc2d
TH
1783 },
1784#endif /* CONFIG_DEBUG_BLK_CGROUP */
1785 { } /* terminate */
1786};
25fb5169 1787#else /* GROUP_IOSCHED */
cd1604fa 1788static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
3c798398 1789 struct blkcg *blkcg)
25fb5169 1790{
f51b802c 1791 return cfqd->root_group;
25fb5169 1792}
7f1dc8a2 1793
25fb5169
VG
1794static inline void
1795cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1796 cfqq->cfqg = cfqg;
1797}
1798
1799#endif /* GROUP_IOSCHED */
1800
498d3aa2 1801/*
c0324a02 1802 * The cfqd->service_trees holds all pending cfq_queue's that have
498d3aa2
JA
1803 * requests waiting to be processed. It is sorted in the order that
1804 * we will service the queues.
1805 */
a36e71f9 1806static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 1807 bool add_front)
d9e7620e 1808{
0871714e
JA
1809 struct rb_node **p, *parent;
1810 struct cfq_queue *__cfqq;
d9e7620e 1811 unsigned long rb_key;
34b98d03 1812 struct cfq_rb_root *st;
498d3aa2 1813 int left;
dae739eb 1814 int new_cfqq = 1;
ae30c286 1815
34b98d03 1816 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
0871714e
JA
1817 if (cfq_class_idle(cfqq)) {
1818 rb_key = CFQ_IDLE_DELAY;
34b98d03 1819 parent = rb_last(&st->rb);
0871714e
JA
1820 if (parent && parent != &cfqq->rb_node) {
1821 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1822 rb_key += __cfqq->rb_key;
1823 } else
1824 rb_key += jiffies;
1825 } else if (!add_front) {
b9c8946b
JA
1826 /*
1827 * Get our rb key offset. Subtract any residual slice
1828 * value carried from last service. A negative resid
1829 * count indicates slice overrun, and this should position
1830 * the next service time further away in the tree.
1831 */
edd75ffd 1832 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
b9c8946b 1833 rb_key -= cfqq->slice_resid;
edd75ffd 1834 cfqq->slice_resid = 0;
48e025e6
CZ
1835 } else {
1836 rb_key = -HZ;
34b98d03 1837 __cfqq = cfq_rb_first(st);
48e025e6
CZ
1838 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1839 }
1da177e4 1840
d9e7620e 1841 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
dae739eb 1842 new_cfqq = 0;
99f9628a 1843 /*
d9e7620e 1844 * same position, nothing more to do
99f9628a 1845 */
34b98d03 1846 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
d9e7620e 1847 return;
1da177e4 1848
aa6f6a3d
CZ
1849 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1850 cfqq->service_tree = NULL;
1da177e4 1851 }
d9e7620e 1852
498d3aa2 1853 left = 1;
0871714e 1854 parent = NULL;
34b98d03
VG
1855 cfqq->service_tree = st;
1856 p = &st->rb.rb_node;
d9e7620e
JA
1857 while (*p) {
1858 parent = *p;
1859 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1860
0c534e0a 1861 /*
c0324a02 1862 * sort by key, that represents service time.
0c534e0a 1863 */
c0324a02 1864 if (time_before(rb_key, __cfqq->rb_key))
1f23f121 1865 p = &parent->rb_left;
c0324a02 1866 else {
1f23f121 1867 p = &parent->rb_right;
cc09e299 1868 left = 0;
c0324a02 1869 }
d9e7620e
JA
1870 }
1871
cc09e299 1872 if (left)
34b98d03 1873 st->left = &cfqq->rb_node;
cc09e299 1874
d9e7620e
JA
1875 cfqq->rb_key = rb_key;
1876 rb_link_node(&cfqq->rb_node, parent, p);
34b98d03
VG
1877 rb_insert_color(&cfqq->rb_node, &st->rb);
1878 st->count++;
20359f27 1879 if (add_front || !new_cfqq)
dae739eb 1880 return;
8184f93e 1881 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
1da177e4
LT
1882}
1883
a36e71f9 1884static struct cfq_queue *
f2d1f0ae
JA
1885cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1886 sector_t sector, struct rb_node **ret_parent,
1887 struct rb_node ***rb_link)
a36e71f9 1888{
a36e71f9
JA
1889 struct rb_node **p, *parent;
1890 struct cfq_queue *cfqq = NULL;
1891
1892 parent = NULL;
1893 p = &root->rb_node;
1894 while (*p) {
1895 struct rb_node **n;
1896
1897 parent = *p;
1898 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1899
1900 /*
1901 * Sort strictly based on sector. Smallest to the left,
1902 * largest to the right.
1903 */
2e46e8b2 1904 if (sector > blk_rq_pos(cfqq->next_rq))
a36e71f9 1905 n = &(*p)->rb_right;
2e46e8b2 1906 else if (sector < blk_rq_pos(cfqq->next_rq))
a36e71f9
JA
1907 n = &(*p)->rb_left;
1908 else
1909 break;
1910 p = n;
3ac6c9f8 1911 cfqq = NULL;
a36e71f9
JA
1912 }
1913
1914 *ret_parent = parent;
1915 if (rb_link)
1916 *rb_link = p;
3ac6c9f8 1917 return cfqq;
a36e71f9
JA
1918}
1919
1920static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1921{
a36e71f9
JA
1922 struct rb_node **p, *parent;
1923 struct cfq_queue *__cfqq;
1924
f2d1f0ae
JA
1925 if (cfqq->p_root) {
1926 rb_erase(&cfqq->p_node, cfqq->p_root);
1927 cfqq->p_root = NULL;
1928 }
a36e71f9
JA
1929
1930 if (cfq_class_idle(cfqq))
1931 return;
1932 if (!cfqq->next_rq)
1933 return;
1934
f2d1f0ae 1935 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
2e46e8b2
TH
1936 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1937 blk_rq_pos(cfqq->next_rq), &parent, &p);
3ac6c9f8
JA
1938 if (!__cfqq) {
1939 rb_link_node(&cfqq->p_node, parent, p);
f2d1f0ae
JA
1940 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1941 } else
1942 cfqq->p_root = NULL;
a36e71f9
JA
1943}
1944
498d3aa2
JA
1945/*
1946 * Update cfqq's position in the service tree.
1947 */
edd75ffd 1948static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
6d048f53 1949{
6d048f53
JA
1950 /*
1951 * Resorting requires the cfqq to be on the RR list already.
1952 */
a36e71f9 1953 if (cfq_cfqq_on_rr(cfqq)) {
edd75ffd 1954 cfq_service_tree_add(cfqd, cfqq, 0);
a36e71f9
JA
1955 cfq_prio_tree_add(cfqd, cfqq);
1956 }
6d048f53
JA
1957}
1958
1da177e4
LT
1959/*
1960 * add to busy list of queues for service, trying to be fair in ordering
22e2c507 1961 * the pending list according to last request service
1da177e4 1962 */
febffd61 1963static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 1964{
7b679138 1965 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
3b18152c
JA
1966 BUG_ON(cfq_cfqq_on_rr(cfqq));
1967 cfq_mark_cfqq_on_rr(cfqq);
1da177e4 1968 cfqd->busy_queues++;
ef8a41df
SL
1969 if (cfq_cfqq_sync(cfqq))
1970 cfqd->busy_sync_queues++;
1da177e4 1971
edd75ffd 1972 cfq_resort_rr_list(cfqd, cfqq);
1da177e4
LT
1973}
1974
498d3aa2
JA
1975/*
1976 * Called when the cfqq no longer has requests pending, remove it from
1977 * the service tree.
1978 */
febffd61 1979static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1da177e4 1980{
7b679138 1981 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
3b18152c
JA
1982 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1983 cfq_clear_cfqq_on_rr(cfqq);
1da177e4 1984
aa6f6a3d
CZ
1985 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1986 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1987 cfqq->service_tree = NULL;
1988 }
f2d1f0ae
JA
1989 if (cfqq->p_root) {
1990 rb_erase(&cfqq->p_node, cfqq->p_root);
1991 cfqq->p_root = NULL;
1992 }
d9e7620e 1993
8184f93e 1994 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
1da177e4
LT
1995 BUG_ON(!cfqd->busy_queues);
1996 cfqd->busy_queues--;
ef8a41df
SL
1997 if (cfq_cfqq_sync(cfqq))
1998 cfqd->busy_sync_queues--;
1da177e4
LT
1999}
2000
2001/*
2002 * rb tree support functions
2003 */
febffd61 2004static void cfq_del_rq_rb(struct request *rq)
1da177e4 2005{
5e705374 2006 struct cfq_queue *cfqq = RQ_CFQQ(rq);
5e705374 2007 const int sync = rq_is_sync(rq);
1da177e4 2008
b4878f24
JA
2009 BUG_ON(!cfqq->queued[sync]);
2010 cfqq->queued[sync]--;
1da177e4 2011
5e705374 2012 elv_rb_del(&cfqq->sort_list, rq);
1da177e4 2013
f04a6424
VG
2014 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2015 /*
2016 * Queue will be deleted from service tree when we actually
2017 * expire it later. Right now just remove it from prio tree
2018 * as it is empty.
2019 */
2020 if (cfqq->p_root) {
2021 rb_erase(&cfqq->p_node, cfqq->p_root);
2022 cfqq->p_root = NULL;
2023 }
2024 }
1da177e4
LT
2025}
2026
5e705374 2027static void cfq_add_rq_rb(struct request *rq)
1da177e4 2028{
5e705374 2029 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 2030 struct cfq_data *cfqd = cfqq->cfqd;
796d5116 2031 struct request *prev;
1da177e4 2032
5380a101 2033 cfqq->queued[rq_is_sync(rq)]++;
1da177e4 2034
796d5116 2035 elv_rb_add(&cfqq->sort_list, rq);
5fccbf61
JA
2036
2037 if (!cfq_cfqq_on_rr(cfqq))
2038 cfq_add_cfqq_rr(cfqd, cfqq);
5044eed4
JA
2039
2040 /*
2041 * check if this request is a better next-serve candidate
2042 */
a36e71f9 2043 prev = cfqq->next_rq;
cf7c25cf 2044 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
a36e71f9
JA
2045
2046 /*
2047 * adjust priority tree position, if ->next_rq changes
2048 */
2049 if (prev != cfqq->next_rq)
2050 cfq_prio_tree_add(cfqd, cfqq);
2051
5044eed4 2052 BUG_ON(!cfqq->next_rq);
1da177e4
LT
2053}
2054
febffd61 2055static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
1da177e4 2056{
5380a101
JA
2057 elv_rb_del(&cfqq->sort_list, rq);
2058 cfqq->queued[rq_is_sync(rq)]--;
155fead9 2059 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
5e705374 2060 cfq_add_rq_rb(rq);
155fead9
TH
2061 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2062 rq->cmd_flags);
1da177e4
LT
2063}
2064
206dc69b
JA
2065static struct request *
2066cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
1da177e4 2067{
206dc69b 2068 struct task_struct *tsk = current;
c5869807 2069 struct cfq_io_cq *cic;
206dc69b 2070 struct cfq_queue *cfqq;
1da177e4 2071
4ac845a2 2072 cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317
VT
2073 if (!cic)
2074 return NULL;
2075
2076 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
89850f7e
JA
2077 if (cfqq) {
2078 sector_t sector = bio->bi_sector + bio_sectors(bio);
2079
21183b07 2080 return elv_rb_find(&cfqq->sort_list, sector);
89850f7e 2081 }
1da177e4 2082
1da177e4
LT
2083 return NULL;
2084}
2085
165125e1 2086static void cfq_activate_request(struct request_queue *q, struct request *rq)
1da177e4 2087{
22e2c507 2088 struct cfq_data *cfqd = q->elevator->elevator_data;
3b18152c 2089
53c583d2 2090 cfqd->rq_in_driver++;
7b679138 2091 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
53c583d2 2092 cfqd->rq_in_driver);
25776e35 2093
5b93629b 2094 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
1da177e4
LT
2095}
2096
165125e1 2097static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
1da177e4 2098{
b4878f24
JA
2099 struct cfq_data *cfqd = q->elevator->elevator_data;
2100
53c583d2
CZ
2101 WARN_ON(!cfqd->rq_in_driver);
2102 cfqd->rq_in_driver--;
7b679138 2103 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
53c583d2 2104 cfqd->rq_in_driver);
1da177e4
LT
2105}
2106
b4878f24 2107static void cfq_remove_request(struct request *rq)
1da177e4 2108{
5e705374 2109 struct cfq_queue *cfqq = RQ_CFQQ(rq);
21183b07 2110
5e705374
JA
2111 if (cfqq->next_rq == rq)
2112 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
1da177e4 2113
b4878f24 2114 list_del_init(&rq->queuelist);
5e705374 2115 cfq_del_rq_rb(rq);
374f84ac 2116
45333d5a 2117 cfqq->cfqd->rq_queued--;
155fead9 2118 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
65299a3b
CH
2119 if (rq->cmd_flags & REQ_PRIO) {
2120 WARN_ON(!cfqq->prio_pending);
2121 cfqq->prio_pending--;
b53d1ed7 2122 }
1da177e4
LT
2123}
2124
165125e1
JA
2125static int cfq_merge(struct request_queue *q, struct request **req,
2126 struct bio *bio)
1da177e4
LT
2127{
2128 struct cfq_data *cfqd = q->elevator->elevator_data;
2129 struct request *__rq;
1da177e4 2130
206dc69b 2131 __rq = cfq_find_rq_fmerge(cfqd, bio);
22e2c507 2132 if (__rq && elv_rq_merge_ok(__rq, bio)) {
9817064b
JA
2133 *req = __rq;
2134 return ELEVATOR_FRONT_MERGE;
1da177e4
LT
2135 }
2136
2137 return ELEVATOR_NO_MERGE;
1da177e4
LT
2138}
2139
165125e1 2140static void cfq_merged_request(struct request_queue *q, struct request *req,
21183b07 2141 int type)
1da177e4 2142{
21183b07 2143 if (type == ELEVATOR_FRONT_MERGE) {
5e705374 2144 struct cfq_queue *cfqq = RQ_CFQQ(req);
1da177e4 2145
5e705374 2146 cfq_reposition_rq_rb(cfqq, req);
1da177e4 2147 }
1da177e4
LT
2148}
2149
812d4026
DS
2150static void cfq_bio_merged(struct request_queue *q, struct request *req,
2151 struct bio *bio)
2152{
155fead9 2153 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
812d4026
DS
2154}
2155
1da177e4 2156static void
165125e1 2157cfq_merged_requests(struct request_queue *q, struct request *rq,
1da177e4
LT
2158 struct request *next)
2159{
cf7c25cf 2160 struct cfq_queue *cfqq = RQ_CFQQ(rq);
4a0b75c7
SL
2161 struct cfq_data *cfqd = q->elevator->elevator_data;
2162
22e2c507
JA
2163 /*
2164 * reposition in fifo if next is older than rq
2165 */
2166 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
3d106fba
SL
2167 time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
2168 cfqq == RQ_CFQQ(next)) {
22e2c507 2169 list_move(&rq->queuelist, &next->queuelist);
30996f40
JA
2170 rq_set_fifo_time(rq, rq_fifo_time(next));
2171 }
22e2c507 2172
cf7c25cf
CZ
2173 if (cfqq->next_rq == next)
2174 cfqq->next_rq = rq;
b4878f24 2175 cfq_remove_request(next);
155fead9 2176 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
4a0b75c7
SL
2177
2178 cfqq = RQ_CFQQ(next);
2179 /*
2180 * all requests of this queue are merged to other queues, delete it
2181 * from the service tree. If it's the active_queue,
2182 * cfq_dispatch_requests() will choose to expire it or do idle
2183 */
2184 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2185 cfqq != cfqd->active_queue)
2186 cfq_del_cfqq_rr(cfqd, cfqq);
22e2c507
JA
2187}
2188
165125e1 2189static int cfq_allow_merge(struct request_queue *q, struct request *rq,
da775265
JA
2190 struct bio *bio)
2191{
2192 struct cfq_data *cfqd = q->elevator->elevator_data;
c5869807 2193 struct cfq_io_cq *cic;
da775265 2194 struct cfq_queue *cfqq;
da775265
JA
2195
2196 /*
ec8acb69 2197 * Disallow merge of a sync bio into an async request.
da775265 2198 */
91fac317 2199 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
a6151c3a 2200 return false;
da775265
JA
2201
2202 /*
f1a4f4d3 2203 * Lookup the cfqq that this bio will be queued with and allow
07c2bd37 2204 * merge only if rq is queued there.
f1a4f4d3 2205 */
07c2bd37
TH
2206 cic = cfq_cic_lookup(cfqd, current->io_context);
2207 if (!cic)
2208 return false;
719d3402 2209
91fac317 2210 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
a6151c3a 2211 return cfqq == RQ_CFQQ(rq);
da775265
JA
2212}
2213
812df48d
DS
2214static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2215{
2216 del_timer(&cfqd->idle_slice_timer);
155fead9 2217 cfqg_stats_update_idle_time(cfqq->cfqg);
812df48d
DS
2218}
2219
febffd61
JA
2220static void __cfq_set_active_queue(struct cfq_data *cfqd,
2221 struct cfq_queue *cfqq)
22e2c507
JA
2222{
2223 if (cfqq) {
3bf10fea 2224 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
4d2ceea4 2225 cfqd->serving_wl_class, cfqd->serving_wl_type);
155fead9 2226 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
62a37f6b
JT
2227 cfqq->slice_start = 0;
2228 cfqq->dispatch_start = jiffies;
2229 cfqq->allocated_slice = 0;
2230 cfqq->slice_end = 0;
2231 cfqq->slice_dispatch = 0;
2232 cfqq->nr_sectors = 0;
2233
2234 cfq_clear_cfqq_wait_request(cfqq);
2235 cfq_clear_cfqq_must_dispatch(cfqq);
2236 cfq_clear_cfqq_must_alloc_slice(cfqq);
2237 cfq_clear_cfqq_fifo_expire(cfqq);
2238 cfq_mark_cfqq_slice_new(cfqq);
2239
2240 cfq_del_timer(cfqd, cfqq);
22e2c507
JA
2241 }
2242
2243 cfqd->active_queue = cfqq;
2244}
2245
7b14e3b5
JA
2246/*
2247 * current cfqq expired its slice (or was too idle), select new one
2248 */
2249static void
2250__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e5ff082e 2251 bool timed_out)
7b14e3b5 2252{
7b679138
JA
2253 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2254
7b14e3b5 2255 if (cfq_cfqq_wait_request(cfqq))
812df48d 2256 cfq_del_timer(cfqd, cfqq);
7b14e3b5 2257
7b14e3b5 2258 cfq_clear_cfqq_wait_request(cfqq);
f75edf2d 2259 cfq_clear_cfqq_wait_busy(cfqq);
7b14e3b5 2260
ae54abed
SL
2261 /*
2262 * If this cfqq is shared between multiple processes, check to
2263 * make sure that those processes are still issuing I/Os within
2264 * the mean seek distance. If not, it may be time to break the
2265 * queues apart again.
2266 */
2267 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2268 cfq_mark_cfqq_split_coop(cfqq);
2269
7b14e3b5 2270 /*
6084cdda 2271 * store what was left of this slice, if the queue idled/timed out
7b14e3b5 2272 */
c553f8e3
SL
2273 if (timed_out) {
2274 if (cfq_cfqq_slice_new(cfqq))
ba5bd520 2275 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
c553f8e3
SL
2276 else
2277 cfqq->slice_resid = cfqq->slice_end - jiffies;
7b679138
JA
2278 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2279 }
7b14e3b5 2280
e5ff082e 2281 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
dae739eb 2282
f04a6424
VG
2283 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2284 cfq_del_cfqq_rr(cfqd, cfqq);
2285
edd75ffd 2286 cfq_resort_rr_list(cfqd, cfqq);
7b14e3b5
JA
2287
2288 if (cfqq == cfqd->active_queue)
2289 cfqd->active_queue = NULL;
2290
2291 if (cfqd->active_cic) {
11a3122f 2292 put_io_context(cfqd->active_cic->icq.ioc);
7b14e3b5
JA
2293 cfqd->active_cic = NULL;
2294 }
7b14e3b5
JA
2295}
2296
e5ff082e 2297static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
7b14e3b5
JA
2298{
2299 struct cfq_queue *cfqq = cfqd->active_queue;
2300
2301 if (cfqq)
e5ff082e 2302 __cfq_slice_expired(cfqd, cfqq, timed_out);
7b14e3b5
JA
2303}
2304
498d3aa2
JA
2305/*
2306 * Get next queue for service. Unless we have a queue preemption,
2307 * we'll simply select the first cfqq in the service tree.
2308 */
6d048f53 2309static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
22e2c507 2310{
34b98d03
VG
2311 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2312 cfqd->serving_wl_class, cfqd->serving_wl_type);
d9e7620e 2313
f04a6424
VG
2314 if (!cfqd->rq_queued)
2315 return NULL;
2316
1fa8f6d6 2317 /* There is nothing to dispatch */
34b98d03 2318 if (!st)
1fa8f6d6 2319 return NULL;
34b98d03 2320 if (RB_EMPTY_ROOT(&st->rb))
c0324a02 2321 return NULL;
34b98d03 2322 return cfq_rb_first(st);
6d048f53
JA
2323}
2324
f04a6424
VG
2325static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2326{
25fb5169 2327 struct cfq_group *cfqg;
f04a6424
VG
2328 struct cfq_queue *cfqq;
2329 int i, j;
2330 struct cfq_rb_root *st;
2331
2332 if (!cfqd->rq_queued)
2333 return NULL;
2334
25fb5169
VG
2335 cfqg = cfq_get_next_cfqg(cfqd);
2336 if (!cfqg)
2337 return NULL;
2338
f04a6424
VG
2339 for_each_cfqg_st(cfqg, i, j, st)
2340 if ((cfqq = cfq_rb_first(st)) != NULL)
2341 return cfqq;
2342 return NULL;
2343}
2344
498d3aa2
JA
2345/*
2346 * Get and set a new active queue for service.
2347 */
a36e71f9
JA
2348static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2349 struct cfq_queue *cfqq)
6d048f53 2350{
e00ef799 2351 if (!cfqq)
a36e71f9 2352 cfqq = cfq_get_next_queue(cfqd);
6d048f53 2353
22e2c507 2354 __cfq_set_active_queue(cfqd, cfqq);
3b18152c 2355 return cfqq;
22e2c507
JA
2356}
2357
d9e7620e
JA
2358static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2359 struct request *rq)
2360{
83096ebf
TH
2361 if (blk_rq_pos(rq) >= cfqd->last_position)
2362 return blk_rq_pos(rq) - cfqd->last_position;
d9e7620e 2363 else
83096ebf 2364 return cfqd->last_position - blk_rq_pos(rq);
d9e7620e
JA
2365}
2366
b2c18e1e 2367static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
e9ce335d 2368 struct request *rq)
6d048f53 2369{
e9ce335d 2370 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
6d048f53
JA
2371}
2372
a36e71f9
JA
2373static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2374 struct cfq_queue *cur_cfqq)
2375{
f2d1f0ae 2376 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
a36e71f9
JA
2377 struct rb_node *parent, *node;
2378 struct cfq_queue *__cfqq;
2379 sector_t sector = cfqd->last_position;
2380
2381 if (RB_EMPTY_ROOT(root))
2382 return NULL;
2383
2384 /*
2385 * First, if we find a request starting at the end of the last
2386 * request, choose it.
2387 */
f2d1f0ae 2388 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
a36e71f9
JA
2389 if (__cfqq)
2390 return __cfqq;
2391
2392 /*
2393 * If the exact sector wasn't found, the parent of the NULL leaf
2394 * will contain the closest sector.
2395 */
2396 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
e9ce335d 2397 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f9
JA
2398 return __cfqq;
2399
2e46e8b2 2400 if (blk_rq_pos(__cfqq->next_rq) < sector)
a36e71f9
JA
2401 node = rb_next(&__cfqq->p_node);
2402 else
2403 node = rb_prev(&__cfqq->p_node);
2404 if (!node)
2405 return NULL;
2406
2407 __cfqq = rb_entry(node, struct cfq_queue, p_node);
e9ce335d 2408 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
a36e71f9
JA
2409 return __cfqq;
2410
2411 return NULL;
2412}
2413
2414/*
2415 * cfqd - obvious
2416 * cur_cfqq - passed in so that we don't decide that the current queue is
2417 * closely cooperating with itself.
2418 *
2419 * So, basically we're assuming that that cur_cfqq has dispatched at least
2420 * one request, and that cfqd->last_position reflects a position on the disk
2421 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2422 * assumption.
2423 */
2424static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
b3b6d040 2425 struct cfq_queue *cur_cfqq)
6d048f53 2426{
a36e71f9
JA
2427 struct cfq_queue *cfqq;
2428
39c01b21
DS
2429 if (cfq_class_idle(cur_cfqq))
2430 return NULL;
e6c5bc73
JM
2431 if (!cfq_cfqq_sync(cur_cfqq))
2432 return NULL;
2433 if (CFQQ_SEEKY(cur_cfqq))
2434 return NULL;
2435
b9d8f4c7
GJ
2436 /*
2437 * Don't search priority tree if it's the only queue in the group.
2438 */
2439 if (cur_cfqq->cfqg->nr_cfqq == 1)
2440 return NULL;
2441
6d048f53 2442 /*
d9e7620e
JA
2443 * We should notice if some of the queues are cooperating, eg
2444 * working closely on the same area of the disk. In that case,
2445 * we can group them together and don't waste time idling.
6d048f53 2446 */
a36e71f9
JA
2447 cfqq = cfqq_close(cfqd, cur_cfqq);
2448 if (!cfqq)
2449 return NULL;
2450
8682e1f1
VG
2451 /* If new queue belongs to different cfq_group, don't choose it */
2452 if (cur_cfqq->cfqg != cfqq->cfqg)
2453 return NULL;
2454
df5fe3e8
JM
2455 /*
2456 * It only makes sense to merge sync queues.
2457 */
2458 if (!cfq_cfqq_sync(cfqq))
2459 return NULL;
e6c5bc73
JM
2460 if (CFQQ_SEEKY(cfqq))
2461 return NULL;
df5fe3e8 2462
c0324a02
CZ
2463 /*
2464 * Do not merge queues of different priority classes
2465 */
2466 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2467 return NULL;
2468
a36e71f9 2469 return cfqq;
6d048f53
JA
2470}
2471
a6d44e98
CZ
2472/*
2473 * Determine whether we should enforce idle window for this queue.
2474 */
2475
2476static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2477{
3bf10fea 2478 enum wl_class_t wl_class = cfqq_class(cfqq);
34b98d03 2479 struct cfq_rb_root *st = cfqq->service_tree;
a6d44e98 2480
34b98d03
VG
2481 BUG_ON(!st);
2482 BUG_ON(!st->count);
f04a6424 2483
b6508c16
VG
2484 if (!cfqd->cfq_slice_idle)
2485 return false;
2486
a6d44e98 2487 /* We never do for idle class queues. */
3bf10fea 2488 if (wl_class == IDLE_WORKLOAD)
a6d44e98
CZ
2489 return false;
2490
2491 /* We do for queues that were marked with idle window flag. */
3c764b7a
SL
2492 if (cfq_cfqq_idle_window(cfqq) &&
2493 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
a6d44e98
CZ
2494 return true;
2495
2496 /*
2497 * Otherwise, we do only if they are the last ones
2498 * in their service tree.
2499 */
34b98d03
VG
2500 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2501 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
c1e44756 2502 return true;
34b98d03 2503 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
c1e44756 2504 return false;
a6d44e98
CZ
2505}
2506
6d048f53 2507static void cfq_arm_slice_timer(struct cfq_data *cfqd)
22e2c507 2508{
1792669c 2509 struct cfq_queue *cfqq = cfqd->active_queue;
c5869807 2510 struct cfq_io_cq *cic;
80bdf0c7 2511 unsigned long sl, group_idle = 0;
7b14e3b5 2512
a68bbddb 2513 /*
f7d7b7a7
JA
2514 * SSD device without seek penalty, disable idling. But only do so
2515 * for devices that support queuing, otherwise we still have a problem
2516 * with sync vs async workloads.
a68bbddb 2517 */
f7d7b7a7 2518 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
a68bbddb
JA
2519 return;
2520
dd67d051 2521 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f53 2522 WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507
JA
2523
2524 /*
2525 * idle is disabled, either manually or by past process history
2526 */
80bdf0c7
VG
2527 if (!cfq_should_idle(cfqd, cfqq)) {
2528 /* no queue idling. Check for group idling */
2529 if (cfqd->cfq_group_idle)
2530 group_idle = cfqd->cfq_group_idle;
2531 else
2532 return;
2533 }
6d048f53 2534
7b679138 2535 /*
8e550632 2536 * still active requests from this queue, don't idle
7b679138 2537 */
8e550632 2538 if (cfqq->dispatched)
7b679138
JA
2539 return;
2540
22e2c507
JA
2541 /*
2542 * task has exited, don't wait
2543 */
206dc69b 2544 cic = cfqd->active_cic;
f6e8d01b 2545 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
6d048f53
JA
2546 return;
2547
355b659c
CZ
2548 /*
2549 * If our average think time is larger than the remaining time
2550 * slice, then don't idle. This avoids overrunning the allotted
2551 * time slice.
2552 */
383cd721
SL
2553 if (sample_valid(cic->ttime.ttime_samples) &&
2554 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
fd16d263 2555 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
383cd721 2556 cic->ttime.ttime_mean);
355b659c 2557 return;
b1ffe737 2558 }
355b659c 2559
80bdf0c7
VG
2560 /* There are other queues in the group, don't do group idle */
2561 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2562 return;
2563
3b18152c 2564 cfq_mark_cfqq_wait_request(cfqq);
22e2c507 2565
80bdf0c7
VG
2566 if (group_idle)
2567 sl = cfqd->cfq_group_idle;
2568 else
2569 sl = cfqd->cfq_slice_idle;
206dc69b 2570
7b14e3b5 2571 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
155fead9 2572 cfqg_stats_set_start_idle_time(cfqq->cfqg);
80bdf0c7
VG
2573 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2574 group_idle ? 1 : 0);
1da177e4
LT
2575}
2576
498d3aa2
JA
2577/*
2578 * Move request from internal lists to the request queue dispatch list.
2579 */
165125e1 2580static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4 2581{
3ed9a296 2582 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 2583 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 2584
7b679138
JA
2585 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2586
06d21886 2587 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101 2588 cfq_remove_request(rq);
6d048f53 2589 cfqq->dispatched++;
80bdf0c7 2590 (RQ_CFQG(rq))->dispatched++;
5380a101 2591 elv_dispatch_sort(q, rq);
3ed9a296 2592
53c583d2 2593 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
c4e7893e 2594 cfqq->nr_sectors += blk_rq_sectors(rq);
155fead9 2595 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
1da177e4
LT
2596}
2597
2598/*
2599 * return expired entry, or NULL to just start from scratch in rbtree
2600 */
febffd61 2601static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4 2602{
30996f40 2603 struct request *rq = NULL;
1da177e4 2604
3b18152c 2605 if (cfq_cfqq_fifo_expire(cfqq))
1da177e4 2606 return NULL;
cb887411
JA
2607
2608 cfq_mark_cfqq_fifo_expire(cfqq);
2609
89850f7e
JA
2610 if (list_empty(&cfqq->fifo))
2611 return NULL;
1da177e4 2612
89850f7e 2613 rq = rq_entry_fifo(cfqq->fifo.next);
30996f40 2614 if (time_before(jiffies, rq_fifo_time(rq)))
7b679138 2615 rq = NULL;
1da177e4 2616
30996f40 2617 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
6d048f53 2618 return rq;
1da177e4
LT
2619}
2620
22e2c507
JA
2621static inline int
2622cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2623{
2624 const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4 2625
22e2c507 2626 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4 2627
b9f8ce05 2628 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
1da177e4
LT
2629}
2630
df5fe3e8
JM
2631/*
2632 * Must be called with the queue_lock held.
2633 */
2634static int cfqq_process_refs(struct cfq_queue *cfqq)
2635{
2636 int process_refs, io_refs;
2637
2638 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
30d7b944 2639 process_refs = cfqq->ref - io_refs;
df5fe3e8
JM
2640 BUG_ON(process_refs < 0);
2641 return process_refs;
2642}
2643
2644static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2645{
e6c5bc73 2646 int process_refs, new_process_refs;
df5fe3e8
JM
2647 struct cfq_queue *__cfqq;
2648
c10b61f0
JM
2649 /*
2650 * If there are no process references on the new_cfqq, then it is
2651 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2652 * chain may have dropped their last reference (not just their
2653 * last process reference).
2654 */
2655 if (!cfqq_process_refs(new_cfqq))
2656 return;
2657
df5fe3e8
JM
2658 /* Avoid a circular list and skip interim queue merges */
2659 while ((__cfqq = new_cfqq->new_cfqq)) {
2660 if (__cfqq == cfqq)
2661 return;
2662 new_cfqq = __cfqq;
2663 }
2664
2665 process_refs = cfqq_process_refs(cfqq);
c10b61f0 2666 new_process_refs = cfqq_process_refs(new_cfqq);
df5fe3e8
JM
2667 /*
2668 * If the process for the cfqq has gone away, there is no
2669 * sense in merging the queues.
2670 */
c10b61f0 2671 if (process_refs == 0 || new_process_refs == 0)
df5fe3e8
JM
2672 return;
2673
e6c5bc73
JM
2674 /*
2675 * Merge in the direction of the lesser amount of work.
2676 */
e6c5bc73
JM
2677 if (new_process_refs >= process_refs) {
2678 cfqq->new_cfqq = new_cfqq;
30d7b944 2679 new_cfqq->ref += process_refs;
e6c5bc73
JM
2680 } else {
2681 new_cfqq->new_cfqq = cfqq;
30d7b944 2682 cfqq->ref += new_process_refs;
e6c5bc73 2683 }
df5fe3e8
JM
2684}
2685
6d816ec7 2686static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3bf10fea 2687 struct cfq_group *cfqg, enum wl_class_t wl_class)
718eee05
CZ
2688{
2689 struct cfq_queue *queue;
2690 int i;
2691 bool key_valid = false;
2692 unsigned long lowest_key = 0;
2693 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2694
65b32a57
VG
2695 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2696 /* select the one with lowest rb_key */
34b98d03 2697 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
718eee05
CZ
2698 if (queue &&
2699 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2700 lowest_key = queue->rb_key;
2701 cur_best = i;
2702 key_valid = true;
2703 }
2704 }
2705
2706 return cur_best;
2707}
2708
6d816ec7
VG
2709static void
2710choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee05 2711{
718eee05
CZ
2712 unsigned slice;
2713 unsigned count;
cdb16e8f 2714 struct cfq_rb_root *st;
58ff82f3 2715 unsigned group_slice;
4d2ceea4 2716 enum wl_class_t original_class = cfqd->serving_wl_class;
1fa8f6d6 2717
718eee05 2718 /* Choose next priority. RT > BE > IDLE */
58ff82f3 2719 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
4d2ceea4 2720 cfqd->serving_wl_class = RT_WORKLOAD;
58ff82f3 2721 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
4d2ceea4 2722 cfqd->serving_wl_class = BE_WORKLOAD;
718eee05 2723 else {
4d2ceea4 2724 cfqd->serving_wl_class = IDLE_WORKLOAD;
718eee05
CZ
2725 cfqd->workload_expires = jiffies + 1;
2726 return;
2727 }
2728
4d2ceea4 2729 if (original_class != cfqd->serving_wl_class)
e4ea0c16
SL
2730 goto new_workload;
2731
718eee05
CZ
2732 /*
2733 * For RT and BE, we have to choose also the type
2734 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2735 * expiration time
2736 */
34b98d03 2737 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f 2738 count = st->count;
718eee05
CZ
2739
2740 /*
65b32a57 2741 * check workload expiration, and that we still have other queues ready
718eee05 2742 */
65b32a57 2743 if (count && !time_after(jiffies, cfqd->workload_expires))
718eee05
CZ
2744 return;
2745
e4ea0c16 2746new_workload:
718eee05 2747 /* otherwise select new workload type */
6d816ec7 2748 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
4d2ceea4 2749 cfqd->serving_wl_class);
34b98d03 2750 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f 2751 count = st->count;
718eee05
CZ
2752
2753 /*
2754 * the workload slice is computed as a fraction of target latency
2755 * proportional to the number of queues in that workload, over
2756 * all the queues in the same priority class
2757 */
58ff82f3
VG
2758 group_slice = cfq_group_slice(cfqd, cfqg);
2759
2760 slice = group_slice * count /
4d2ceea4
VG
2761 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
2762 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
3bf10fea 2763 cfqg));
718eee05 2764
4d2ceea4 2765 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
f26bd1f0
VG
2766 unsigned int tmp;
2767
2768 /*
2769 * Async queues are currently system wide. Just taking
2770 * proportion of queues with-in same group will lead to higher
2771 * async ratio system wide as generally root group is going
2772 * to have higher weight. A more accurate thing would be to
2773 * calculate system wide asnc/sync ratio.
2774 */
5bf14c07
TM
2775 tmp = cfqd->cfq_target_latency *
2776 cfqg_busy_async_queues(cfqd, cfqg);
f26bd1f0
VG
2777 tmp = tmp/cfqd->busy_queues;
2778 slice = min_t(unsigned, slice, tmp);
2779
718eee05
CZ
2780 /* async workload slice is scaled down according to
2781 * the sync/async slice ratio. */
2782 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
f26bd1f0 2783 } else
718eee05
CZ
2784 /* sync workload slice is at least 2 * cfq_slice_idle */
2785 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2786
2787 slice = max_t(unsigned, slice, CFQ_MIN_TT);
b1ffe737 2788 cfq_log(cfqd, "workload slice:%d", slice);
718eee05
CZ
2789 cfqd->workload_expires = jiffies + slice;
2790}
2791
1fa8f6d6
VG
2792static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2793{
2794 struct cfq_rb_root *st = &cfqd->grp_service_tree;
25bc6b07 2795 struct cfq_group *cfqg;
1fa8f6d6
VG
2796
2797 if (RB_EMPTY_ROOT(&st->rb))
2798 return NULL;
25bc6b07 2799 cfqg = cfq_rb_first_group(st);
25bc6b07
VG
2800 update_min_vdisktime(st);
2801 return cfqg;
1fa8f6d6
VG
2802}
2803
cdb16e8f
VG
2804static void cfq_choose_cfqg(struct cfq_data *cfqd)
2805{
1fa8f6d6
VG
2806 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2807
2808 cfqd->serving_group = cfqg;
dae739eb
VG
2809
2810 /* Restore the workload type data */
4d2ceea4
VG
2811 if (cfqg->saved_wl_slice) {
2812 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
2813 cfqd->serving_wl_type = cfqg->saved_wl_type;
2814 cfqd->serving_wl_class = cfqg->saved_wl_class;
66ae2919
GJ
2815 } else
2816 cfqd->workload_expires = jiffies - 1;
2817
6d816ec7 2818 choose_wl_class_and_type(cfqd, cfqg);
cdb16e8f
VG
2819}
2820
22e2c507 2821/*
498d3aa2
JA
2822 * Select a queue for service. If we have a current active queue,
2823 * check whether to continue servicing it, or retrieve and set a new one.
22e2c507 2824 */
1b5ed5e1 2825static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4 2826{
a36e71f9 2827 struct cfq_queue *cfqq, *new_cfqq = NULL;
1da177e4 2828
22e2c507
JA
2829 cfqq = cfqd->active_queue;
2830 if (!cfqq)
2831 goto new_queue;
1da177e4 2832
f04a6424
VG
2833 if (!cfqd->rq_queued)
2834 return NULL;
c244bb50
VG
2835
2836 /*
2837 * We were waiting for group to get backlogged. Expire the queue
2838 */
2839 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2840 goto expire;
2841
22e2c507 2842 /*
6d048f53 2843 * The active queue has run out of time, expire it and select new.
22e2c507 2844 */
7667aa06
VG
2845 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2846 /*
2847 * If slice had not expired at the completion of last request
2848 * we might not have turned on wait_busy flag. Don't expire
2849 * the queue yet. Allow the group to get backlogged.
2850 *
2851 * The very fact that we have used the slice, that means we
2852 * have been idling all along on this queue and it should be
2853 * ok to wait for this request to complete.
2854 */
82bbbf28
VG
2855 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2856 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2857 cfqq = NULL;
7667aa06 2858 goto keep_queue;
82bbbf28 2859 } else
80bdf0c7 2860 goto check_group_idle;
7667aa06 2861 }
1da177e4 2862
22e2c507 2863 /*
6d048f53
JA
2864 * The active queue has requests and isn't expired, allow it to
2865 * dispatch.
22e2c507 2866 */
dd67d051 2867 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 2868 goto keep_queue;
6d048f53 2869
a36e71f9
JA
2870 /*
2871 * If another queue has a request waiting within our mean seek
2872 * distance, let it run. The expire code will check for close
2873 * cooperators and put the close queue at the front of the service
df5fe3e8 2874 * tree. If possible, merge the expiring queue with the new cfqq.
a36e71f9 2875 */
b3b6d040 2876 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8
JM
2877 if (new_cfqq) {
2878 if (!cfqq->new_cfqq)
2879 cfq_setup_merge(cfqq, new_cfqq);
a36e71f9 2880 goto expire;
df5fe3e8 2881 }
a36e71f9 2882
6d048f53
JA
2883 /*
2884 * No requests pending. If the active queue still has requests in
2885 * flight or is idling for a new request, allow either of these
2886 * conditions to happen (or time out) before selecting a new queue.
2887 */
80bdf0c7
VG
2888 if (timer_pending(&cfqd->idle_slice_timer)) {
2889 cfqq = NULL;
2890 goto keep_queue;
2891 }
2892
8e1ac665
SL
2893 /*
2894 * This is a deep seek queue, but the device is much faster than
2895 * the queue can deliver, don't idle
2896 **/
2897 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2898 (cfq_cfqq_slice_new(cfqq) ||
2899 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2900 cfq_clear_cfqq_deep(cfqq);
2901 cfq_clear_cfqq_idle_window(cfqq);
2902 }
2903
80bdf0c7
VG
2904 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2905 cfqq = NULL;
2906 goto keep_queue;
2907 }
2908
2909 /*
2910 * If group idle is enabled and there are requests dispatched from
2911 * this group, wait for requests to complete.
2912 */
2913check_group_idle:
7700fc4f
SL
2914 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2915 cfqq->cfqg->dispatched &&
2916 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
caaa5f9f
JA
2917 cfqq = NULL;
2918 goto keep_queue;
22e2c507
JA
2919 }
2920
3b18152c 2921expire:
e5ff082e 2922 cfq_slice_expired(cfqd, 0);
3b18152c 2923new_queue:
718eee05
CZ
2924 /*
2925 * Current queue expired. Check if we have to switch to a new
2926 * service tree
2927 */
2928 if (!new_cfqq)
cdb16e8f 2929 cfq_choose_cfqg(cfqd);
718eee05 2930
a36e71f9 2931 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507 2932keep_queue:
3b18152c 2933 return cfqq;
22e2c507
JA
2934}
2935
febffd61 2936static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e
JA
2937{
2938 int dispatched = 0;
2939
2940 while (cfqq->next_rq) {
2941 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2942 dispatched++;
2943 }
2944
2945 BUG_ON(!list_empty(&cfqq->fifo));
f04a6424
VG
2946
2947 /* By default cfqq is not expired if it is empty. Do it explicitly */
e5ff082e 2948 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e
JA
2949 return dispatched;
2950}
2951
498d3aa2
JA
2952/*
2953 * Drain our current requests. Used for barriers and when switching
2954 * io schedulers on-the-fly.
2955 */
d9e7620e 2956static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1 2957{
0871714e 2958 struct cfq_queue *cfqq;
d9e7620e 2959 int dispatched = 0;
cdb16e8f 2960
3440c49f 2961 /* Expire the timeslice of the current active queue first */
e5ff082e 2962 cfq_slice_expired(cfqd, 0);
3440c49f
DS
2963 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2964 __cfq_set_active_queue(cfqd, cfqq);
f04a6424 2965 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3440c49f 2966 }
1b5ed5e1 2967
1b5ed5e1
TH
2968 BUG_ON(cfqd->busy_queues);
2969
6923715a 2970 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1
TH
2971 return dispatched;
2972}
2973
abc3c744
SL
2974static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2975 struct cfq_queue *cfqq)
2976{
2977 /* the queue hasn't finished any request, can't estimate */
2978 if (cfq_cfqq_slice_new(cfqq))
c1e44756 2979 return true;
abc3c744
SL
2980 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2981 cfqq->slice_end))
c1e44756 2982 return true;
abc3c744 2983
c1e44756 2984 return false;
abc3c744
SL
2985}
2986
0b182d61 2987static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb738 2988{
2f5cb738 2989 unsigned int max_dispatch;
22e2c507 2990
5ad531db
JA
2991 /*
2992 * Drain async requests before we start sync IO
2993 */
53c583d2 2994 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
0b182d61 2995 return false;
5ad531db 2996
2f5cb738
JA
2997 /*
2998 * If this is an async queue and we have sync IO in flight, let it wait
2999 */
53c583d2 3000 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
0b182d61 3001 return false;
2f5cb738 3002
abc3c744 3003 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
2f5cb738
JA
3004 if (cfq_class_idle(cfqq))
3005 max_dispatch = 1;
b4878f24 3006
2f5cb738
JA
3007 /*
3008 * Does this cfqq already have too much IO in flight?
3009 */
3010 if (cfqq->dispatched >= max_dispatch) {
ef8a41df 3011 bool promote_sync = false;
2f5cb738
JA
3012 /*
3013 * idle queue must always only have a single IO in flight
3014 */
3ed9a296 3015 if (cfq_class_idle(cfqq))
0b182d61 3016 return false;
3ed9a296 3017
ef8a41df 3018 /*
c4ade94f
LS
3019 * If there is only one sync queue
3020 * we can ignore async queue here and give the sync
ef8a41df
SL
3021 * queue no dispatch limit. The reason is a sync queue can
3022 * preempt async queue, limiting the sync queue doesn't make
3023 * sense. This is useful for aiostress test.
3024 */
c4ade94f
LS
3025 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3026 promote_sync = true;
ef8a41df 3027
2f5cb738
JA
3028 /*
3029 * We have other queues, don't allow more IO from this one
3030 */
ef8a41df
SL
3031 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3032 !promote_sync)
0b182d61 3033 return false;
9ede209e 3034
365722bb 3035 /*
474b18cc 3036 * Sole queue user, no limit
365722bb 3037 */
ef8a41df 3038 if (cfqd->busy_queues == 1 || promote_sync)
abc3c744
SL
3039 max_dispatch = -1;
3040 else
3041 /*
3042 * Normally we start throttling cfqq when cfq_quantum/2
3043 * requests have been dispatched. But we can drive
3044 * deeper queue depths at the beginning of slice
3045 * subjected to upper limit of cfq_quantum.
3046 * */
3047 max_dispatch = cfqd->cfq_quantum;
8e296755
JA
3048 }
3049
3050 /*
3051 * Async queues must wait a bit before being allowed dispatch.
3052 * We also ramp up the dispatch depth gradually for async IO,
3053 * based on the last sync IO we serviced
3054 */
963b72fc 3055 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
573412b2 3056 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
8e296755 3057 unsigned int depth;
365722bb 3058
61f0c1dc 3059 depth = last_sync / cfqd->cfq_slice[1];
e00c54c3
JA
3060 if (!depth && !cfqq->dispatched)
3061 depth = 1;
8e296755
JA
3062 if (depth < max_dispatch)
3063 max_dispatch = depth;
2f5cb738 3064 }
3ed9a296 3065
0b182d61
JA
3066 /*
3067 * If we're below the current max, allow a dispatch
3068 */
3069 return cfqq->dispatched < max_dispatch;
3070}
3071
3072/*
3073 * Dispatch a request from cfqq, moving them to the request queue
3074 * dispatch list.
3075 */
3076static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3077{
3078 struct request *rq;
3079
3080 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3081
3082 if (!cfq_may_dispatch(cfqd, cfqq))
3083 return false;
3084
3085 /*
3086 * follow expired path, else get first next available
3087 */
3088 rq = cfq_check_fifo(cfqq);
3089 if (!rq)
3090 rq = cfqq->next_rq;
3091
3092 /*
3093 * insert request into driver dispatch list
3094 */
3095 cfq_dispatch_insert(cfqd->queue, rq);
3096
3097 if (!cfqd->active_cic) {
c5869807 3098 struct cfq_io_cq *cic = RQ_CIC(rq);
0b182d61 3099
c5869807 3100 atomic_long_inc(&cic->icq.ioc->refcount);
0b182d61
JA
3101 cfqd->active_cic = cic;
3102 }
3103
3104 return true;
3105}
3106
3107/*
3108 * Find the cfqq that we need to service and move a request from that to the
3109 * dispatch list
3110 */
3111static int cfq_dispatch_requests(struct request_queue *q, int force)
3112{
3113 struct cfq_data *cfqd = q->elevator->elevator_data;
3114 struct cfq_queue *cfqq;
3115
3116 if (!cfqd->busy_queues)
3117 return 0;
3118
3119 if (unlikely(force))
3120 return cfq_forced_dispatch(cfqd);
3121
3122 cfqq = cfq_select_queue(cfqd);
3123 if (!cfqq)
8e296755
JA
3124 return 0;
3125
2f5cb738 3126 /*
0b182d61 3127 * Dispatch a request from this cfqq, if it is allowed
2f5cb738 3128 */
0b182d61
JA
3129 if (!cfq_dispatch_request(cfqd, cfqq))
3130 return 0;
3131
2f5cb738 3132 cfqq->slice_dispatch++;
b029195d 3133 cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507 3134
2f5cb738
JA
3135 /*
3136 * expire an async queue immediately if it has used up its slice. idle
3137 * queue always expire after 1 dispatch round.
3138 */
3139 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3140 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3141 cfq_class_idle(cfqq))) {
3142 cfqq->slice_end = jiffies + 1;
e5ff082e 3143 cfq_slice_expired(cfqd, 0);
1da177e4
LT
3144 }
3145
b217a903 3146 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb738 3147 return 1;
1da177e4
LT
3148}
3149
1da177e4 3150/*
5e705374
JA
3151 * task holds one reference to the queue, dropped when task exits. each rq
3152 * in-flight on this queue also holds a reference, dropped when rq is freed.
1da177e4 3153 *
b1c35769 3154 * Each cfq queue took a reference on the parent group. Drop it now.
1da177e4
LT
3155 * queue lock must be held here.
3156 */
3157static void cfq_put_queue(struct cfq_queue *cfqq)
3158{
22e2c507 3159 struct cfq_data *cfqd = cfqq->cfqd;
0bbfeb83 3160 struct cfq_group *cfqg;
22e2c507 3161
30d7b944 3162 BUG_ON(cfqq->ref <= 0);
1da177e4 3163
30d7b944
SL
3164 cfqq->ref--;
3165 if (cfqq->ref)
1da177e4
LT
3166 return;
3167
7b679138 3168 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4 3169 BUG_ON(rb_first(&cfqq->sort_list));
22e2c507 3170 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
b1c35769 3171 cfqg = cfqq->cfqg;
1da177e4 3172
28f95cbc 3173 if (unlikely(cfqd->active_queue == cfqq)) {
e5ff082e 3174 __cfq_slice_expired(cfqd, cfqq, 0);
23e018a1 3175 cfq_schedule_dispatch(cfqd);
28f95cbc 3176 }
22e2c507 3177
f04a6424 3178 BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4 3179 kmem_cache_free(cfq_pool, cfqq);
eb7d8c07 3180 cfqg_put(cfqg);
1da177e4
LT
3181}
3182
d02a2c07 3183static void cfq_put_cooperator(struct cfq_queue *cfqq)
1da177e4 3184{
df5fe3e8
JM
3185 struct cfq_queue *__cfqq, *next;
3186
df5fe3e8
JM
3187 /*
3188 * If this queue was scheduled to merge with another queue, be
3189 * sure to drop the reference taken on that queue (and others in
3190 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3191 */
3192 __cfqq = cfqq->new_cfqq;
3193 while (__cfqq) {
3194 if (__cfqq == cfqq) {
3195 WARN(1, "cfqq->new_cfqq loop detected\n");
3196 break;
3197 }
3198 next = __cfqq->new_cfqq;
3199 cfq_put_queue(__cfqq);
3200 __cfqq = next;
3201 }
d02a2c07
SL
3202}
3203
3204static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3205{
3206 if (unlikely(cfqq == cfqd->active_queue)) {
3207 __cfq_slice_expired(cfqd, cfqq, 0);
3208 cfq_schedule_dispatch(cfqd);
3209 }
3210
3211 cfq_put_cooperator(cfqq);
df5fe3e8 3212
89850f7e
JA
3213 cfq_put_queue(cfqq);
3214}
22e2c507 3215
9b84cacd
TH
3216static void cfq_init_icq(struct io_cq *icq)
3217{
3218 struct cfq_io_cq *cic = icq_to_cic(icq);
3219
3220 cic->ttime.last_end_request = jiffies;
3221}
3222
c5869807 3223static void cfq_exit_icq(struct io_cq *icq)
89850f7e 3224{
c5869807 3225 struct cfq_io_cq *cic = icq_to_cic(icq);
283287a5 3226 struct cfq_data *cfqd = cic_to_cfqd(cic);
4faa3c81 3227
ff6657c6
JA
3228 if (cic->cfqq[BLK_RW_ASYNC]) {
3229 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3230 cic->cfqq[BLK_RW_ASYNC] = NULL;
12a05732
AV
3231 }
3232
ff6657c6
JA
3233 if (cic->cfqq[BLK_RW_SYNC]) {
3234 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3235 cic->cfqq[BLK_RW_SYNC] = NULL;
12a05732 3236 }
89850f7e
JA
3237}
3238
abede6da 3239static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
22e2c507
JA
3240{
3241 struct task_struct *tsk = current;
3242 int ioprio_class;
3243
3b18152c 3244 if (!cfq_cfqq_prio_changed(cfqq))
22e2c507
JA
3245 return;
3246
598971bf 3247 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
22e2c507 3248 switch (ioprio_class) {
fe094d98
JA
3249 default:
3250 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3251 case IOPRIO_CLASS_NONE:
3252 /*
6d63c275 3253 * no prio set, inherit CPU scheduling settings
fe094d98
JA
3254 */
3255 cfqq->ioprio = task_nice_ioprio(tsk);
6d63c275 3256 cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98
JA
3257 break;
3258 case IOPRIO_CLASS_RT:
598971bf 3259 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98
JA
3260 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3261 break;
3262 case IOPRIO_CLASS_BE:
598971bf 3263 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98
JA
3264 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3265 break;
3266 case IOPRIO_CLASS_IDLE:
3267 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3268 cfqq->ioprio = 7;
3269 cfq_clear_cfqq_idle_window(cfqq);
3270 break;
22e2c507
JA
3271 }
3272
3273 /*
3274 * keep track of original prio settings in case we have to temporarily
3275 * elevate the priority of this queue
3276 */
3277 cfqq->org_ioprio = cfqq->ioprio;
3b18152c 3278 cfq_clear_cfqq_prio_changed(cfqq);
22e2c507
JA
3279}
3280
598971bf 3281static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
22e2c507 3282{
598971bf 3283 int ioprio = cic->icq.ioc->ioprio;
bca4b914 3284 struct cfq_data *cfqd = cic_to_cfqd(cic);
478a82b0 3285 struct cfq_queue *cfqq;
35e6077c 3286
598971bf
TH
3287 /*
3288 * Check whether ioprio has changed. The condition may trigger
3289 * spuriously on a newly created cic but there's no harm.
3290 */
3291 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
caaa5f9f
JA
3292 return;
3293
ff6657c6 3294 cfqq = cic->cfqq[BLK_RW_ASYNC];
caaa5f9f
JA
3295 if (cfqq) {
3296 struct cfq_queue *new_cfqq;
abede6da
TH
3297 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3298 GFP_ATOMIC);
caaa5f9f 3299 if (new_cfqq) {
ff6657c6 3300 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
caaa5f9f
JA
3301 cfq_put_queue(cfqq);
3302 }
22e2c507 3303 }
caaa5f9f 3304
ff6657c6 3305 cfqq = cic->cfqq[BLK_RW_SYNC];
caaa5f9f
JA
3306 if (cfqq)
3307 cfq_mark_cfqq_prio_changed(cfqq);
598971bf
TH
3308
3309 cic->ioprio = ioprio;
22e2c507
JA
3310}
3311
d5036d77 3312static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 3313 pid_t pid, bool is_sync)
d5036d77
JA
3314{
3315 RB_CLEAR_NODE(&cfqq->rb_node);
3316 RB_CLEAR_NODE(&cfqq->p_node);
3317 INIT_LIST_HEAD(&cfqq->fifo);
3318
30d7b944 3319 cfqq->ref = 0;
d5036d77
JA
3320 cfqq->cfqd = cfqd;
3321
3322 cfq_mark_cfqq_prio_changed(cfqq);
3323
3324 if (is_sync) {
3325 if (!cfq_class_idle(cfqq))
3326 cfq_mark_cfqq_idle_window(cfqq);
3327 cfq_mark_cfqq_sync(cfqq);
3328 }
3329 cfqq->pid = pid;
3330}
3331
24610333 3332#ifdef CONFIG_CFQ_GROUP_IOSCHED
598971bf 3333static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
24610333 3334{
bca4b914 3335 struct cfq_data *cfqd = cic_to_cfqd(cic);
598971bf
TH
3336 struct cfq_queue *sync_cfqq;
3337 uint64_t id;
24610333 3338
598971bf 3339 rcu_read_lock();
3c798398 3340 id = bio_blkcg(bio)->id;
598971bf 3341 rcu_read_unlock();
24610333 3342
598971bf
TH
3343 /*
3344 * Check whether blkcg has changed. The condition may trigger
3345 * spuriously on a newly created cic but there's no harm.
3346 */
3347 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
3348 return;
24610333 3349
598971bf 3350 sync_cfqq = cic_to_cfqq(cic, 1);
24610333
VG
3351 if (sync_cfqq) {
3352 /*
3353 * Drop reference to sync queue. A new sync queue will be
3354 * assigned in new group upon arrival of a fresh request.
3355 */
3356 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3357 cic_set_cfqq(cic, NULL, 1);
3358 cfq_put_queue(sync_cfqq);
3359 }
598971bf
TH
3360
3361 cic->blkcg_id = id;
24610333 3362}
598971bf
TH
3363#else
3364static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
24610333
VG
3365#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3366
22e2c507 3367static struct cfq_queue *
abede6da
TH
3368cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3369 struct bio *bio, gfp_t gfp_mask)
22e2c507 3370{
3c798398 3371 struct blkcg *blkcg;
22e2c507 3372 struct cfq_queue *cfqq, *new_cfqq = NULL;
cdb16e8f 3373 struct cfq_group *cfqg;
22e2c507
JA
3374
3375retry:
2a7f1244
TH
3376 rcu_read_lock();
3377
3c798398 3378 blkcg = bio_blkcg(bio);
cd1604fa 3379 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
91fac317 3380 cfqq = cic_to_cfqq(cic, is_sync);
22e2c507 3381
6118b70b
JA
3382 /*
3383 * Always try a new alloc if we fell back to the OOM cfqq
3384 * originally, since it should just be a temporary situation.
3385 */
3386 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3387 cfqq = NULL;
22e2c507
JA
3388 if (new_cfqq) {
3389 cfqq = new_cfqq;
3390 new_cfqq = NULL;
3391 } else if (gfp_mask & __GFP_WAIT) {
2a7f1244 3392 rcu_read_unlock();
22e2c507 3393 spin_unlock_irq(cfqd->queue->queue_lock);
94f6030c 3394 new_cfqq = kmem_cache_alloc_node(cfq_pool,
6118b70b 3395 gfp_mask | __GFP_ZERO,
94f6030c 3396 cfqd->queue->node);
22e2c507 3397 spin_lock_irq(cfqd->queue->queue_lock);
6118b70b
JA
3398 if (new_cfqq)
3399 goto retry;
22e2c507 3400 } else {
94f6030c
CL
3401 cfqq = kmem_cache_alloc_node(cfq_pool,
3402 gfp_mask | __GFP_ZERO,
3403 cfqd->queue->node);
22e2c507
JA
3404 }
3405
6118b70b
JA
3406 if (cfqq) {
3407 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
abede6da 3408 cfq_init_prio_data(cfqq, cic);
cdb16e8f 3409 cfq_link_cfqq_cfqg(cfqq, cfqg);
6118b70b
JA
3410 cfq_log_cfqq(cfqd, cfqq, "alloced");
3411 } else
3412 cfqq = &cfqd->oom_cfqq;
22e2c507
JA
3413 }
3414
3415 if (new_cfqq)
3416 kmem_cache_free(cfq_pool, new_cfqq);
3417
2a7f1244 3418 rcu_read_unlock();
22e2c507
JA
3419 return cfqq;
3420}
3421
c2dea2d1
VT
3422static struct cfq_queue **
3423cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3424{
fe094d98 3425 switch (ioprio_class) {
c2dea2d1
VT
3426 case IOPRIO_CLASS_RT:
3427 return &cfqd->async_cfqq[0][ioprio];
598971bf
TH
3428 case IOPRIO_CLASS_NONE:
3429 ioprio = IOPRIO_NORM;
3430 /* fall through */
c2dea2d1
VT
3431 case IOPRIO_CLASS_BE:
3432 return &cfqd->async_cfqq[1][ioprio];
3433 case IOPRIO_CLASS_IDLE:
3434 return &cfqd->async_idle_cfqq;
3435 default:
3436 BUG();
3437 }
3438}
3439
15c31be4 3440static struct cfq_queue *
abede6da 3441cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
4f85cb96 3442 struct bio *bio, gfp_t gfp_mask)
15c31be4 3443{
598971bf
TH
3444 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3445 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
c2dea2d1 3446 struct cfq_queue **async_cfqq = NULL;
15c31be4
JA
3447 struct cfq_queue *cfqq = NULL;
3448
c2dea2d1
VT
3449 if (!is_sync) {
3450 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3451 cfqq = *async_cfqq;
3452 }
3453
6118b70b 3454 if (!cfqq)
abede6da 3455 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
15c31be4
JA
3456
3457 /*
3458 * pin the queue now that it's allocated, scheduler exit will prune it
3459 */
c2dea2d1 3460 if (!is_sync && !(*async_cfqq)) {
30d7b944 3461 cfqq->ref++;
c2dea2d1 3462 *async_cfqq = cfqq;
15c31be4
JA
3463 }
3464
30d7b944 3465 cfqq->ref++;
15c31be4
JA
3466 return cfqq;
3467}
3468
22e2c507 3469static void
383cd721 3470__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
1da177e4 3471{
383cd721
SL
3472 unsigned long elapsed = jiffies - ttime->last_end_request;
3473 elapsed = min(elapsed, 2UL * slice_idle);
db3b5848 3474
383cd721
SL
3475 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3476 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3477 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3478}
3479
3480static void
3481cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c5869807 3482 struct cfq_io_cq *cic)
383cd721 3483{
f5f2b6ce 3484 if (cfq_cfqq_sync(cfqq)) {
383cd721 3485 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
f5f2b6ce
SL
3486 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3487 cfqd->cfq_slice_idle);
3488 }
7700fc4f
SL
3489#ifdef CONFIG_CFQ_GROUP_IOSCHED
3490 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3491#endif
22e2c507 3492}
1da177e4 3493
206dc69b 3494static void
b2c18e1e 3495cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f53 3496 struct request *rq)
206dc69b 3497{
3dde36dd 3498 sector_t sdist = 0;
41647e7a 3499 sector_t n_sec = blk_rq_sectors(rq);
3dde36dd
CZ
3500 if (cfqq->last_request_pos) {
3501 if (cfqq->last_request_pos < blk_rq_pos(rq))
3502 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3503 else
3504 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3505 }
206dc69b 3506
3dde36dd 3507 cfqq->seek_history <<= 1;
41647e7a
CZ
3508 if (blk_queue_nonrot(cfqd->queue))
3509 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3510 else
3511 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
206dc69b 3512}
1da177e4 3513
22e2c507
JA
3514/*
3515 * Disable idle window if the process thinks too long or seeks so much that
3516 * it doesn't matter
3517 */
3518static void
3519cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c5869807 3520 struct cfq_io_cq *cic)
22e2c507 3521{
7b679138 3522 int old_idle, enable_idle;
1be92f2f 3523
0871714e
JA
3524 /*
3525 * Don't idle for async or idle io prio class
3526 */
3527 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
1be92f2f
JA
3528 return;
3529
c265a7f4 3530 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4 3531
76280aff
CZ
3532 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3533 cfq_mark_cfqq_deep(cfqq);
3534
749ef9f8
CZ
3535 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3536 enable_idle = 0;
f6e8d01b 3537 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
c5869807
TH
3538 !cfqd->cfq_slice_idle ||
3539 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
22e2c507 3540 enable_idle = 0;
383cd721
SL
3541 else if (sample_valid(cic->ttime.ttime_samples)) {
3542 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
22e2c507
JA
3543 enable_idle = 0;
3544 else
3545 enable_idle = 1;
1da177e4
LT
3546 }
3547
7b679138
JA
3548 if (old_idle != enable_idle) {
3549 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3550 if (enable_idle)
3551 cfq_mark_cfqq_idle_window(cfqq);
3552 else
3553 cfq_clear_cfqq_idle_window(cfqq);
3554 }
22e2c507 3555}
1da177e4 3556
22e2c507
JA
3557/*
3558 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3559 * no or if we aren't sure, a 1 will cause a preempt.
3560 */
a6151c3a 3561static bool
22e2c507 3562cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e705374 3563 struct request *rq)
22e2c507 3564{
6d048f53 3565 struct cfq_queue *cfqq;
22e2c507 3566
6d048f53
JA
3567 cfqq = cfqd->active_queue;
3568 if (!cfqq)
a6151c3a 3569 return false;
22e2c507 3570
6d048f53 3571 if (cfq_class_idle(new_cfqq))
a6151c3a 3572 return false;
22e2c507
JA
3573
3574 if (cfq_class_idle(cfqq))
a6151c3a 3575 return true;
1e3335de 3576
875feb63
DS
3577 /*
3578 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3579 */
3580 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3581 return false;
3582
374f84ac
JA
3583 /*
3584 * if the new request is sync, but the currently running queue is
3585 * not, let the sync request have priority.
3586 */
5e705374 3587 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
a6151c3a 3588 return true;
1e3335de 3589
8682e1f1
VG
3590 if (new_cfqq->cfqg != cfqq->cfqg)
3591 return false;
3592
3593 if (cfq_slice_used(cfqq))
3594 return true;
3595
3596 /* Allow preemption only if we are idling on sync-noidle tree */
4d2ceea4 3597 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
8682e1f1
VG
3598 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3599 new_cfqq->service_tree->count == 2 &&
3600 RB_EMPTY_ROOT(&cfqq->sort_list))
3601 return true;
3602
b53d1ed7
JA
3603 /*
3604 * So both queues are sync. Let the new request get disk time if
3605 * it's a metadata request and the current queue is doing regular IO.
3606 */
65299a3b 3607 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
b53d1ed7
JA
3608 return true;
3609
3a9a3f6c
DS
3610 /*
3611 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3612 */
3613 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
a6151c3a 3614 return true;
3a9a3f6c 3615
d2d59e18
SL
3616 /* An idle queue should not be idle now for some reason */
3617 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3618 return true;
3619
1e3335de 3620 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a 3621 return false;
1e3335de
JA
3622
3623 /*
3624 * if this request is as-good as one we would expect from the
3625 * current cfqq, let it preempt
3626 */
e9ce335d 3627 if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a 3628 return true;
1e3335de 3629
a6151c3a 3630 return false;
22e2c507
JA
3631}
3632
3633/*
3634 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3635 * let it have half of its nominal slice.
3636 */
3637static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3638{
df0793ab
SL
3639 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3640
7b679138 3641 cfq_log_cfqq(cfqd, cfqq, "preempt");
df0793ab 3642 cfq_slice_expired(cfqd, 1);
22e2c507 3643
f8ae6e3e
SL
3644 /*
3645 * workload type is changed, don't save slice, otherwise preempt
3646 * doesn't happen
3647 */
df0793ab 3648 if (old_type != cfqq_type(cfqq))
4d2ceea4 3649 cfqq->cfqg->saved_wl_slice = 0;
f8ae6e3e 3650
bf572256
JA
3651 /*
3652 * Put the new queue at the front of the of the current list,
3653 * so we know that it will be selected next.
3654 */
3655 BUG_ON(!cfq_cfqq_on_rr(cfqq));
edd75ffd
JA
3656
3657 cfq_service_tree_add(cfqd, cfqq, 1);
eda5e0c9 3658
62a37f6b
JT
3659 cfqq->slice_end = 0;
3660 cfq_mark_cfqq_slice_new(cfqq);
22e2c507
JA
3661}
3662
22e2c507 3663/*
5e705374 3664 * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507
JA
3665 * something we should do about it
3666 */
3667static void
5e705374
JA
3668cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3669 struct request *rq)
22e2c507 3670{
c5869807 3671 struct cfq_io_cq *cic = RQ_CIC(rq);
12e9fddd 3672
45333d5a 3673 cfqd->rq_queued++;
65299a3b
CH
3674 if (rq->cmd_flags & REQ_PRIO)
3675 cfqq->prio_pending++;
374f84ac 3676
383cd721 3677 cfq_update_io_thinktime(cfqd, cfqq, cic);
b2c18e1e 3678 cfq_update_io_seektime(cfqd, cfqq, rq);
9c2c38a1
JA
3679 cfq_update_idle_window(cfqd, cfqq, cic);
3680
b2c18e1e 3681 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
22e2c507
JA
3682
3683 if (cfqq == cfqd->active_queue) {
3684 /*
b029195d
JA
3685 * Remember that we saw a request from this process, but
3686 * don't start queuing just yet. Otherwise we risk seeing lots
3687 * of tiny requests, because we disrupt the normal plugging
d6ceb25e
JA
3688 * and merging. If the request is already larger than a single
3689 * page, let it rip immediately. For that case we assume that
2d870722
JA
3690 * merging is already done. Ditto for a busy system that
3691 * has other work pending, don't risk delaying until the
3692 * idle timer unplug to continue working.
22e2c507 3693 */
d6ceb25e 3694 if (cfq_cfqq_wait_request(cfqq)) {
2d870722
JA
3695 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3696 cfqd->busy_queues > 1) {
812df48d 3697 cfq_del_timer(cfqd, cfqq);
554554f6 3698 cfq_clear_cfqq_wait_request(cfqq);
24ecfbe2 3699 __blk_run_queue(cfqd->queue);
a11cdaa7 3700 } else {
155fead9 3701 cfqg_stats_update_idle_time(cfqq->cfqg);
bf791937 3702 cfq_mark_cfqq_must_dispatch(cfqq);
a11cdaa7 3703 }
d6ceb25e 3704 }
5e705374 3705 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507
JA
3706 /*
3707 * not the active queue - expire current slice if it is
3708 * idle and has expired it's mean thinktime or this new queue
3a9a3f6c
DS
3709 * has some old slice time left and is of higher priority or
3710 * this new queue is RT and the current one is BE
22e2c507
JA
3711 */
3712 cfq_preempt_queue(cfqd, cfqq);
24ecfbe2 3713 __blk_run_queue(cfqd->queue);
22e2c507 3714 }
1da177e4
LT
3715}
3716
165125e1 3717static void cfq_insert_request(struct request_queue *q, struct request *rq)
1da177e4 3718{
b4878f24 3719 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 3720 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 3721
7b679138 3722 cfq_log_cfqq(cfqd, cfqq, "insert_request");
abede6da 3723 cfq_init_prio_data(cfqq, RQ_CIC(rq));
1da177e4 3724
30996f40 3725 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
22e2c507 3726 list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3d 3727 cfq_add_rq_rb(rq);
155fead9
TH
3728 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
3729 rq->cmd_flags);
5e705374 3730 cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4
LT
3731}
3732
45333d5a
AC
3733/*
3734 * Update hw_tag based on peak queue depth over 50 samples under
3735 * sufficient load.
3736 */
3737static void cfq_update_hw_tag(struct cfq_data *cfqd)
3738{
1a1238a7
SL
3739 struct cfq_queue *cfqq = cfqd->active_queue;
3740
53c583d2
CZ
3741 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3742 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
e459dd08
CZ
3743
3744 if (cfqd->hw_tag == 1)
3745 return;
45333d5a
AC
3746
3747 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
53c583d2 3748 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
45333d5a
AC
3749 return;
3750
1a1238a7
SL
3751 /*
3752 * If active queue hasn't enough requests and can idle, cfq might not
3753 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3754 * case
3755 */
3756 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3757 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
53c583d2 3758 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
1a1238a7
SL
3759 return;
3760
45333d5a
AC
3761 if (cfqd->hw_tag_samples++ < 50)
3762 return;
3763
e459dd08 3764 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a
AC
3765 cfqd->hw_tag = 1;
3766 else
3767 cfqd->hw_tag = 0;
45333d5a
AC
3768}
3769
7667aa06
VG
3770static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3771{
c5869807 3772 struct cfq_io_cq *cic = cfqd->active_cic;
7667aa06 3773
02a8f01b
JT
3774 /* If the queue already has requests, don't wait */
3775 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3776 return false;
3777
7667aa06
VG
3778 /* If there are other queues in the group, don't wait */
3779 if (cfqq->cfqg->nr_cfqq > 1)
3780 return false;
3781
7700fc4f
SL
3782 /* the only queue in the group, but think time is big */
3783 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3784 return false;
3785
7667aa06
VG
3786 if (cfq_slice_used(cfqq))
3787 return true;
3788
3789 /* if slice left is less than think time, wait busy */
383cd721
SL
3790 if (cic && sample_valid(cic->ttime.ttime_samples)
3791 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
7667aa06
VG
3792 return true;
3793
3794 /*
3795 * If think times is less than a jiffy than ttime_mean=0 and above
3796 * will not be true. It might happen that slice has not expired yet
3797 * but will expire soon (4-5 ns) during select_queue(). To cover the
3798 * case where think time is less than a jiffy, mark the queue wait
3799 * busy if only 1 jiffy is left in the slice.
3800 */
3801 if (cfqq->slice_end - jiffies == 1)
3802 return true;
3803
3804 return false;
3805}
3806
165125e1 3807static void cfq_completed_request(struct request_queue *q, struct request *rq)
1da177e4 3808{
5e705374 3809 struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f24 3810 struct cfq_data *cfqd = cfqq->cfqd;
5380a101 3811 const int sync = rq_is_sync(rq);
b4878f24 3812 unsigned long now;
1da177e4 3813
b4878f24 3814 now = jiffies;
33659ebb
CH
3815 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3816 !!(rq->cmd_flags & REQ_NOIDLE));
1da177e4 3817
45333d5a
AC
3818 cfq_update_hw_tag(cfqd);
3819
53c583d2 3820 WARN_ON(!cfqd->rq_in_driver);
6d048f53 3821 WARN_ON(!cfqq->dispatched);
53c583d2 3822 cfqd->rq_in_driver--;
6d048f53 3823 cfqq->dispatched--;
80bdf0c7 3824 (RQ_CFQG(rq))->dispatched--;
155fead9
TH
3825 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
3826 rq_io_start_time_ns(rq), rq->cmd_flags);
1da177e4 3827
53c583d2 3828 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
3ed9a296 3829
365722bb 3830 if (sync) {
34b98d03 3831 struct cfq_rb_root *st;
f5f2b6ce 3832
383cd721 3833 RQ_CIC(rq)->ttime.last_end_request = now;
f5f2b6ce
SL
3834
3835 if (cfq_cfqq_on_rr(cfqq))
34b98d03 3836 st = cfqq->service_tree;
f5f2b6ce 3837 else
34b98d03
VG
3838 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
3839 cfqq_type(cfqq));
3840
3841 st->ttime.last_end_request = now;
573412b2
CZ
3842 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3843 cfqd->last_delayed_sync = now;
365722bb 3844 }
caaa5f9f 3845
7700fc4f
SL
3846#ifdef CONFIG_CFQ_GROUP_IOSCHED
3847 cfqq->cfqg->ttime.last_end_request = now;
3848#endif
3849
caaa5f9f
JA
3850 /*
3851 * If this is the active queue, check if it needs to be expired,
3852 * or if we want to idle in case it has no pending requests.
3853 */
3854 if (cfqd->active_queue == cfqq) {
a36e71f9
JA
3855 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3856
44f7c160
JA
3857 if (cfq_cfqq_slice_new(cfqq)) {
3858 cfq_set_prio_slice(cfqd, cfqq);
3859 cfq_clear_cfqq_slice_new(cfqq);
3860 }
f75edf2d
VG
3861
3862 /*
7667aa06
VG
3863 * Should we wait for next request to come in before we expire
3864 * the queue.
f75edf2d 3865 */
7667aa06 3866 if (cfq_should_wait_busy(cfqd, cfqq)) {
80bdf0c7
VG
3867 unsigned long extend_sl = cfqd->cfq_slice_idle;
3868 if (!cfqd->cfq_slice_idle)
3869 extend_sl = cfqd->cfq_group_idle;
3870 cfqq->slice_end = jiffies + extend_sl;
f75edf2d 3871 cfq_mark_cfqq_wait_busy(cfqq);
b1ffe737 3872 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
f75edf2d
VG
3873 }
3874
a36e71f9 3875 /*
8e550632
CZ
3876 * Idling is not enabled on:
3877 * - expired queues
3878 * - idle-priority queues
3879 * - async queues
3880 * - queues with still some requests queued
3881 * - when there is a close cooperator
a36e71f9 3882 */
0871714e 3883 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
e5ff082e 3884 cfq_slice_expired(cfqd, 1);
8e550632
CZ
3885 else if (sync && cfqq_empty &&
3886 !cfq_close_cooperator(cfqd, cfqq)) {
749ef9f8 3887 cfq_arm_slice_timer(cfqd);
8e550632 3888 }
caaa5f9f 3889 }
6d048f53 3890
53c583d2 3891 if (!cfqd->rq_in_driver)
23e018a1 3892 cfq_schedule_dispatch(cfqd);
1da177e4
LT
3893}
3894
89850f7e 3895static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507 3896{
1b379d8d 3897 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c 3898 cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507 3899 return ELV_MQUEUE_MUST;
3b18152c 3900 }
1da177e4 3901
22e2c507 3902 return ELV_MQUEUE_MAY;
22e2c507
JA
3903}
3904
165125e1 3905static int cfq_may_queue(struct request_queue *q, int rw)
22e2c507
JA
3906{
3907 struct cfq_data *cfqd = q->elevator->elevator_data;
3908 struct task_struct *tsk = current;
c5869807 3909 struct cfq_io_cq *cic;
22e2c507
JA
3910 struct cfq_queue *cfqq;
3911
3912 /*
3913 * don't force setup of a queue from here, as a call to may_queue
3914 * does not necessarily imply that a request actually will be queued.
3915 * so just lookup a possibly existing queue, or return 'may queue'
3916 * if that fails
3917 */
4ac845a2 3918 cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317
VT
3919 if (!cic)
3920 return ELV_MQUEUE_MAY;
3921
b0b78f81 3922 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
22e2c507 3923 if (cfqq) {
abede6da 3924 cfq_init_prio_data(cfqq, cic);
22e2c507 3925
89850f7e 3926 return __cfq_may_queue(cfqq);
22e2c507
JA
3927 }
3928
3929 return ELV_MQUEUE_MAY;
1da177e4
LT
3930}
3931
1da177e4
LT
3932/*
3933 * queue lock held here
3934 */
bb37b94c 3935static void cfq_put_request(struct request *rq)
1da177e4 3936{
5e705374 3937 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 3938
5e705374 3939 if (cfqq) {
22e2c507 3940 const int rw = rq_data_dir(rq);
1da177e4 3941
22e2c507
JA
3942 BUG_ON(!cfqq->allocated[rw]);
3943 cfqq->allocated[rw]--;
1da177e4 3944
7f1dc8a2 3945 /* Put down rq reference on cfqg */
eb7d8c07 3946 cfqg_put(RQ_CFQG(rq));
a612fddf
TH
3947 rq->elv.priv[0] = NULL;
3948 rq->elv.priv[1] = NULL;
7f1dc8a2 3949
1da177e4
LT
3950 cfq_put_queue(cfqq);
3951 }
3952}
3953
df5fe3e8 3954static struct cfq_queue *
c5869807 3955cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
df5fe3e8
JM
3956 struct cfq_queue *cfqq)
3957{
3958 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3959 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
b3b6d040 3960 cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8
JM
3961 cfq_put_queue(cfqq);
3962 return cic_to_cfqq(cic, 1);
3963}
3964
e6c5bc73
JM
3965/*
3966 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3967 * was the last process referring to said cfqq.
3968 */
3969static struct cfq_queue *
c5869807 3970split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
e6c5bc73
JM
3971{
3972 if (cfqq_process_refs(cfqq) == 1) {
e6c5bc73
JM
3973 cfqq->pid = current->pid;
3974 cfq_clear_cfqq_coop(cfqq);
ae54abed 3975 cfq_clear_cfqq_split_coop(cfqq);
e6c5bc73
JM
3976 return cfqq;
3977 }
3978
3979 cic_set_cfqq(cic, NULL, 1);
d02a2c07
SL
3980
3981 cfq_put_cooperator(cfqq);
3982
e6c5bc73
JM
3983 cfq_put_queue(cfqq);
3984 return NULL;
3985}
1da177e4 3986/*
22e2c507 3987 * Allocate cfq data structures associated with this request.
1da177e4 3988 */
22e2c507 3989static int
852c788f
TH
3990cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
3991 gfp_t gfp_mask)
1da177e4
LT
3992{
3993 struct cfq_data *cfqd = q->elevator->elevator_data;
f1f8cc94 3994 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
1da177e4 3995 const int rw = rq_data_dir(rq);
a6151c3a 3996 const bool is_sync = rq_is_sync(rq);
22e2c507 3997 struct cfq_queue *cfqq;
1da177e4
LT
3998
3999 might_sleep_if(gfp_mask & __GFP_WAIT);
4000
216284c3 4001 spin_lock_irq(q->queue_lock);
f1f8cc94 4002
598971bf
TH
4003 check_ioprio_changed(cic, bio);
4004 check_blkcg_changed(cic, bio);
e6c5bc73 4005new_queue:
91fac317 4006 cfqq = cic_to_cfqq(cic, is_sync);
32f2e807 4007 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
abede6da 4008 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
91fac317 4009 cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8 4010 } else {
e6c5bc73
JM
4011 /*
4012 * If the queue was seeky for too long, break it apart.
4013 */
ae54abed 4014 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
e6c5bc73
JM
4015 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4016 cfqq = split_cfqq(cic, cfqq);
4017 if (!cfqq)
4018 goto new_queue;
4019 }
4020
df5fe3e8
JM
4021 /*
4022 * Check to see if this queue is scheduled to merge with
4023 * another, closely cooperating queue. The merging of
4024 * queues happens here as it must be done in process context.
4025 * The reference on new_cfqq was taken in merge_cfqqs.
4026 */
4027 if (cfqq->new_cfqq)
4028 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
91fac317 4029 }
1da177e4
LT
4030
4031 cfqq->allocated[rw]++;
1da177e4 4032
6fae9c25 4033 cfqq->ref++;
eb7d8c07 4034 cfqg_get(cfqq->cfqg);
a612fddf 4035 rq->elv.priv[0] = cfqq;
1adaf3dd 4036 rq->elv.priv[1] = cfqq->cfqg;
216284c3 4037 spin_unlock_irq(q->queue_lock);
5e705374 4038 return 0;
1da177e4
LT
4039}
4040
65f27f38 4041static void cfq_kick_queue(struct work_struct *work)
22e2c507 4042{
65f27f38 4043 struct cfq_data *cfqd =
23e018a1 4044 container_of(work, struct cfq_data, unplug_work);
165125e1 4045 struct request_queue *q = cfqd->queue;
22e2c507 4046
40bb54d1 4047 spin_lock_irq(q->queue_lock);
24ecfbe2 4048 __blk_run_queue(cfqd->queue);
40bb54d1 4049 spin_unlock_irq(q->queue_lock);
22e2c507
JA
4050}
4051
4052/*
4053 * Timer running if the active_queue is currently idling inside its time slice
4054 */
4055static void cfq_idle_slice_timer(unsigned long data)
4056{
4057 struct cfq_data *cfqd = (struct cfq_data *) data;
4058 struct cfq_queue *cfqq;
4059 unsigned long flags;
3c6bd2f8 4060 int timed_out = 1;
22e2c507 4061
7b679138
JA
4062 cfq_log(cfqd, "idle timer fired");
4063
22e2c507
JA
4064 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4065
fe094d98
JA
4066 cfqq = cfqd->active_queue;
4067 if (cfqq) {
3c6bd2f8
JA
4068 timed_out = 0;
4069
b029195d
JA
4070 /*
4071 * We saw a request before the queue expired, let it through
4072 */
4073 if (cfq_cfqq_must_dispatch(cfqq))
4074 goto out_kick;
4075
22e2c507
JA
4076 /*
4077 * expired
4078 */
44f7c160 4079 if (cfq_slice_used(cfqq))
22e2c507
JA
4080 goto expire;
4081
4082 /*
4083 * only expire and reinvoke request handler, if there are
4084 * other queues with pending requests
4085 */
caaa5f9f 4086 if (!cfqd->busy_queues)
22e2c507 4087 goto out_cont;
22e2c507
JA
4088
4089 /*
4090 * not expired and it has a request pending, let it dispatch
4091 */
75e50984 4092 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 4093 goto out_kick;
76280aff
CZ
4094
4095 /*
4096 * Queue depth flag is reset only when the idle didn't succeed
4097 */
4098 cfq_clear_cfqq_deep(cfqq);
22e2c507
JA
4099 }
4100expire:
e5ff082e 4101 cfq_slice_expired(cfqd, timed_out);
22e2c507 4102out_kick:
23e018a1 4103 cfq_schedule_dispatch(cfqd);
22e2c507
JA
4104out_cont:
4105 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4106}
4107
3b18152c
JA
4108static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4109{
4110 del_timer_sync(&cfqd->idle_slice_timer);
23e018a1 4111 cancel_work_sync(&cfqd->unplug_work);
3b18152c 4112}
22e2c507 4113
c2dea2d1
VT
4114static void cfq_put_async_queues(struct cfq_data *cfqd)
4115{
4116 int i;
4117
4118 for (i = 0; i < IOPRIO_BE_NR; i++) {
4119 if (cfqd->async_cfqq[0][i])
4120 cfq_put_queue(cfqd->async_cfqq[0][i]);
4121 if (cfqd->async_cfqq[1][i])
4122 cfq_put_queue(cfqd->async_cfqq[1][i]);
c2dea2d1 4123 }
2389d1ef
ON
4124
4125 if (cfqd->async_idle_cfqq)
4126 cfq_put_queue(cfqd->async_idle_cfqq);
c2dea2d1
VT
4127}
4128
b374d18a 4129static void cfq_exit_queue(struct elevator_queue *e)
1da177e4 4130{
22e2c507 4131 struct cfq_data *cfqd = e->elevator_data;
165125e1 4132 struct request_queue *q = cfqd->queue;
22e2c507 4133
3b18152c 4134 cfq_shutdown_timer_wq(cfqd);
e2d74ac0 4135
d9ff4187 4136 spin_lock_irq(q->queue_lock);
e2d74ac0 4137
d9ff4187 4138 if (cfqd->active_queue)
e5ff082e 4139 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
e2d74ac0 4140
c2dea2d1 4141 cfq_put_async_queues(cfqd);
03aa264a
TH
4142
4143 spin_unlock_irq(q->queue_lock);
4144
a90d742e
AV
4145 cfq_shutdown_timer_wq(cfqd);
4146
ffea73fc
TH
4147#ifdef CONFIG_CFQ_GROUP_IOSCHED
4148 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4149#else
f51b802c 4150 kfree(cfqd->root_group);
2abae55f 4151#endif
56edf7d7 4152 kfree(cfqd);
1da177e4
LT
4153}
4154
b2fab5ac 4155static int cfq_init_queue(struct request_queue *q)
1da177e4
LT
4156{
4157 struct cfq_data *cfqd;
3c798398 4158 struct blkcg_gq *blkg __maybe_unused;
a2b1693b 4159 int i, ret;
1da177e4 4160
94f6030c 4161 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
a73f730d 4162 if (!cfqd)
b2fab5ac 4163 return -ENOMEM;
80b15c73 4164
f51b802c
TH
4165 cfqd->queue = q;
4166 q->elevator->elevator_data = cfqd;
4167
1fa8f6d6
VG
4168 /* Init root service tree */
4169 cfqd->grp_service_tree = CFQ_RB_ROOT;
4170
f51b802c 4171 /* Init root group and prefer root group over other groups by default */
25fb5169 4172#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 4173 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
a2b1693b
TH
4174 if (ret)
4175 goto out_free;
f51b802c 4176
a2b1693b 4177 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
f51b802c 4178#else
a2b1693b 4179 ret = -ENOMEM;
f51b802c
TH
4180 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4181 GFP_KERNEL, cfqd->queue->node);
a2b1693b
TH
4182 if (!cfqd->root_group)
4183 goto out_free;
5624a4e4 4184
a2b1693b
TH
4185 cfq_init_cfqg_base(cfqd->root_group);
4186#endif
3381cb8d 4187 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
e71357e1 4188 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
5624a4e4 4189
26a2ac00
JA
4190 /*
4191 * Not strictly needed (since RB_ROOT just clears the node and we
4192 * zeroed cfqd on alloc), but better be safe in case someone decides
4193 * to add magic to the rb code
4194 */
4195 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4196 cfqd->prio_trees[i] = RB_ROOT;
4197
6118b70b
JA
4198 /*
4199 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4200 * Grab a permanent reference to it, so that the normal code flow
f51b802c
TH
4201 * will not attempt to free it. oom_cfqq is linked to root_group
4202 * but shouldn't hold a reference as it'll never be unlinked. Lose
4203 * the reference from linking right away.
6118b70b
JA
4204 */
4205 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
30d7b944 4206 cfqd->oom_cfqq.ref++;
1adaf3dd
TH
4207
4208 spin_lock_irq(q->queue_lock);
f51b802c 4209 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
eb7d8c07 4210 cfqg_put(cfqd->root_group);
1adaf3dd 4211 spin_unlock_irq(q->queue_lock);
1da177e4 4212
22e2c507
JA
4213 init_timer(&cfqd->idle_slice_timer);
4214 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4215 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4216
23e018a1 4217 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507 4218
1da177e4 4219 cfqd->cfq_quantum = cfq_quantum;
22e2c507
JA
4220 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4221 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4
LT
4222 cfqd->cfq_back_max = cfq_back_max;
4223 cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507
JA
4224 cfqd->cfq_slice[0] = cfq_slice_async;
4225 cfqd->cfq_slice[1] = cfq_slice_sync;
5bf14c07 4226 cfqd->cfq_target_latency = cfq_target_latency;
22e2c507
JA
4227 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4228 cfqd->cfq_slice_idle = cfq_slice_idle;
80bdf0c7 4229 cfqd->cfq_group_idle = cfq_group_idle;
963b72fc 4230 cfqd->cfq_latency = 1;
e459dd08 4231 cfqd->hw_tag = -1;
edc71131
CZ
4232 /*
4233 * we optimistically start assuming sync ops weren't delayed in last
4234 * second, in order to have larger depth for async operations.
4235 */
573412b2 4236 cfqd->last_delayed_sync = jiffies - HZ;
b2fab5ac 4237 return 0;
a2b1693b
TH
4238
4239out_free:
4240 kfree(cfqd);
4241 return ret;
1da177e4
LT
4242}
4243
1da177e4
LT
4244/*
4245 * sysfs parts below -->
4246 */
1da177e4
LT
4247static ssize_t
4248cfq_var_show(unsigned int var, char *page)
4249{
4250 return sprintf(page, "%d\n", var);
4251}
4252
4253static ssize_t
4254cfq_var_store(unsigned int *var, const char *page, size_t count)
4255{
4256 char *p = (char *) page;
4257
4258 *var = simple_strtoul(p, &p, 10);
4259 return count;
4260}
4261
1da177e4 4262#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
b374d18a 4263static ssize_t __FUNC(struct elevator_queue *e, char *page) \
1da177e4 4264{ \
3d1ab40f 4265 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
4266 unsigned int __data = __VAR; \
4267 if (__CONV) \
4268 __data = jiffies_to_msecs(__data); \
4269 return cfq_var_show(__data, (page)); \
4270}
4271SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
22e2c507
JA
4272SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4273SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
e572ec7e
AV
4274SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4275SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
22e2c507 4276SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
80bdf0c7 4277SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
22e2c507
JA
4278SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4279SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4280SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
963b72fc 4281SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
5bf14c07 4282SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
1da177e4
LT
4283#undef SHOW_FUNCTION
4284
4285#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
b374d18a 4286static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
1da177e4 4287{ \
3d1ab40f 4288 struct cfq_data *cfqd = e->elevator_data; \
1da177e4
LT
4289 unsigned int __data; \
4290 int ret = cfq_var_store(&__data, (page), count); \
4291 if (__data < (MIN)) \
4292 __data = (MIN); \
4293 else if (__data > (MAX)) \
4294 __data = (MAX); \
4295 if (__CONV) \
4296 *(__PTR) = msecs_to_jiffies(__data); \
4297 else \
4298 *(__PTR) = __data; \
4299 return ret; \
4300}
4301STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
fe094d98
JA
4302STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4303 UINT_MAX, 1);
4304STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4305 UINT_MAX, 1);
e572ec7e 4306STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
fe094d98
JA
4307STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4308 UINT_MAX, 0);
22e2c507 4309STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
80bdf0c7 4310STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
22e2c507
JA
4311STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4312STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
fe094d98
JA
4313STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4314 UINT_MAX, 0);
963b72fc 4315STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
5bf14c07 4316STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
1da177e4
LT
4317#undef STORE_FUNCTION
4318
e572ec7e
AV
4319#define CFQ_ATTR(name) \
4320 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
4321
4322static struct elv_fs_entry cfq_attrs[] = {
4323 CFQ_ATTR(quantum),
e572ec7e
AV
4324 CFQ_ATTR(fifo_expire_sync),
4325 CFQ_ATTR(fifo_expire_async),
4326 CFQ_ATTR(back_seek_max),
4327 CFQ_ATTR(back_seek_penalty),
4328 CFQ_ATTR(slice_sync),
4329 CFQ_ATTR(slice_async),
4330 CFQ_ATTR(slice_async_rq),
4331 CFQ_ATTR(slice_idle),
80bdf0c7 4332 CFQ_ATTR(group_idle),
963b72fc 4333 CFQ_ATTR(low_latency),
5bf14c07 4334 CFQ_ATTR(target_latency),
e572ec7e 4335 __ATTR_NULL
1da177e4
LT
4336};
4337
1da177e4
LT
4338static struct elevator_type iosched_cfq = {
4339 .ops = {
4340 .elevator_merge_fn = cfq_merge,
4341 .elevator_merged_fn = cfq_merged_request,
4342 .elevator_merge_req_fn = cfq_merged_requests,
da775265 4343 .elevator_allow_merge_fn = cfq_allow_merge,
812d4026 4344 .elevator_bio_merged_fn = cfq_bio_merged,
b4878f24 4345 .elevator_dispatch_fn = cfq_dispatch_requests,
1da177e4 4346 .elevator_add_req_fn = cfq_insert_request,
b4878f24 4347 .elevator_activate_req_fn = cfq_activate_request,
1da177e4 4348 .elevator_deactivate_req_fn = cfq_deactivate_request,
1da177e4 4349 .elevator_completed_req_fn = cfq_completed_request,
21183b07
JA
4350 .elevator_former_req_fn = elv_rb_former_request,
4351 .elevator_latter_req_fn = elv_rb_latter_request,
9b84cacd 4352 .elevator_init_icq_fn = cfq_init_icq,
7e5a8794 4353 .elevator_exit_icq_fn = cfq_exit_icq,
1da177e4
LT
4354 .elevator_set_req_fn = cfq_set_request,
4355 .elevator_put_req_fn = cfq_put_request,
4356 .elevator_may_queue_fn = cfq_may_queue,
4357 .elevator_init_fn = cfq_init_queue,
4358 .elevator_exit_fn = cfq_exit_queue,
4359 },
3d3c2379
TH
4360 .icq_size = sizeof(struct cfq_io_cq),
4361 .icq_align = __alignof__(struct cfq_io_cq),
3d1ab40f 4362 .elevator_attrs = cfq_attrs,
3d3c2379 4363 .elevator_name = "cfq",
1da177e4
LT
4364 .elevator_owner = THIS_MODULE,
4365};
4366
3e252066 4367#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 4368static struct blkcg_policy blkcg_policy_cfq = {
f9fcc2d3
TH
4369 .pd_size = sizeof(struct cfq_group),
4370 .cftypes = cfq_blkcg_files,
4371
4372 .pd_init_fn = cfq_pd_init,
4373 .pd_reset_stats_fn = cfq_pd_reset_stats,
3e252066 4374};
3e252066
VG
4375#endif
4376
1da177e4
LT
4377static int __init cfq_init(void)
4378{
3d3c2379
TH
4379 int ret;
4380
22e2c507
JA
4381 /*
4382 * could be 0 on HZ < 1000 setups
4383 */
4384 if (!cfq_slice_async)
4385 cfq_slice_async = 1;
4386 if (!cfq_slice_idle)
4387 cfq_slice_idle = 1;
4388
80bdf0c7
VG
4389#ifdef CONFIG_CFQ_GROUP_IOSCHED
4390 if (!cfq_group_idle)
4391 cfq_group_idle = 1;
8bd435b3 4392
3c798398 4393 ret = blkcg_policy_register(&blkcg_policy_cfq);
8bd435b3
TH
4394 if (ret)
4395 return ret;
ffea73fc
TH
4396#else
4397 cfq_group_idle = 0;
4398#endif
8bd435b3 4399
fd794956 4400 ret = -ENOMEM;
3d3c2379
TH
4401 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4402 if (!cfq_pool)
8bd435b3 4403 goto err_pol_unreg;
1da177e4 4404
3d3c2379 4405 ret = elv_register(&iosched_cfq);
8bd435b3
TH
4406 if (ret)
4407 goto err_free_pool;
3d3c2379 4408
2fdd82bd 4409 return 0;
8bd435b3
TH
4410
4411err_free_pool:
4412 kmem_cache_destroy(cfq_pool);
4413err_pol_unreg:
ffea73fc 4414#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 4415 blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc 4416#endif
8bd435b3 4417 return ret;
1da177e4
LT
4418}
4419
4420static void __exit cfq_exit(void)
4421{
ffea73fc 4422#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 4423 blkcg_policy_unregister(&blkcg_policy_cfq);
ffea73fc 4424#endif
1da177e4 4425 elv_unregister(&iosched_cfq);
3d3c2379 4426 kmem_cache_destroy(cfq_pool);
1da177e4
LT
4427}
4428
4429module_init(cfq_init);
4430module_exit(cfq_exit);
4431
4432MODULE_AUTHOR("Jens Axboe");
4433MODULE_LICENSE("GPL");
4434MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");