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