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