block, scheduler: convert xxx_var_store to void
[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 */
b3193bc0
RH
2937 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag &&
2938 !cfqd->cfq_group_idle)
a68bbddb
JA
2939 return;
2940
dd67d051 2941 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
6d048f53 2942 WARN_ON(cfq_cfqq_slice_new(cfqq));
22e2c507
JA
2943
2944 /*
2945 * idle is disabled, either manually or by past process history
2946 */
80bdf0c7
VG
2947 if (!cfq_should_idle(cfqd, cfqq)) {
2948 /* no queue idling. Check for group idling */
2949 if (cfqd->cfq_group_idle)
2950 group_idle = cfqd->cfq_group_idle;
2951 else
2952 return;
2953 }
6d048f53 2954
7b679138 2955 /*
8e550632 2956 * still active requests from this queue, don't idle
7b679138 2957 */
8e550632 2958 if (cfqq->dispatched)
7b679138
JA
2959 return;
2960
22e2c507
JA
2961 /*
2962 * task has exited, don't wait
2963 */
206dc69b 2964 cic = cfqd->active_cic;
f6e8d01b 2965 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
6d048f53
JA
2966 return;
2967
355b659c
CZ
2968 /*
2969 * If our average think time is larger than the remaining time
2970 * slice, then don't idle. This avoids overrunning the allotted
2971 * time slice.
2972 */
383cd721 2973 if (sample_valid(cic->ttime.ttime_samples) &&
9a7f38c4
JM
2974 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2975 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
383cd721 2976 cic->ttime.ttime_mean);
355b659c 2977 return;
b1ffe737 2978 }
355b659c 2979
e795421e
JK
2980 /*
2981 * There are other queues in the group or this is the only group and
2982 * it has too big thinktime, don't do group idle.
2983 */
2984 if (group_idle &&
2985 (cfqq->cfqg->nr_cfqq > 1 ||
2986 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
80bdf0c7
VG
2987 return;
2988
3b18152c 2989 cfq_mark_cfqq_wait_request(cfqq);
22e2c507 2990
80bdf0c7
VG
2991 if (group_idle)
2992 sl = cfqd->cfq_group_idle;
2993 else
2994 sl = cfqd->cfq_slice_idle;
206dc69b 2995
91148325
JK
2996 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2997 HRTIMER_MODE_REL);
155fead9 2998 cfqg_stats_set_start_idle_time(cfqq->cfqg);
9a7f38c4 2999 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
80bdf0c7 3000 group_idle ? 1 : 0);
1da177e4
LT
3001}
3002
498d3aa2
JA
3003/*
3004 * Move request from internal lists to the request queue dispatch list.
3005 */
165125e1 3006static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
1da177e4 3007{
3ed9a296 3008 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 3009 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 3010
7b679138
JA
3011 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
3012
06d21886 3013 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
5380a101 3014 cfq_remove_request(rq);
6d048f53 3015 cfqq->dispatched++;
80bdf0c7 3016 (RQ_CFQG(rq))->dispatched++;
5380a101 3017 elv_dispatch_sort(q, rq);
3ed9a296 3018
53c583d2 3019 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
c4e7893e 3020 cfqq->nr_sectors += blk_rq_sectors(rq);
1da177e4
LT
3021}
3022
3023/*
3024 * return expired entry, or NULL to just start from scratch in rbtree
3025 */
febffd61 3026static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
1da177e4 3027{
30996f40 3028 struct request *rq = NULL;
1da177e4 3029
3b18152c 3030 if (cfq_cfqq_fifo_expire(cfqq))
1da177e4 3031 return NULL;
cb887411
JA
3032
3033 cfq_mark_cfqq_fifo_expire(cfqq);
3034
89850f7e
JA
3035 if (list_empty(&cfqq->fifo))
3036 return NULL;
1da177e4 3037
89850f7e 3038 rq = rq_entry_fifo(cfqq->fifo.next);
9a7f38c4 3039 if (ktime_get_ns() < rq->fifo_time)
7b679138 3040 rq = NULL;
1da177e4 3041
6d048f53 3042 return rq;
1da177e4
LT
3043}
3044
22e2c507
JA
3045static inline int
3046cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3047{
3048 const int base_rq = cfqd->cfq_slice_async_rq;
1da177e4 3049
22e2c507 3050 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1da177e4 3051
b9f8ce05 3052 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
1da177e4
LT
3053}
3054
df5fe3e8
JM
3055/*
3056 * Must be called with the queue_lock held.
3057 */
3058static int cfqq_process_refs(struct cfq_queue *cfqq)
3059{
3060 int process_refs, io_refs;
3061
3062 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
30d7b944 3063 process_refs = cfqq->ref - io_refs;
df5fe3e8
JM
3064 BUG_ON(process_refs < 0);
3065 return process_refs;
3066}
3067
3068static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3069{
e6c5bc73 3070 int process_refs, new_process_refs;
df5fe3e8
JM
3071 struct cfq_queue *__cfqq;
3072
c10b61f0
JM
3073 /*
3074 * If there are no process references on the new_cfqq, then it is
3075 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3076 * chain may have dropped their last reference (not just their
3077 * last process reference).
3078 */
3079 if (!cfqq_process_refs(new_cfqq))
3080 return;
3081
df5fe3e8
JM
3082 /* Avoid a circular list and skip interim queue merges */
3083 while ((__cfqq = new_cfqq->new_cfqq)) {
3084 if (__cfqq == cfqq)
3085 return;
3086 new_cfqq = __cfqq;
3087 }
3088
3089 process_refs = cfqq_process_refs(cfqq);
c10b61f0 3090 new_process_refs = cfqq_process_refs(new_cfqq);
df5fe3e8
JM
3091 /*
3092 * If the process for the cfqq has gone away, there is no
3093 * sense in merging the queues.
3094 */
c10b61f0 3095 if (process_refs == 0 || new_process_refs == 0)
df5fe3e8
JM
3096 return;
3097
e6c5bc73
JM
3098 /*
3099 * Merge in the direction of the lesser amount of work.
3100 */
e6c5bc73
JM
3101 if (new_process_refs >= process_refs) {
3102 cfqq->new_cfqq = new_cfqq;
30d7b944 3103 new_cfqq->ref += process_refs;
e6c5bc73
JM
3104 } else {
3105 new_cfqq->new_cfqq = cfqq;
30d7b944 3106 cfqq->ref += new_process_refs;
e6c5bc73 3107 }
df5fe3e8
JM
3108}
3109
6d816ec7 3110static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
3bf10fea 3111 struct cfq_group *cfqg, enum wl_class_t wl_class)
718eee05
CZ
3112{
3113 struct cfq_queue *queue;
3114 int i;
3115 bool key_valid = false;
9a7f38c4 3116 u64 lowest_key = 0;
718eee05
CZ
3117 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3118
65b32a57
VG
3119 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3120 /* select the one with lowest rb_key */
34b98d03 3121 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
718eee05 3122 if (queue &&
9a7f38c4 3123 (!key_valid || queue->rb_key < lowest_key)) {
718eee05
CZ
3124 lowest_key = queue->rb_key;
3125 cur_best = i;
3126 key_valid = true;
3127 }
3128 }
3129
3130 return cur_best;
3131}
3132
6d816ec7
VG
3133static void
3134choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
718eee05 3135{
9a7f38c4 3136 u64 slice;
718eee05 3137 unsigned count;
cdb16e8f 3138 struct cfq_rb_root *st;
9a7f38c4 3139 u64 group_slice;
4d2ceea4 3140 enum wl_class_t original_class = cfqd->serving_wl_class;
9a7f38c4 3141 u64 now = ktime_get_ns();
1fa8f6d6 3142
718eee05 3143 /* Choose next priority. RT > BE > IDLE */
58ff82f3 3144 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
4d2ceea4 3145 cfqd->serving_wl_class = RT_WORKLOAD;
58ff82f3 3146 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
4d2ceea4 3147 cfqd->serving_wl_class = BE_WORKLOAD;
718eee05 3148 else {
4d2ceea4 3149 cfqd->serving_wl_class = IDLE_WORKLOAD;
9a7f38c4 3150 cfqd->workload_expires = now + jiffies_to_nsecs(1);
718eee05
CZ
3151 return;
3152 }
3153
4d2ceea4 3154 if (original_class != cfqd->serving_wl_class)
e4ea0c16
SL
3155 goto new_workload;
3156
718eee05
CZ
3157 /*
3158 * For RT and BE, we have to choose also the type
3159 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3160 * expiration time
3161 */
34b98d03 3162 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f 3163 count = st->count;
718eee05
CZ
3164
3165 /*
65b32a57 3166 * check workload expiration, and that we still have other queues ready
718eee05 3167 */
9a7f38c4 3168 if (count && !(now > cfqd->workload_expires))
718eee05
CZ
3169 return;
3170
e4ea0c16 3171new_workload:
718eee05 3172 /* otherwise select new workload type */
6d816ec7 3173 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
4d2ceea4 3174 cfqd->serving_wl_class);
34b98d03 3175 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
cdb16e8f 3176 count = st->count;
718eee05
CZ
3177
3178 /*
3179 * the workload slice is computed as a fraction of target latency
3180 * proportional to the number of queues in that workload, over
3181 * all the queues in the same priority class
3182 */
58ff82f3
VG
3183 group_slice = cfq_group_slice(cfqd, cfqg);
3184
9a7f38c4 3185 slice = div_u64(group_slice * count,
4d2ceea4
VG
3186 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3187 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
9a7f38c4 3188 cfqg)));
718eee05 3189
4d2ceea4 3190 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
9a7f38c4 3191 u64 tmp;
f26bd1f0
VG
3192
3193 /*
3194 * Async queues are currently system wide. Just taking
3195 * proportion of queues with-in same group will lead to higher
3196 * async ratio system wide as generally root group is going
3197 * to have higher weight. A more accurate thing would be to
3198 * calculate system wide asnc/sync ratio.
3199 */
5bf14c07
TM
3200 tmp = cfqd->cfq_target_latency *
3201 cfqg_busy_async_queues(cfqd, cfqg);
9a7f38c4
JM
3202 tmp = div_u64(tmp, cfqd->busy_queues);
3203 slice = min_t(u64, slice, tmp);
f26bd1f0 3204
718eee05
CZ
3205 /* async workload slice is scaled down according to
3206 * the sync/async slice ratio. */
9a7f38c4 3207 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
f26bd1f0 3208 } else
718eee05
CZ
3209 /* sync workload slice is at least 2 * cfq_slice_idle */
3210 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3211
9a7f38c4
JM
3212 slice = max_t(u64, slice, CFQ_MIN_TT);
3213 cfq_log(cfqd, "workload slice:%llu", slice);
3214 cfqd->workload_expires = now + slice;
718eee05
CZ
3215}
3216
1fa8f6d6
VG
3217static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3218{
3219 struct cfq_rb_root *st = &cfqd->grp_service_tree;
25bc6b07 3220 struct cfq_group *cfqg;
1fa8f6d6
VG
3221
3222 if (RB_EMPTY_ROOT(&st->rb))
3223 return NULL;
25bc6b07 3224 cfqg = cfq_rb_first_group(st);
25bc6b07
VG
3225 update_min_vdisktime(st);
3226 return cfqg;
1fa8f6d6
VG
3227}
3228
cdb16e8f
VG
3229static void cfq_choose_cfqg(struct cfq_data *cfqd)
3230{
1fa8f6d6 3231 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
9a7f38c4 3232 u64 now = ktime_get_ns();
1fa8f6d6
VG
3233
3234 cfqd->serving_group = cfqg;
dae739eb
VG
3235
3236 /* Restore the workload type data */
4d2ceea4 3237 if (cfqg->saved_wl_slice) {
9a7f38c4 3238 cfqd->workload_expires = now + cfqg->saved_wl_slice;
4d2ceea4
VG
3239 cfqd->serving_wl_type = cfqg->saved_wl_type;
3240 cfqd->serving_wl_class = cfqg->saved_wl_class;
66ae2919 3241 } else
9a7f38c4 3242 cfqd->workload_expires = now - 1;
66ae2919 3243
6d816ec7 3244 choose_wl_class_and_type(cfqd, cfqg);
cdb16e8f
VG
3245}
3246
22e2c507 3247/*
498d3aa2
JA
3248 * Select a queue for service. If we have a current active queue,
3249 * check whether to continue servicing it, or retrieve and set a new one.
22e2c507 3250 */
1b5ed5e1 3251static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1da177e4 3252{
a36e71f9 3253 struct cfq_queue *cfqq, *new_cfqq = NULL;
9a7f38c4 3254 u64 now = ktime_get_ns();
1da177e4 3255
22e2c507
JA
3256 cfqq = cfqd->active_queue;
3257 if (!cfqq)
3258 goto new_queue;
1da177e4 3259
f04a6424
VG
3260 if (!cfqd->rq_queued)
3261 return NULL;
c244bb50
VG
3262
3263 /*
3264 * We were waiting for group to get backlogged. Expire the queue
3265 */
3266 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3267 goto expire;
3268
22e2c507 3269 /*
6d048f53 3270 * The active queue has run out of time, expire it and select new.
22e2c507 3271 */
7667aa06
VG
3272 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3273 /*
3274 * If slice had not expired at the completion of last request
3275 * we might not have turned on wait_busy flag. Don't expire
3276 * the queue yet. Allow the group to get backlogged.
3277 *
3278 * The very fact that we have used the slice, that means we
3279 * have been idling all along on this queue and it should be
3280 * ok to wait for this request to complete.
3281 */
82bbbf28
VG
3282 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3283 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3284 cfqq = NULL;
7667aa06 3285 goto keep_queue;
82bbbf28 3286 } else
80bdf0c7 3287 goto check_group_idle;
7667aa06 3288 }
1da177e4 3289
22e2c507 3290 /*
6d048f53
JA
3291 * The active queue has requests and isn't expired, allow it to
3292 * dispatch.
22e2c507 3293 */
dd67d051 3294 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 3295 goto keep_queue;
6d048f53 3296
a36e71f9
JA
3297 /*
3298 * If another queue has a request waiting within our mean seek
3299 * distance, let it run. The expire code will check for close
3300 * cooperators and put the close queue at the front of the service
df5fe3e8 3301 * tree. If possible, merge the expiring queue with the new cfqq.
a36e71f9 3302 */
b3b6d040 3303 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
df5fe3e8
JM
3304 if (new_cfqq) {
3305 if (!cfqq->new_cfqq)
3306 cfq_setup_merge(cfqq, new_cfqq);
a36e71f9 3307 goto expire;
df5fe3e8 3308 }
a36e71f9 3309
6d048f53
JA
3310 /*
3311 * No requests pending. If the active queue still has requests in
3312 * flight or is idling for a new request, allow either of these
3313 * conditions to happen (or time out) before selecting a new queue.
3314 */
91148325 3315 if (hrtimer_active(&cfqd->idle_slice_timer)) {
80bdf0c7
VG
3316 cfqq = NULL;
3317 goto keep_queue;
3318 }
3319
8e1ac665
SL
3320 /*
3321 * This is a deep seek queue, but the device is much faster than
3322 * the queue can deliver, don't idle
3323 **/
3324 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3325 (cfq_cfqq_slice_new(cfqq) ||
9a7f38c4 3326 (cfqq->slice_end - now > now - cfqq->slice_start))) {
8e1ac665
SL
3327 cfq_clear_cfqq_deep(cfqq);
3328 cfq_clear_cfqq_idle_window(cfqq);
3329 }
3330
80bdf0c7
VG
3331 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3332 cfqq = NULL;
3333 goto keep_queue;
3334 }
3335
3336 /*
3337 * If group idle is enabled and there are requests dispatched from
3338 * this group, wait for requests to complete.
3339 */
3340check_group_idle:
7700fc4f
SL
3341 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3342 cfqq->cfqg->dispatched &&
3343 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
caaa5f9f
JA
3344 cfqq = NULL;
3345 goto keep_queue;
22e2c507
JA
3346 }
3347
3b18152c 3348expire:
e5ff082e 3349 cfq_slice_expired(cfqd, 0);
3b18152c 3350new_queue:
718eee05
CZ
3351 /*
3352 * Current queue expired. Check if we have to switch to a new
3353 * service tree
3354 */
3355 if (!new_cfqq)
cdb16e8f 3356 cfq_choose_cfqg(cfqd);
718eee05 3357
a36e71f9 3358 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
22e2c507 3359keep_queue:
3b18152c 3360 return cfqq;
22e2c507
JA
3361}
3362
febffd61 3363static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
d9e7620e
JA
3364{
3365 int dispatched = 0;
3366
3367 while (cfqq->next_rq) {
3368 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3369 dispatched++;
3370 }
3371
3372 BUG_ON(!list_empty(&cfqq->fifo));
f04a6424
VG
3373
3374 /* By default cfqq is not expired if it is empty. Do it explicitly */
e5ff082e 3375 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
d9e7620e
JA
3376 return dispatched;
3377}
3378
498d3aa2
JA
3379/*
3380 * Drain our current requests. Used for barriers and when switching
3381 * io schedulers on-the-fly.
3382 */
d9e7620e 3383static int cfq_forced_dispatch(struct cfq_data *cfqd)
1b5ed5e1 3384{
0871714e 3385 struct cfq_queue *cfqq;
d9e7620e 3386 int dispatched = 0;
cdb16e8f 3387
3440c49f 3388 /* Expire the timeslice of the current active queue first */
e5ff082e 3389 cfq_slice_expired(cfqd, 0);
3440c49f
DS
3390 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3391 __cfq_set_active_queue(cfqd, cfqq);
f04a6424 3392 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
3440c49f 3393 }
1b5ed5e1 3394
1b5ed5e1
TH
3395 BUG_ON(cfqd->busy_queues);
3396
6923715a 3397 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
1b5ed5e1
TH
3398 return dispatched;
3399}
3400
abc3c744
SL
3401static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3402 struct cfq_queue *cfqq)
3403{
9a7f38c4
JM
3404 u64 now = ktime_get_ns();
3405
abc3c744
SL
3406 /* the queue hasn't finished any request, can't estimate */
3407 if (cfq_cfqq_slice_new(cfqq))
c1e44756 3408 return true;
9a7f38c4 3409 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
c1e44756 3410 return true;
abc3c744 3411
c1e44756 3412 return false;
abc3c744
SL
3413}
3414
0b182d61 3415static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2f5cb738 3416{
2f5cb738 3417 unsigned int max_dispatch;
22e2c507 3418
3932a86b
GC
3419 if (cfq_cfqq_must_dispatch(cfqq))
3420 return true;
3421
5ad531db
JA
3422 /*
3423 * Drain async requests before we start sync IO
3424 */
53c583d2 3425 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
0b182d61 3426 return false;
5ad531db 3427
2f5cb738
JA
3428 /*
3429 * If this is an async queue and we have sync IO in flight, let it wait
3430 */
53c583d2 3431 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
0b182d61 3432 return false;
2f5cb738 3433
abc3c744 3434 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
2f5cb738
JA
3435 if (cfq_class_idle(cfqq))
3436 max_dispatch = 1;
b4878f24 3437
2f5cb738
JA
3438 /*
3439 * Does this cfqq already have too much IO in flight?
3440 */
3441 if (cfqq->dispatched >= max_dispatch) {
ef8a41df 3442 bool promote_sync = false;
2f5cb738
JA
3443 /*
3444 * idle queue must always only have a single IO in flight
3445 */
3ed9a296 3446 if (cfq_class_idle(cfqq))
0b182d61 3447 return false;
3ed9a296 3448
ef8a41df 3449 /*
c4ade94f
LS
3450 * If there is only one sync queue
3451 * we can ignore async queue here and give the sync
ef8a41df
SL
3452 * queue no dispatch limit. The reason is a sync queue can
3453 * preempt async queue, limiting the sync queue doesn't make
3454 * sense. This is useful for aiostress test.
3455 */
c4ade94f
LS
3456 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3457 promote_sync = true;
ef8a41df 3458
2f5cb738
JA
3459 /*
3460 * We have other queues, don't allow more IO from this one
3461 */
ef8a41df
SL
3462 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3463 !promote_sync)
0b182d61 3464 return false;
9ede209e 3465
365722bb 3466 /*
474b18cc 3467 * Sole queue user, no limit
365722bb 3468 */
ef8a41df 3469 if (cfqd->busy_queues == 1 || promote_sync)
abc3c744
SL
3470 max_dispatch = -1;
3471 else
3472 /*
3473 * Normally we start throttling cfqq when cfq_quantum/2
3474 * requests have been dispatched. But we can drive
3475 * deeper queue depths at the beginning of slice
3476 * subjected to upper limit of cfq_quantum.
3477 * */
3478 max_dispatch = cfqd->cfq_quantum;
8e296755
JA
3479 }
3480
3481 /*
3482 * Async queues must wait a bit before being allowed dispatch.
3483 * We also ramp up the dispatch depth gradually for async IO,
3484 * based on the last sync IO we serviced
3485 */
963b72fc 3486 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
9a7f38c4 3487 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
8e296755 3488 unsigned int depth;
365722bb 3489
9a7f38c4 3490 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
e00c54c3
JA
3491 if (!depth && !cfqq->dispatched)
3492 depth = 1;
8e296755
JA
3493 if (depth < max_dispatch)
3494 max_dispatch = depth;
2f5cb738 3495 }
3ed9a296 3496
0b182d61
JA
3497 /*
3498 * If we're below the current max, allow a dispatch
3499 */
3500 return cfqq->dispatched < max_dispatch;
3501}
3502
3503/*
3504 * Dispatch a request from cfqq, moving them to the request queue
3505 * dispatch list.
3506 */
3507static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3508{
3509 struct request *rq;
3510
3511 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3512
3932a86b
GC
3513 rq = cfq_check_fifo(cfqq);
3514 if (rq)
3515 cfq_mark_cfqq_must_dispatch(cfqq);
3516
0b182d61
JA
3517 if (!cfq_may_dispatch(cfqd, cfqq))
3518 return false;
3519
3520 /*
3521 * follow expired path, else get first next available
3522 */
0b182d61
JA
3523 if (!rq)
3524 rq = cfqq->next_rq;
3932a86b
GC
3525 else
3526 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
0b182d61
JA
3527
3528 /*
3529 * insert request into driver dispatch list
3530 */
3531 cfq_dispatch_insert(cfqd->queue, rq);
3532
3533 if (!cfqd->active_cic) {
c5869807 3534 struct cfq_io_cq *cic = RQ_CIC(rq);
0b182d61 3535
c5869807 3536 atomic_long_inc(&cic->icq.ioc->refcount);
0b182d61
JA
3537 cfqd->active_cic = cic;
3538 }
3539
3540 return true;
3541}
3542
3543/*
3544 * Find the cfqq that we need to service and move a request from that to the
3545 * dispatch list
3546 */
3547static int cfq_dispatch_requests(struct request_queue *q, int force)
3548{
3549 struct cfq_data *cfqd = q->elevator->elevator_data;
3550 struct cfq_queue *cfqq;
3551
3552 if (!cfqd->busy_queues)
3553 return 0;
3554
3555 if (unlikely(force))
3556 return cfq_forced_dispatch(cfqd);
3557
3558 cfqq = cfq_select_queue(cfqd);
3559 if (!cfqq)
8e296755
JA
3560 return 0;
3561
2f5cb738 3562 /*
0b182d61 3563 * Dispatch a request from this cfqq, if it is allowed
2f5cb738 3564 */
0b182d61
JA
3565 if (!cfq_dispatch_request(cfqd, cfqq))
3566 return 0;
3567
2f5cb738 3568 cfqq->slice_dispatch++;
b029195d 3569 cfq_clear_cfqq_must_dispatch(cfqq);
22e2c507 3570
2f5cb738
JA
3571 /*
3572 * expire an async queue immediately if it has used up its slice. idle
3573 * queue always expire after 1 dispatch round.
3574 */
3575 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3576 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3577 cfq_class_idle(cfqq))) {
9a7f38c4 3578 cfqq->slice_end = ktime_get_ns() + 1;
e5ff082e 3579 cfq_slice_expired(cfqd, 0);
1da177e4
LT
3580 }
3581
b217a903 3582 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
2f5cb738 3583 return 1;
1da177e4
LT
3584}
3585
1da177e4 3586/*
5e705374
JA
3587 * task holds one reference to the queue, dropped when task exits. each rq
3588 * in-flight on this queue also holds a reference, dropped when rq is freed.
1da177e4 3589 *
b1c35769 3590 * Each cfq queue took a reference on the parent group. Drop it now.
1da177e4
LT
3591 * queue lock must be held here.
3592 */
3593static void cfq_put_queue(struct cfq_queue *cfqq)
3594{
22e2c507 3595 struct cfq_data *cfqd = cfqq->cfqd;
0bbfeb83 3596 struct cfq_group *cfqg;
22e2c507 3597
30d7b944 3598 BUG_ON(cfqq->ref <= 0);
1da177e4 3599
30d7b944
SL
3600 cfqq->ref--;
3601 if (cfqq->ref)
1da177e4
LT
3602 return;
3603
7b679138 3604 cfq_log_cfqq(cfqd, cfqq, "put_queue");
1da177e4 3605 BUG_ON(rb_first(&cfqq->sort_list));
22e2c507 3606 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
b1c35769 3607 cfqg = cfqq->cfqg;
1da177e4 3608
28f95cbc 3609 if (unlikely(cfqd->active_queue == cfqq)) {
e5ff082e 3610 __cfq_slice_expired(cfqd, cfqq, 0);
23e018a1 3611 cfq_schedule_dispatch(cfqd);
28f95cbc 3612 }
22e2c507 3613
f04a6424 3614 BUG_ON(cfq_cfqq_on_rr(cfqq));
1da177e4 3615 kmem_cache_free(cfq_pool, cfqq);
eb7d8c07 3616 cfqg_put(cfqg);
1da177e4
LT
3617}
3618
d02a2c07 3619static void cfq_put_cooperator(struct cfq_queue *cfqq)
1da177e4 3620{
df5fe3e8
JM
3621 struct cfq_queue *__cfqq, *next;
3622
df5fe3e8
JM
3623 /*
3624 * If this queue was scheduled to merge with another queue, be
3625 * sure to drop the reference taken on that queue (and others in
3626 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3627 */
3628 __cfqq = cfqq->new_cfqq;
3629 while (__cfqq) {
3630 if (__cfqq == cfqq) {
3631 WARN(1, "cfqq->new_cfqq loop detected\n");
3632 break;
3633 }
3634 next = __cfqq->new_cfqq;
3635 cfq_put_queue(__cfqq);
3636 __cfqq = next;
3637 }
d02a2c07
SL
3638}
3639
3640static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3641{
3642 if (unlikely(cfqq == cfqd->active_queue)) {
3643 __cfq_slice_expired(cfqd, cfqq, 0);
3644 cfq_schedule_dispatch(cfqd);
3645 }
3646
3647 cfq_put_cooperator(cfqq);
df5fe3e8 3648
89850f7e
JA
3649 cfq_put_queue(cfqq);
3650}
22e2c507 3651
9b84cacd
TH
3652static void cfq_init_icq(struct io_cq *icq)
3653{
3654 struct cfq_io_cq *cic = icq_to_cic(icq);
3655
9a7f38c4 3656 cic->ttime.last_end_request = ktime_get_ns();
9b84cacd
TH
3657}
3658
c5869807 3659static void cfq_exit_icq(struct io_cq *icq)
89850f7e 3660{
c5869807 3661 struct cfq_io_cq *cic = icq_to_cic(icq);
283287a5 3662 struct cfq_data *cfqd = cic_to_cfqd(cic);
4faa3c81 3663
563180a4
TH
3664 if (cic_to_cfqq(cic, false)) {
3665 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3666 cic_set_cfqq(cic, NULL, false);
12a05732
AV
3667 }
3668
563180a4
TH
3669 if (cic_to_cfqq(cic, true)) {
3670 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3671 cic_set_cfqq(cic, NULL, true);
12a05732 3672 }
89850f7e
JA
3673}
3674
abede6da 3675static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
22e2c507
JA
3676{
3677 struct task_struct *tsk = current;
3678 int ioprio_class;
3679
3b18152c 3680 if (!cfq_cfqq_prio_changed(cfqq))
22e2c507
JA
3681 return;
3682
598971bf 3683 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
22e2c507 3684 switch (ioprio_class) {
fe094d98
JA
3685 default:
3686 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3687 case IOPRIO_CLASS_NONE:
3688 /*
6d63c275 3689 * no prio set, inherit CPU scheduling settings
fe094d98
JA
3690 */
3691 cfqq->ioprio = task_nice_ioprio(tsk);
6d63c275 3692 cfqq->ioprio_class = task_nice_ioclass(tsk);
fe094d98
JA
3693 break;
3694 case IOPRIO_CLASS_RT:
598971bf 3695 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98
JA
3696 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3697 break;
3698 case IOPRIO_CLASS_BE:
598971bf 3699 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
fe094d98
JA
3700 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3701 break;
3702 case IOPRIO_CLASS_IDLE:
3703 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3704 cfqq->ioprio = 7;
3705 cfq_clear_cfqq_idle_window(cfqq);
3706 break;
22e2c507
JA
3707 }
3708
3709 /*
3710 * keep track of original prio settings in case we have to temporarily
3711 * elevate the priority of this queue
3712 */
3713 cfqq->org_ioprio = cfqq->ioprio;
b8269db4 3714 cfqq->org_ioprio_class = cfqq->ioprio_class;
3b18152c 3715 cfq_clear_cfqq_prio_changed(cfqq);
22e2c507
JA
3716}
3717
598971bf 3718static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
22e2c507 3719{
598971bf 3720 int ioprio = cic->icq.ioc->ioprio;
bca4b914 3721 struct cfq_data *cfqd = cic_to_cfqd(cic);
478a82b0 3722 struct cfq_queue *cfqq;
35e6077c 3723
598971bf
TH
3724 /*
3725 * Check whether ioprio has changed. The condition may trigger
3726 * spuriously on a newly created cic but there's no harm.
3727 */
3728 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
caaa5f9f
JA
3729 return;
3730
563180a4 3731 cfqq = cic_to_cfqq(cic, false);
caaa5f9f 3732 if (cfqq) {
563180a4 3733 cfq_put_queue(cfqq);
2da8de0b 3734 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
563180a4 3735 cic_set_cfqq(cic, cfqq, false);
22e2c507 3736 }
caaa5f9f 3737
563180a4 3738 cfqq = cic_to_cfqq(cic, true);
caaa5f9f
JA
3739 if (cfqq)
3740 cfq_mark_cfqq_prio_changed(cfqq);
598971bf
TH
3741
3742 cic->ioprio = ioprio;
22e2c507
JA
3743}
3744
d5036d77 3745static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
a6151c3a 3746 pid_t pid, bool is_sync)
d5036d77
JA
3747{
3748 RB_CLEAR_NODE(&cfqq->rb_node);
3749 RB_CLEAR_NODE(&cfqq->p_node);
3750 INIT_LIST_HEAD(&cfqq->fifo);
3751
30d7b944 3752 cfqq->ref = 0;
d5036d77
JA
3753 cfqq->cfqd = cfqd;
3754
3755 cfq_mark_cfqq_prio_changed(cfqq);
3756
3757 if (is_sync) {
3758 if (!cfq_class_idle(cfqq))
3759 cfq_mark_cfqq_idle_window(cfqq);
3760 cfq_mark_cfqq_sync(cfqq);
3761 }
3762 cfqq->pid = pid;
3763}
3764
24610333 3765#ifdef CONFIG_CFQ_GROUP_IOSCHED
142bbdfc 3766static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
24610333 3767{
bca4b914 3768 struct cfq_data *cfqd = cic_to_cfqd(cic);
60a83707 3769 struct cfq_queue *cfqq;
f4da8072 3770 uint64_t serial_nr;
24610333 3771
598971bf 3772 rcu_read_lock();
f4da8072 3773 serial_nr = bio_blkcg(bio)->css.serial_nr;
598971bf 3774 rcu_read_unlock();
24610333 3775
598971bf
TH
3776 /*
3777 * Check whether blkcg has changed. The condition may trigger
3778 * spuriously on a newly created cic but there's no harm.
3779 */
f4da8072 3780 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
142bbdfc 3781 return;
87760e5e 3782
60a83707
TH
3783 /*
3784 * Drop reference to queues. New queues will be assigned in new
3785 * group upon arrival of fresh requests.
3786 */
3787 cfqq = cic_to_cfqq(cic, false);
3788 if (cfqq) {
3789 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3790 cic_set_cfqq(cic, NULL, false);
3791 cfq_put_queue(cfqq);
3792 }
3793
3794 cfqq = cic_to_cfqq(cic, true);
3795 if (cfqq) {
3796 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3797 cic_set_cfqq(cic, NULL, true);
3798 cfq_put_queue(cfqq);
24610333 3799 }
598971bf 3800
f4da8072 3801 cic->blkcg_serial_nr = serial_nr;
24610333 3802}
598971bf 3803#else
142bbdfc 3804static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
5d7f5ce1 3805{
5d7f5ce1 3806}
24610333
VG
3807#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3808
c2dea2d1 3809static struct cfq_queue **
60a83707 3810cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
c2dea2d1 3811{
fe094d98 3812 switch (ioprio_class) {
c2dea2d1 3813 case IOPRIO_CLASS_RT:
60a83707 3814 return &cfqg->async_cfqq[0][ioprio];
598971bf
TH
3815 case IOPRIO_CLASS_NONE:
3816 ioprio = IOPRIO_NORM;
3817 /* fall through */
c2dea2d1 3818 case IOPRIO_CLASS_BE:
60a83707 3819 return &cfqg->async_cfqq[1][ioprio];
c2dea2d1 3820 case IOPRIO_CLASS_IDLE:
60a83707 3821 return &cfqg->async_idle_cfqq;
c2dea2d1
VT
3822 default:
3823 BUG();
3824 }
3825}
3826
15c31be4 3827static struct cfq_queue *
abede6da 3828cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
2da8de0b 3829 struct bio *bio)
15c31be4 3830{
c6ce1943
JM
3831 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3832 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
d4aad7ff 3833 struct cfq_queue **async_cfqq = NULL;
4ebc1c61 3834 struct cfq_queue *cfqq;
322731ed
TH
3835 struct cfq_group *cfqg;
3836
3837 rcu_read_lock();
ae118896 3838 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
322731ed
TH
3839 if (!cfqg) {
3840 cfqq = &cfqd->oom_cfqq;
3841 goto out;
3842 }
15c31be4 3843
c2dea2d1 3844 if (!is_sync) {
c6ce1943
JM
3845 if (!ioprio_valid(cic->ioprio)) {
3846 struct task_struct *tsk = current;
3847 ioprio = task_nice_ioprio(tsk);
3848 ioprio_class = task_nice_ioclass(tsk);
3849 }
60a83707 3850 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
c2dea2d1 3851 cfqq = *async_cfqq;
4ebc1c61
TH
3852 if (cfqq)
3853 goto out;
c2dea2d1
VT
3854 }
3855
e00f4f4d
TH
3856 cfqq = kmem_cache_alloc_node(cfq_pool,
3857 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
d4aad7ff
TH
3858 cfqd->queue->node);
3859 if (!cfqq) {
3860 cfqq = &cfqd->oom_cfqq;
3861 goto out;
3862 }
3863
4d608baa
AP
3864 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3865 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
d4aad7ff
TH
3866 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3867 cfq_init_prio_data(cfqq, cic);
3868 cfq_link_cfqq_cfqg(cfqq, cfqg);
3869 cfq_log_cfqq(cfqd, cfqq, "alloced");
15c31be4 3870
d4aad7ff
TH
3871 if (async_cfqq) {
3872 /* a new async queue is created, pin and remember */
30d7b944 3873 cfqq->ref++;
c2dea2d1 3874 *async_cfqq = cfqq;
15c31be4 3875 }
4ebc1c61 3876out:
30d7b944 3877 cfqq->ref++;
322731ed 3878 rcu_read_unlock();
15c31be4
JA
3879 return cfqq;
3880}
3881
22e2c507 3882static void
9a7f38c4 3883__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
1da177e4 3884{
9a7f38c4 3885 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
383cd721 3886 elapsed = min(elapsed, 2UL * slice_idle);
db3b5848 3887
383cd721 3888 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
9a7f38c4
JM
3889 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3890 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3891 ttime->ttime_samples);
383cd721
SL
3892}
3893
3894static void
3895cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c5869807 3896 struct cfq_io_cq *cic)
383cd721 3897{
f5f2b6ce 3898 if (cfq_cfqq_sync(cfqq)) {
383cd721 3899 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
f5f2b6ce
SL
3900 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3901 cfqd->cfq_slice_idle);
3902 }
7700fc4f
SL
3903#ifdef CONFIG_CFQ_GROUP_IOSCHED
3904 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3905#endif
22e2c507 3906}
1da177e4 3907
206dc69b 3908static void
b2c18e1e 3909cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
6d048f53 3910 struct request *rq)
206dc69b 3911{
3dde36dd 3912 sector_t sdist = 0;
41647e7a 3913 sector_t n_sec = blk_rq_sectors(rq);
3dde36dd
CZ
3914 if (cfqq->last_request_pos) {
3915 if (cfqq->last_request_pos < blk_rq_pos(rq))
3916 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3917 else
3918 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3919 }
206dc69b 3920
3dde36dd 3921 cfqq->seek_history <<= 1;
41647e7a
CZ
3922 if (blk_queue_nonrot(cfqd->queue))
3923 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3924 else
3925 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
206dc69b 3926}
1da177e4 3927
a2b80967
CH
3928static inline bool req_noidle(struct request *req)
3929{
3930 return req_op(req) == REQ_OP_WRITE &&
3931 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3932}
3933
22e2c507
JA
3934/*
3935 * Disable idle window if the process thinks too long or seeks so much that
3936 * it doesn't matter
3937 */
3938static void
3939cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
c5869807 3940 struct cfq_io_cq *cic)
22e2c507 3941{
7b679138 3942 int old_idle, enable_idle;
1be92f2f 3943
0871714e
JA
3944 /*
3945 * Don't idle for async or idle io prio class
3946 */
3947 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
1be92f2f
JA
3948 return;
3949
c265a7f4 3950 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
1da177e4 3951
76280aff
CZ
3952 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3953 cfq_mark_cfqq_deep(cfqq);
3954
a2b80967 3955 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
749ef9f8 3956 enable_idle = 0;
f6e8d01b 3957 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
c5869807
TH
3958 !cfqd->cfq_slice_idle ||
3959 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
22e2c507 3960 enable_idle = 0;
383cd721
SL
3961 else if (sample_valid(cic->ttime.ttime_samples)) {
3962 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
22e2c507
JA
3963 enable_idle = 0;
3964 else
3965 enable_idle = 1;
1da177e4
LT
3966 }
3967
7b679138
JA
3968 if (old_idle != enable_idle) {
3969 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3970 if (enable_idle)
3971 cfq_mark_cfqq_idle_window(cfqq);
3972 else
3973 cfq_clear_cfqq_idle_window(cfqq);
3974 }
22e2c507 3975}
1da177e4 3976
22e2c507
JA
3977/*
3978 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3979 * no or if we aren't sure, a 1 will cause a preempt.
3980 */
a6151c3a 3981static bool
22e2c507 3982cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
5e705374 3983 struct request *rq)
22e2c507 3984{
6d048f53 3985 struct cfq_queue *cfqq;
22e2c507 3986
6d048f53
JA
3987 cfqq = cfqd->active_queue;
3988 if (!cfqq)
a6151c3a 3989 return false;
22e2c507 3990
6d048f53 3991 if (cfq_class_idle(new_cfqq))
a6151c3a 3992 return false;
22e2c507
JA
3993
3994 if (cfq_class_idle(cfqq))
a6151c3a 3995 return true;
1e3335de 3996
875feb63
DS
3997 /*
3998 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3999 */
4000 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
4001 return false;
4002
374f84ac
JA
4003 /*
4004 * if the new request is sync, but the currently running queue is
4005 * not, let the sync request have priority.
4006 */
3932a86b 4007 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
a6151c3a 4008 return true;
1e3335de 4009
3984aa55
JK
4010 /*
4011 * Treat ancestors of current cgroup the same way as current cgroup.
4012 * For anybody else we disallow preemption to guarantee service
4013 * fairness among cgroups.
4014 */
4015 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
8682e1f1
VG
4016 return false;
4017
4018 if (cfq_slice_used(cfqq))
4019 return true;
4020
6c80731c
JK
4021 /*
4022 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4023 */
4024 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4025 return true;
4026
4027 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
8682e1f1 4028 /* Allow preemption only if we are idling on sync-noidle tree */
4d2ceea4 4029 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
8682e1f1 4030 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
8682e1f1
VG
4031 RB_EMPTY_ROOT(&cfqq->sort_list))
4032 return true;
4033
b53d1ed7
JA
4034 /*
4035 * So both queues are sync. Let the new request get disk time if
4036 * it's a metadata request and the current queue is doing regular IO.
4037 */
65299a3b 4038 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
b53d1ed7
JA
4039 return true;
4040
d2d59e18
SL
4041 /* An idle queue should not be idle now for some reason */
4042 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4043 return true;
4044
1e3335de 4045 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
a6151c3a 4046 return false;
1e3335de
JA
4047
4048 /*
4049 * if this request is as-good as one we would expect from the
4050 * current cfqq, let it preempt
4051 */
e9ce335d 4052 if (cfq_rq_close(cfqd, cfqq, rq))
a6151c3a 4053 return true;
1e3335de 4054
a6151c3a 4055 return false;
22e2c507
JA
4056}
4057
4058/*
4059 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4060 * let it have half of its nominal slice.
4061 */
4062static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4063{
df0793ab
SL
4064 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4065
7b679138 4066 cfq_log_cfqq(cfqd, cfqq, "preempt");
df0793ab 4067 cfq_slice_expired(cfqd, 1);
22e2c507 4068
f8ae6e3e
SL
4069 /*
4070 * workload type is changed, don't save slice, otherwise preempt
4071 * doesn't happen
4072 */
df0793ab 4073 if (old_type != cfqq_type(cfqq))
4d2ceea4 4074 cfqq->cfqg->saved_wl_slice = 0;
f8ae6e3e 4075
bf572256
JA
4076 /*
4077 * Put the new queue at the front of the of the current list,
4078 * so we know that it will be selected next.
4079 */
4080 BUG_ON(!cfq_cfqq_on_rr(cfqq));
edd75ffd
JA
4081
4082 cfq_service_tree_add(cfqd, cfqq, 1);
eda5e0c9 4083
62a37f6b
JT
4084 cfqq->slice_end = 0;
4085 cfq_mark_cfqq_slice_new(cfqq);
22e2c507
JA
4086}
4087
22e2c507 4088/*
5e705374 4089 * Called when a new fs request (rq) is added (to cfqq). Check if there's
22e2c507
JA
4090 * something we should do about it
4091 */
4092static void
5e705374
JA
4093cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4094 struct request *rq)
22e2c507 4095{
c5869807 4096 struct cfq_io_cq *cic = RQ_CIC(rq);
12e9fddd 4097
45333d5a 4098 cfqd->rq_queued++;
65299a3b
CH
4099 if (rq->cmd_flags & REQ_PRIO)
4100 cfqq->prio_pending++;
374f84ac 4101
383cd721 4102 cfq_update_io_thinktime(cfqd, cfqq, cic);
b2c18e1e 4103 cfq_update_io_seektime(cfqd, cfqq, rq);
9c2c38a1
JA
4104 cfq_update_idle_window(cfqd, cfqq, cic);
4105
b2c18e1e 4106 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
22e2c507
JA
4107
4108 if (cfqq == cfqd->active_queue) {
4109 /*
b029195d
JA
4110 * Remember that we saw a request from this process, but
4111 * don't start queuing just yet. Otherwise we risk seeing lots
4112 * of tiny requests, because we disrupt the normal plugging
d6ceb25e
JA
4113 * and merging. If the request is already larger than a single
4114 * page, let it rip immediately. For that case we assume that
2d870722
JA
4115 * merging is already done. Ditto for a busy system that
4116 * has other work pending, don't risk delaying until the
4117 * idle timer unplug to continue working.
22e2c507 4118 */
d6ceb25e 4119 if (cfq_cfqq_wait_request(cfqq)) {
09cbfeaf 4120 if (blk_rq_bytes(rq) > PAGE_SIZE ||
2d870722 4121 cfqd->busy_queues > 1) {
812df48d 4122 cfq_del_timer(cfqd, cfqq);
554554f6 4123 cfq_clear_cfqq_wait_request(cfqq);
24ecfbe2 4124 __blk_run_queue(cfqd->queue);
a11cdaa7 4125 } else {
155fead9 4126 cfqg_stats_update_idle_time(cfqq->cfqg);
bf791937 4127 cfq_mark_cfqq_must_dispatch(cfqq);
a11cdaa7 4128 }
d6ceb25e 4129 }
5e705374 4130 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
22e2c507
JA
4131 /*
4132 * not the active queue - expire current slice if it is
4133 * idle and has expired it's mean thinktime or this new queue
3a9a3f6c
DS
4134 * has some old slice time left and is of higher priority or
4135 * this new queue is RT and the current one is BE
22e2c507
JA
4136 */
4137 cfq_preempt_queue(cfqd, cfqq);
24ecfbe2 4138 __blk_run_queue(cfqd->queue);
22e2c507 4139 }
1da177e4
LT
4140}
4141
165125e1 4142static void cfq_insert_request(struct request_queue *q, struct request *rq)
1da177e4 4143{
b4878f24 4144 struct cfq_data *cfqd = q->elevator->elevator_data;
5e705374 4145 struct cfq_queue *cfqq = RQ_CFQQ(rq);
22e2c507 4146
7b679138 4147 cfq_log_cfqq(cfqd, cfqq, "insert_request");
abede6da 4148 cfq_init_prio_data(cfqq, RQ_CIC(rq));
1da177e4 4149
9a7f38c4 4150 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
22e2c507 4151 list_add_tail(&rq->queuelist, &cfqq->fifo);
aa6f6a3d 4152 cfq_add_rq_rb(rq);
ef295ecf 4153 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
155fead9 4154 rq->cmd_flags);
5e705374 4155 cfq_rq_enqueued(cfqd, cfqq, rq);
1da177e4
LT
4156}
4157
45333d5a
AC
4158/*
4159 * Update hw_tag based on peak queue depth over 50 samples under
4160 * sufficient load.
4161 */
4162static void cfq_update_hw_tag(struct cfq_data *cfqd)
4163{
1a1238a7
SL
4164 struct cfq_queue *cfqq = cfqd->active_queue;
4165
53c583d2
CZ
4166 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4167 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
e459dd08
CZ
4168
4169 if (cfqd->hw_tag == 1)
4170 return;
45333d5a
AC
4171
4172 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
53c583d2 4173 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
45333d5a
AC
4174 return;
4175
1a1238a7
SL
4176 /*
4177 * If active queue hasn't enough requests and can idle, cfq might not
4178 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4179 * case
4180 */
4181 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4182 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
53c583d2 4183 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
1a1238a7
SL
4184 return;
4185
45333d5a
AC
4186 if (cfqd->hw_tag_samples++ < 50)
4187 return;
4188
e459dd08 4189 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
45333d5a
AC
4190 cfqd->hw_tag = 1;
4191 else
4192 cfqd->hw_tag = 0;
45333d5a
AC
4193}
4194
7667aa06
VG
4195static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4196{
c5869807 4197 struct cfq_io_cq *cic = cfqd->active_cic;
9a7f38c4 4198 u64 now = ktime_get_ns();
7667aa06 4199
02a8f01b
JT
4200 /* If the queue already has requests, don't wait */
4201 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4202 return false;
4203
7667aa06
VG
4204 /* If there are other queues in the group, don't wait */
4205 if (cfqq->cfqg->nr_cfqq > 1)
4206 return false;
4207
7700fc4f
SL
4208 /* the only queue in the group, but think time is big */
4209 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4210 return false;
4211
7667aa06
VG
4212 if (cfq_slice_used(cfqq))
4213 return true;
4214
4215 /* if slice left is less than think time, wait busy */
383cd721 4216 if (cic && sample_valid(cic->ttime.ttime_samples)
9a7f38c4 4217 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
7667aa06
VG
4218 return true;
4219
4220 /*
4221 * If think times is less than a jiffy than ttime_mean=0 and above
4222 * will not be true. It might happen that slice has not expired yet
4223 * but will expire soon (4-5 ns) during select_queue(). To cover the
4224 * case where think time is less than a jiffy, mark the queue wait
4225 * busy if only 1 jiffy is left in the slice.
4226 */
9a7f38c4 4227 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
7667aa06
VG
4228 return true;
4229
4230 return false;
4231}
4232
165125e1 4233static void cfq_completed_request(struct request_queue *q, struct request *rq)
1da177e4 4234{
5e705374 4235 struct cfq_queue *cfqq = RQ_CFQQ(rq);
b4878f24 4236 struct cfq_data *cfqd = cfqq->cfqd;
5380a101 4237 const int sync = rq_is_sync(rq);
9a7f38c4 4238 u64 now = ktime_get_ns();
1da177e4 4239
a2b80967 4240 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
1da177e4 4241
45333d5a
AC
4242 cfq_update_hw_tag(cfqd);
4243
53c583d2 4244 WARN_ON(!cfqd->rq_in_driver);
6d048f53 4245 WARN_ON(!cfqq->dispatched);
53c583d2 4246 cfqd->rq_in_driver--;
6d048f53 4247 cfqq->dispatched--;
80bdf0c7 4248 (RQ_CFQG(rq))->dispatched--;
155fead9 4249 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
ef295ecf 4250 rq_io_start_time_ns(rq), rq->cmd_flags);
1da177e4 4251
53c583d2 4252 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
3ed9a296 4253
365722bb 4254 if (sync) {
34b98d03 4255 struct cfq_rb_root *st;
f5f2b6ce 4256
383cd721 4257 RQ_CIC(rq)->ttime.last_end_request = now;
f5f2b6ce
SL
4258
4259 if (cfq_cfqq_on_rr(cfqq))
34b98d03 4260 st = cfqq->service_tree;
f5f2b6ce 4261 else
34b98d03
VG
4262 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4263 cfqq_type(cfqq));
4264
4265 st->ttime.last_end_request = now;
149321a6
JK
4266 /*
4267 * We have to do this check in jiffies since start_time is in
4268 * jiffies and it is not trivial to convert to ns. If
4269 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
4270 * will become problematic but so far we are fine (the default
4271 * is 128 ms).
4272 */
4273 if (!time_after(rq->start_time +
4274 nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
4275 jiffies))
573412b2 4276 cfqd->last_delayed_sync = now;
365722bb 4277 }
caaa5f9f 4278
7700fc4f
SL
4279#ifdef CONFIG_CFQ_GROUP_IOSCHED
4280 cfqq->cfqg->ttime.last_end_request = now;
4281#endif
4282
caaa5f9f
JA
4283 /*
4284 * If this is the active queue, check if it needs to be expired,
4285 * or if we want to idle in case it has no pending requests.
4286 */
4287 if (cfqd->active_queue == cfqq) {
a36e71f9
JA
4288 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4289
44f7c160
JA
4290 if (cfq_cfqq_slice_new(cfqq)) {
4291 cfq_set_prio_slice(cfqd, cfqq);
4292 cfq_clear_cfqq_slice_new(cfqq);
4293 }
f75edf2d
VG
4294
4295 /*
7667aa06
VG
4296 * Should we wait for next request to come in before we expire
4297 * the queue.
f75edf2d 4298 */
7667aa06 4299 if (cfq_should_wait_busy(cfqd, cfqq)) {
9a7f38c4 4300 u64 extend_sl = cfqd->cfq_slice_idle;
80bdf0c7
VG
4301 if (!cfqd->cfq_slice_idle)
4302 extend_sl = cfqd->cfq_group_idle;
9a7f38c4 4303 cfqq->slice_end = now + extend_sl;
f75edf2d 4304 cfq_mark_cfqq_wait_busy(cfqq);
b1ffe737 4305 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
f75edf2d
VG
4306 }
4307
a36e71f9 4308 /*
8e550632
CZ
4309 * Idling is not enabled on:
4310 * - expired queues
4311 * - idle-priority queues
4312 * - async queues
4313 * - queues with still some requests queued
4314 * - when there is a close cooperator
a36e71f9 4315 */
0871714e 4316 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
e5ff082e 4317 cfq_slice_expired(cfqd, 1);
8e550632
CZ
4318 else if (sync && cfqq_empty &&
4319 !cfq_close_cooperator(cfqd, cfqq)) {
749ef9f8 4320 cfq_arm_slice_timer(cfqd);
8e550632 4321 }
caaa5f9f 4322 }
6d048f53 4323
53c583d2 4324 if (!cfqd->rq_in_driver)
23e018a1 4325 cfq_schedule_dispatch(cfqd);
1da177e4
LT
4326}
4327
ef295ecf 4328static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
b8269db4
JA
4329{
4330 /*
4331 * If REQ_PRIO is set, boost class and prio level, if it's below
4332 * BE/NORM. If prio is not set, restore the potentially boosted
4333 * class/prio level.
4334 */
ef295ecf 4335 if (!(op & REQ_PRIO)) {
b8269db4
JA
4336 cfqq->ioprio_class = cfqq->org_ioprio_class;
4337 cfqq->ioprio = cfqq->org_ioprio;
4338 } else {
4339 if (cfq_class_idle(cfqq))
4340 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4341 if (cfqq->ioprio > IOPRIO_NORM)
4342 cfqq->ioprio = IOPRIO_NORM;
4343 }
4344}
4345
89850f7e 4346static inline int __cfq_may_queue(struct cfq_queue *cfqq)
22e2c507 4347{
1b379d8d 4348 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
3b18152c 4349 cfq_mark_cfqq_must_alloc_slice(cfqq);
22e2c507 4350 return ELV_MQUEUE_MUST;
3b18152c 4351 }
1da177e4 4352
22e2c507 4353 return ELV_MQUEUE_MAY;
22e2c507
JA
4354}
4355
ef295ecf 4356static int cfq_may_queue(struct request_queue *q, unsigned int op)
22e2c507
JA
4357{
4358 struct cfq_data *cfqd = q->elevator->elevator_data;
4359 struct task_struct *tsk = current;
c5869807 4360 struct cfq_io_cq *cic;
22e2c507
JA
4361 struct cfq_queue *cfqq;
4362
4363 /*
4364 * don't force setup of a queue from here, as a call to may_queue
4365 * does not necessarily imply that a request actually will be queued.
4366 * so just lookup a possibly existing queue, or return 'may queue'
4367 * if that fails
4368 */
4ac845a2 4369 cic = cfq_cic_lookup(cfqd, tsk->io_context);
91fac317
VT
4370 if (!cic)
4371 return ELV_MQUEUE_MAY;
4372
ef295ecf 4373 cfqq = cic_to_cfqq(cic, op_is_sync(op));
22e2c507 4374 if (cfqq) {
abede6da 4375 cfq_init_prio_data(cfqq, cic);
ef295ecf 4376 cfqq_boost_on_prio(cfqq, op);
22e2c507 4377
89850f7e 4378 return __cfq_may_queue(cfqq);
22e2c507
JA
4379 }
4380
4381 return ELV_MQUEUE_MAY;
1da177e4
LT
4382}
4383
1da177e4
LT
4384/*
4385 * queue lock held here
4386 */
bb37b94c 4387static void cfq_put_request(struct request *rq)
1da177e4 4388{
5e705374 4389 struct cfq_queue *cfqq = RQ_CFQQ(rq);
1da177e4 4390
5e705374 4391 if (cfqq) {
22e2c507 4392 const int rw = rq_data_dir(rq);
1da177e4 4393
22e2c507
JA
4394 BUG_ON(!cfqq->allocated[rw]);
4395 cfqq->allocated[rw]--;
1da177e4 4396
7f1dc8a2 4397 /* Put down rq reference on cfqg */
eb7d8c07 4398 cfqg_put(RQ_CFQG(rq));
a612fddf
TH
4399 rq->elv.priv[0] = NULL;
4400 rq->elv.priv[1] = NULL;
7f1dc8a2 4401
1da177e4
LT
4402 cfq_put_queue(cfqq);
4403 }
4404}
4405
df5fe3e8 4406static struct cfq_queue *
c5869807 4407cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
df5fe3e8
JM
4408 struct cfq_queue *cfqq)
4409{
4410 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4411 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
b3b6d040 4412 cfq_mark_cfqq_coop(cfqq->new_cfqq);
df5fe3e8
JM
4413 cfq_put_queue(cfqq);
4414 return cic_to_cfqq(cic, 1);
4415}
4416
e6c5bc73
JM
4417/*
4418 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4419 * was the last process referring to said cfqq.
4420 */
4421static struct cfq_queue *
c5869807 4422split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
e6c5bc73
JM
4423{
4424 if (cfqq_process_refs(cfqq) == 1) {
e6c5bc73
JM
4425 cfqq->pid = current->pid;
4426 cfq_clear_cfqq_coop(cfqq);
ae54abed 4427 cfq_clear_cfqq_split_coop(cfqq);
e6c5bc73
JM
4428 return cfqq;
4429 }
4430
4431 cic_set_cfqq(cic, NULL, 1);
d02a2c07
SL
4432
4433 cfq_put_cooperator(cfqq);
4434
e6c5bc73
JM
4435 cfq_put_queue(cfqq);
4436 return NULL;
4437}
1da177e4 4438/*
22e2c507 4439 * Allocate cfq data structures associated with this request.
1da177e4 4440 */
22e2c507 4441static int
852c788f
TH
4442cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4443 gfp_t gfp_mask)
1da177e4
LT
4444{
4445 struct cfq_data *cfqd = q->elevator->elevator_data;
f1f8cc94 4446 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
1da177e4 4447 const int rw = rq_data_dir(rq);
a6151c3a 4448 const bool is_sync = rq_is_sync(rq);
22e2c507 4449 struct cfq_queue *cfqq;
1da177e4 4450
216284c3 4451 spin_lock_irq(q->queue_lock);
f1f8cc94 4452
598971bf 4453 check_ioprio_changed(cic, bio);
142bbdfc 4454 check_blkcg_changed(cic, bio);
e6c5bc73 4455new_queue:
91fac317 4456 cfqq = cic_to_cfqq(cic, is_sync);
32f2e807 4457 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
bce6133b
TH
4458 if (cfqq)
4459 cfq_put_queue(cfqq);
2da8de0b 4460 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
91fac317 4461 cic_set_cfqq(cic, cfqq, is_sync);
df5fe3e8 4462 } else {
e6c5bc73
JM
4463 /*
4464 * If the queue was seeky for too long, break it apart.
4465 */
ae54abed 4466 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
e6c5bc73
JM
4467 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4468 cfqq = split_cfqq(cic, cfqq);
4469 if (!cfqq)
4470 goto new_queue;
4471 }
4472
df5fe3e8
JM
4473 /*
4474 * Check to see if this queue is scheduled to merge with
4475 * another, closely cooperating queue. The merging of
4476 * queues happens here as it must be done in process context.
4477 * The reference on new_cfqq was taken in merge_cfqqs.
4478 */
4479 if (cfqq->new_cfqq)
4480 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
91fac317 4481 }
1da177e4
LT
4482
4483 cfqq->allocated[rw]++;
1da177e4 4484
6fae9c25 4485 cfqq->ref++;
eb7d8c07 4486 cfqg_get(cfqq->cfqg);
a612fddf 4487 rq->elv.priv[0] = cfqq;
1adaf3dd 4488 rq->elv.priv[1] = cfqq->cfqg;
216284c3 4489 spin_unlock_irq(q->queue_lock);
5d7f5ce1 4490
5e705374 4491 return 0;
1da177e4
LT
4492}
4493
65f27f38 4494static void cfq_kick_queue(struct work_struct *work)
22e2c507 4495{
65f27f38 4496 struct cfq_data *cfqd =
23e018a1 4497 container_of(work, struct cfq_data, unplug_work);
165125e1 4498 struct request_queue *q = cfqd->queue;
22e2c507 4499
40bb54d1 4500 spin_lock_irq(q->queue_lock);
24ecfbe2 4501 __blk_run_queue(cfqd->queue);
40bb54d1 4502 spin_unlock_irq(q->queue_lock);
22e2c507
JA
4503}
4504
4505/*
4506 * Timer running if the active_queue is currently idling inside its time slice
4507 */
91148325 4508static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
22e2c507 4509{
91148325
JK
4510 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4511 idle_slice_timer);
22e2c507
JA
4512 struct cfq_queue *cfqq;
4513 unsigned long flags;
3c6bd2f8 4514 int timed_out = 1;
22e2c507 4515
7b679138
JA
4516 cfq_log(cfqd, "idle timer fired");
4517
22e2c507
JA
4518 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4519
fe094d98
JA
4520 cfqq = cfqd->active_queue;
4521 if (cfqq) {
3c6bd2f8
JA
4522 timed_out = 0;
4523
b029195d
JA
4524 /*
4525 * We saw a request before the queue expired, let it through
4526 */
4527 if (cfq_cfqq_must_dispatch(cfqq))
4528 goto out_kick;
4529
22e2c507
JA
4530 /*
4531 * expired
4532 */
44f7c160 4533 if (cfq_slice_used(cfqq))
22e2c507
JA
4534 goto expire;
4535
4536 /*
4537 * only expire and reinvoke request handler, if there are
4538 * other queues with pending requests
4539 */
caaa5f9f 4540 if (!cfqd->busy_queues)
22e2c507 4541 goto out_cont;
22e2c507
JA
4542
4543 /*
4544 * not expired and it has a request pending, let it dispatch
4545 */
75e50984 4546 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
22e2c507 4547 goto out_kick;
76280aff
CZ
4548
4549 /*
4550 * Queue depth flag is reset only when the idle didn't succeed
4551 */
4552 cfq_clear_cfqq_deep(cfqq);
22e2c507
JA
4553 }
4554expire:
e5ff082e 4555 cfq_slice_expired(cfqd, timed_out);
22e2c507 4556out_kick:
23e018a1 4557 cfq_schedule_dispatch(cfqd);
22e2c507
JA
4558out_cont:
4559 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
91148325 4560 return HRTIMER_NORESTART;
22e2c507
JA
4561}
4562
3b18152c
JA
4563static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4564{
91148325 4565 hrtimer_cancel(&cfqd->idle_slice_timer);
23e018a1 4566 cancel_work_sync(&cfqd->unplug_work);
3b18152c 4567}
22e2c507 4568
b374d18a 4569static void cfq_exit_queue(struct elevator_queue *e)
1da177e4 4570{
22e2c507 4571 struct cfq_data *cfqd = e->elevator_data;
165125e1 4572 struct request_queue *q = cfqd->queue;
22e2c507 4573
3b18152c 4574 cfq_shutdown_timer_wq(cfqd);
e2d74ac0 4575
d9ff4187 4576 spin_lock_irq(q->queue_lock);
e2d74ac0 4577
d9ff4187 4578 if (cfqd->active_queue)
e5ff082e 4579 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
e2d74ac0 4580
03aa264a
TH
4581 spin_unlock_irq(q->queue_lock);
4582
a90d742e
AV
4583 cfq_shutdown_timer_wq(cfqd);
4584
ffea73fc
TH
4585#ifdef CONFIG_CFQ_GROUP_IOSCHED
4586 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4587#else
f51b802c 4588 kfree(cfqd->root_group);
2abae55f 4589#endif
56edf7d7 4590 kfree(cfqd);
1da177e4
LT
4591}
4592
d50235b7 4593static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
1da177e4
LT
4594{
4595 struct cfq_data *cfqd;
3c798398 4596 struct blkcg_gq *blkg __maybe_unused;
a2b1693b 4597 int i, ret;
d50235b7
JM
4598 struct elevator_queue *eq;
4599
4600 eq = elevator_alloc(q, e);
4601 if (!eq)
4602 return -ENOMEM;
1da177e4 4603
c1b511eb 4604 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
d50235b7
JM
4605 if (!cfqd) {
4606 kobject_put(&eq->kobj);
b2fab5ac 4607 return -ENOMEM;
d50235b7
JM
4608 }
4609 eq->elevator_data = cfqd;
80b15c73 4610
f51b802c 4611 cfqd->queue = q;
d50235b7
JM
4612 spin_lock_irq(q->queue_lock);
4613 q->elevator = eq;
4614 spin_unlock_irq(q->queue_lock);
f51b802c 4615
1fa8f6d6
VG
4616 /* Init root service tree */
4617 cfqd->grp_service_tree = CFQ_RB_ROOT;
4618
f51b802c 4619 /* Init root group and prefer root group over other groups by default */
25fb5169 4620#ifdef CONFIG_CFQ_GROUP_IOSCHED
3c798398 4621 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
a2b1693b
TH
4622 if (ret)
4623 goto out_free;
f51b802c 4624
a2b1693b 4625 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
f51b802c 4626#else
a2b1693b 4627 ret = -ENOMEM;
f51b802c
TH
4628 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4629 GFP_KERNEL, cfqd->queue->node);
a2b1693b
TH
4630 if (!cfqd->root_group)
4631 goto out_free;
5624a4e4 4632
a2b1693b 4633 cfq_init_cfqg_base(cfqd->root_group);
3ecca629
TH
4634 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4635 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
69d7fde5 4636#endif
5624a4e4 4637
26a2ac00
JA
4638 /*
4639 * Not strictly needed (since RB_ROOT just clears the node and we
4640 * zeroed cfqd on alloc), but better be safe in case someone decides
4641 * to add magic to the rb code
4642 */
4643 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4644 cfqd->prio_trees[i] = RB_ROOT;
4645
6118b70b 4646 /*
d4aad7ff 4647 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
6118b70b 4648 * Grab a permanent reference to it, so that the normal code flow
f51b802c
TH
4649 * will not attempt to free it. oom_cfqq is linked to root_group
4650 * but shouldn't hold a reference as it'll never be unlinked. Lose
4651 * the reference from linking right away.
6118b70b
JA
4652 */
4653 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
30d7b944 4654 cfqd->oom_cfqq.ref++;
1adaf3dd
TH
4655
4656 spin_lock_irq(q->queue_lock);
f51b802c 4657 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
eb7d8c07 4658 cfqg_put(cfqd->root_group);
1adaf3dd 4659 spin_unlock_irq(q->queue_lock);
1da177e4 4660
91148325
JK
4661 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4662 HRTIMER_MODE_REL);
22e2c507 4663 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
22e2c507 4664
23e018a1 4665 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
22e2c507 4666
1da177e4 4667 cfqd->cfq_quantum = cfq_quantum;
22e2c507
JA
4668 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4669 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
1da177e4
LT
4670 cfqd->cfq_back_max = cfq_back_max;
4671 cfqd->cfq_back_penalty = cfq_back_penalty;
22e2c507
JA
4672 cfqd->cfq_slice[0] = cfq_slice_async;
4673 cfqd->cfq_slice[1] = cfq_slice_sync;
5bf14c07 4674 cfqd->cfq_target_latency = cfq_target_latency;
22e2c507 4675 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
0bb97947 4676 cfqd->cfq_slice_idle = cfq_slice_idle;
80bdf0c7 4677 cfqd->cfq_group_idle = cfq_group_idle;
963b72fc 4678 cfqd->cfq_latency = 1;
e459dd08 4679 cfqd->hw_tag = -1;
edc71131
CZ
4680 /*
4681 * we optimistically start assuming sync ops weren't delayed in last
4682 * second, in order to have larger depth for async operations.
4683 */
9a7f38c4 4684 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
b2fab5ac 4685 return 0;
a2b1693b
TH
4686
4687out_free:
4688 kfree(cfqd);
d50235b7 4689 kobject_put(&eq->kobj);
a2b1693b 4690 return ret;
1da177e4
LT
4691}
4692
0bb97947
JA
4693static void cfq_registered_queue(struct request_queue *q)
4694{
4695 struct elevator_queue *e = q->elevator;
4696 struct cfq_data *cfqd = e->elevator_data;
4697
4698 /*
4699 * Default to IOPS mode with no idling for SSDs
4700 */
4701 if (blk_queue_nonrot(q))
4702 cfqd->cfq_slice_idle = 0;
142bbdfc 4703 wbt_disable_default(q);
0bb97947
JA
4704}
4705
1da177e4
LT
4706/*
4707 * sysfs parts below -->
4708 */
1da177e4
LT
4709static ssize_t
4710cfq_var_show(unsigned int var, char *page)
4711{
176167ad 4712 return sprintf(page, "%u\n", var);
1da177e4
LT
4713}
4714
235f8da1 4715static void
4716cfq_var_store(unsigned int *var, const char *page)
1da177e4
LT
4717{
4718 char *p = (char *) page;
4719
4720 *var = simple_strtoul(p, &p, 10);
1da177e4
LT
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 4765 unsigned int __data; \
235f8da1 4766 cfq_var_store(&__data, (page)); \
1da177e4
LT
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; \
235f8da1 4775 return count; \
1da177e4
LT
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; \
235f8da1 4800 cfq_var_store(&__data, (page)); \
d2d481d0
JM
4801 if (__data < (MIN)) \
4802 __data = (MIN); \
4803 else if (__data > (MAX)) \
4804 __data = (MAX); \
4805 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
235f8da1 4806 return count; \
d2d481d0
JM
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");