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