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