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