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