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