Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 7 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 11 | #include <linux/sched/clock.h> |
Al Viro | 1cc9be6 | 2006-03-18 12:29:52 -0500 | [diff] [blame] | 12 | #include <linux/blkdev.h> |
| 13 | #include <linux/elevator.h> |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 14 | #include <linux/ktime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/rbtree.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 16 | #include <linux/ioprio.h> |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 17 | #include <linux/blktrace_api.h> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 18 | #include <linux/blk-cgroup.h> |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 19 | #include "blk.h" |
Jens Axboe | 87760e5 | 2016-11-09 12:38:14 -0700 | [diff] [blame] | 20 | #include "blk-wbt.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * tunables |
| 24 | */ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 25 | /* max queue in one round of service */ |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 26 | static const int cfq_quantum = 8; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 27 | static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 }; |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 28 | /* maximum backwards seek, in KiB */ |
| 29 | static const int cfq_back_max = 16 * 1024; |
| 30 | /* penalty of a backwards seek */ |
| 31 | static const int cfq_back_penalty = 2; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 32 | static const u64 cfq_slice_sync = NSEC_PER_SEC / 10; |
| 33 | static u64 cfq_slice_async = NSEC_PER_SEC / 25; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 34 | static const int cfq_slice_async_rq = 2; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 35 | static u64 cfq_slice_idle = NSEC_PER_SEC / 125; |
| 36 | static u64 cfq_group_idle = NSEC_PER_SEC / 125; |
| 37 | static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */ |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 38 | static const int cfq_hist_divisor = 4; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 39 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 40 | /* |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 41 | * offset from end of service tree |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 42 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 43 | #define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * below this threshold, we consider thinktime immediate |
| 47 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 48 | #define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 49 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 50 | #define CFQ_SLICE_SCALE (5) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 51 | #define CFQ_HW_QUEUE_MIN (5) |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 52 | #define CFQ_SERVICE_SHIFT 12 |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 53 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 54 | #define CFQQ_SEEK_THR (sector_t)(8 * 100) |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 55 | #define CFQQ_CLOSE_THR (sector_t)(8 * 1024) |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 56 | #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32) |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 57 | #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8) |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 58 | |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 59 | #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq) |
| 60 | #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0]) |
| 61 | #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 63 | static struct kmem_cache *cfq_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 65 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
| 66 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 67 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
| 68 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 69 | #define sample_valid(samples) ((samples) > 80) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 70 | #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 71 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 72 | /* blkio-related constants */ |
Tejun Heo | 3ecca62 | 2015-08-18 14:55:35 -0700 | [diff] [blame] | 73 | #define CFQ_WEIGHT_LEGACY_MIN 10 |
| 74 | #define CFQ_WEIGHT_LEGACY_DFL 500 |
| 75 | #define CFQ_WEIGHT_LEGACY_MAX 1000 |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 76 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 77 | struct cfq_ttime { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 78 | u64 last_end_request; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 79 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 80 | u64 ttime_total; |
| 81 | u64 ttime_mean; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 82 | unsigned long ttime_samples; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 83 | }; |
| 84 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 85 | /* |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 86 | * Most of our rbtree usage is for sorting with min extraction, so |
| 87 | * if we cache the leftmost node we don't have to walk down the tree |
| 88 | * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should |
| 89 | * move this into the elevator for the rq sorting as well. |
| 90 | */ |
| 91 | struct cfq_rb_root { |
| 92 | struct rb_root rb; |
| 93 | struct rb_node *left; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 94 | unsigned count; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 95 | u64 min_vdisktime; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 96 | struct cfq_ttime ttime; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 97 | }; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 98 | #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 99 | .ttime = {.last_end_request = ktime_get_ns(),},} |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 100 | |
| 101 | /* |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 102 | * Per process-grouping structure |
| 103 | */ |
| 104 | struct cfq_queue { |
| 105 | /* reference count */ |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 106 | int ref; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 107 | /* various state flags, see below */ |
| 108 | unsigned int flags; |
| 109 | /* parent cfq_data */ |
| 110 | struct cfq_data *cfqd; |
| 111 | /* service_tree member */ |
| 112 | struct rb_node rb_node; |
| 113 | /* service_tree key */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 114 | u64 rb_key; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 115 | /* prio tree member */ |
| 116 | struct rb_node p_node; |
| 117 | /* prio tree root we belong to, if any */ |
| 118 | struct rb_root *p_root; |
| 119 | /* sorted list of pending requests */ |
| 120 | struct rb_root sort_list; |
| 121 | /* if fifo isn't expired, next request to serve */ |
| 122 | struct request *next_rq; |
| 123 | /* requests queued in sort_list */ |
| 124 | int queued[2]; |
| 125 | /* currently allocated requests */ |
| 126 | int allocated[2]; |
| 127 | /* fifo list of requests in sort_list */ |
| 128 | struct list_head fifo; |
| 129 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 130 | /* time when queue got scheduled in to dispatch first request. */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 131 | u64 dispatch_start; |
| 132 | u64 allocated_slice; |
| 133 | u64 slice_dispatch; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 134 | /* time when first request from queue completed and slice started. */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 135 | u64 slice_start; |
| 136 | u64 slice_end; |
Jan Kara | 93fdf14 | 2016-06-28 09:04:00 +0200 | [diff] [blame] | 137 | s64 slice_resid; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 138 | |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 139 | /* pending priority requests */ |
| 140 | int prio_pending; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 141 | /* number of requests that are on the dispatch list or inside driver */ |
| 142 | int dispatched; |
| 143 | |
| 144 | /* io prio of this group */ |
| 145 | unsigned short ioprio, org_ioprio; |
Jens Axboe | b8269db | 2016-06-09 15:47:29 -0600 | [diff] [blame] | 146 | unsigned short ioprio_class, org_ioprio_class; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 147 | |
Richard Kennedy | c4081ba | 2010-02-22 13:49:24 +0100 | [diff] [blame] | 148 | pid_t pid; |
| 149 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 150 | u32 seek_history; |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 151 | sector_t last_request_pos; |
| 152 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 153 | struct cfq_rb_root *service_tree; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 154 | struct cfq_queue *new_cfqq; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 155 | struct cfq_group *cfqg; |
Vivek Goyal | c4e7893 | 2010-08-23 12:25:03 +0200 | [diff] [blame] | 156 | /* Number of sectors dispatched from queue in single dispatch round */ |
| 157 | unsigned long nr_sectors; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 161 | * First index in the service_trees. |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 162 | * IDLE is handled separately, so it has negative index |
| 163 | */ |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 164 | enum wl_class_t { |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 165 | BE_WORKLOAD = 0, |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 166 | RT_WORKLOAD = 1, |
| 167 | IDLE_WORKLOAD = 2, |
Vivek Goyal | b462732 | 2010-10-22 09:48:43 +0200 | [diff] [blame] | 168 | CFQ_PRIO_NR, |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 172 | * Second index in the service_trees. |
| 173 | */ |
| 174 | enum wl_type_t { |
| 175 | ASYNC_WORKLOAD = 0, |
| 176 | SYNC_NOIDLE_WORKLOAD = 1, |
| 177 | SYNC_WORKLOAD = 2 |
| 178 | }; |
| 179 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 180 | struct cfqg_stats { |
| 181 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 182 | /* number of ios merged */ |
| 183 | struct blkg_rwstat merged; |
| 184 | /* total time spent on device in ns, may not be accurate w/ queueing */ |
| 185 | struct blkg_rwstat service_time; |
| 186 | /* total time spent waiting in scheduler queue in ns */ |
| 187 | struct blkg_rwstat wait_time; |
| 188 | /* number of IOs queued up */ |
| 189 | struct blkg_rwstat queued; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 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; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 203 | /* time spent idling for this blkcg_gq */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 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 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 216 | /* Per-cgroup data */ |
| 217 | struct cfq_group_data { |
| 218 | /* must be the first member */ |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 219 | struct blkcg_policy_data cpd; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 220 | |
| 221 | unsigned int weight; |
| 222 | unsigned int leaf_weight; |
| 223 | }; |
| 224 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 225 | /* This is per cgroup per device grouping structure */ |
| 226 | struct cfq_group { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 227 | /* must be the first member */ |
| 228 | struct blkg_policy_data pd; |
| 229 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 230 | /* group service_tree member */ |
| 231 | struct rb_node rb_node; |
| 232 | |
| 233 | /* group service_tree key */ |
| 234 | u64 vdisktime; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 235 | |
| 236 | /* |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 237 | * The number of active cfqgs and sum of their weights under this |
| 238 | * cfqg. This covers this cfqg's leaf_weight and all children's |
| 239 | * weights, but does not cover weights of further descendants. |
| 240 | * |
| 241 | * If a cfqg is on the service tree, it's active. An active cfqg |
| 242 | * also activates its parent and contributes to the children_weight |
| 243 | * of the parent. |
| 244 | */ |
| 245 | int nr_active; |
| 246 | unsigned int children_weight; |
| 247 | |
| 248 | /* |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 249 | * vfraction is the fraction of vdisktime that the tasks in this |
| 250 | * cfqg are entitled to. This is determined by compounding the |
| 251 | * ratios walking up from this cfqg to the root. |
| 252 | * |
| 253 | * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all |
| 254 | * vfractions on a service tree is approximately 1. The sum may |
| 255 | * deviate a bit due to rounding errors and fluctuations caused by |
| 256 | * cfqgs entering and leaving the service tree. |
| 257 | */ |
| 258 | unsigned int vfraction; |
| 259 | |
| 260 | /* |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 261 | * There are two weights - (internal) weight is the weight of this |
| 262 | * cfqg against the sibling cfqgs. leaf_weight is the wight of |
| 263 | * this cfqg against the child cfqgs. For the root cfqg, both |
| 264 | * weights are kept in sync for backward compatibility. |
| 265 | */ |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 266 | unsigned int weight; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 267 | unsigned int new_weight; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 268 | unsigned int dev_weight; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 269 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 270 | unsigned int leaf_weight; |
| 271 | unsigned int new_leaf_weight; |
| 272 | unsigned int dev_leaf_weight; |
| 273 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 274 | /* number of cfqq currently on this group */ |
| 275 | int nr_cfqq; |
| 276 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 277 | /* |
Kyungmin Park | 4495a7d | 2011-05-31 10:04:09 +0200 | [diff] [blame] | 278 | * Per group busy queues average. Useful for workload slice calc. We |
Vivek Goyal | b462732 | 2010-10-22 09:48:43 +0200 | [diff] [blame] | 279 | * create the array for each prio class but at run time it is used |
| 280 | * only for RT and BE class and slot for IDLE class remains unused. |
| 281 | * This is primarily done to avoid confusion and a gcc warning. |
| 282 | */ |
| 283 | unsigned int busy_queues_avg[CFQ_PRIO_NR]; |
| 284 | /* |
| 285 | * rr lists of queues with requests. We maintain service trees for |
| 286 | * RT and BE classes. These trees are subdivided in subclasses |
| 287 | * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE |
| 288 | * class there is no subclassification and all the cfq queues go on |
| 289 | * a single tree service_tree_idle. |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 290 | * Counts are embedded in the cfq_rb_root |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 291 | */ |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 292 | struct cfq_rb_root service_trees[2][3]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 293 | struct cfq_rb_root service_tree_idle; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 294 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 295 | u64 saved_wl_slice; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 296 | enum wl_type_t saved_wl_type; |
| 297 | enum wl_class_t saved_wl_class; |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame] | 298 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 299 | /* number of requests that are on the dispatch list or inside driver */ |
| 300 | int dispatched; |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 301 | struct cfq_ttime ttime; |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 302 | struct cfqg_stats stats; /* stats for this cfqg */ |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 303 | |
| 304 | /* async queue for each priority case */ |
| 305 | struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR]; |
| 306 | struct cfq_queue *async_idle_cfqq; |
| 307 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 308 | }; |
| 309 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 310 | struct cfq_io_cq { |
| 311 | struct io_cq icq; /* must be the first member */ |
| 312 | struct cfq_queue *cfqq[2]; |
| 313 | struct cfq_ttime ttime; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 314 | int ioprio; /* the current ioprio */ |
| 315 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | f4da807 | 2014-09-08 08:15:20 +0900 | [diff] [blame] | 316 | uint64_t blkcg_serial_nr; /* the current blkcg serial */ |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 317 | #endif |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 318 | }; |
| 319 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 320 | /* |
| 321 | * Per block device queue structure |
| 322 | */ |
| 323 | struct cfq_data { |
| 324 | struct request_queue *queue; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 325 | /* Root service tree for cfq_groups */ |
| 326 | struct cfq_rb_root grp_service_tree; |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 327 | struct cfq_group *root_group; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 328 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 329 | /* |
| 330 | * The priority currently being served |
| 331 | */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 332 | enum wl_class_t serving_wl_class; |
| 333 | enum wl_type_t serving_wl_type; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 334 | u64 workload_expires; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 335 | struct cfq_group *serving_group; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * Each priority tree is sorted by next_request position. These |
| 339 | * trees are used when determining if two or more queues are |
| 340 | * interleaving requests (see cfq_close_cooperator). |
| 341 | */ |
| 342 | struct rb_root prio_trees[CFQ_PRIO_LISTS]; |
| 343 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 344 | unsigned int busy_queues; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 345 | unsigned int busy_sync_queues; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 346 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 347 | int rq_in_driver; |
| 348 | int rq_in_flight[2]; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * queue-depth detection |
| 352 | */ |
| 353 | int rq_queued; |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 354 | int hw_tag; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 355 | /* |
| 356 | * hw_tag can be |
| 357 | * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection) |
| 358 | * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth) |
| 359 | * 0 => no NCQ |
| 360 | */ |
| 361 | int hw_tag_est_depth; |
| 362 | unsigned int hw_tag_samples; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 363 | |
| 364 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 365 | * idle window management |
| 366 | */ |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 367 | struct hrtimer idle_slice_timer; |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 368 | struct work_struct unplug_work; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 369 | |
| 370 | struct cfq_queue *active_queue; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 371 | struct cfq_io_cq *active_cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 372 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 373 | sector_t last_position; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
| 376 | * tunables, see top of file |
| 377 | */ |
| 378 | unsigned int cfq_quantum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | unsigned int cfq_back_penalty; |
| 380 | unsigned int cfq_back_max; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 381 | unsigned int cfq_slice_async_rq; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 382 | unsigned int cfq_latency; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 383 | u64 cfq_fifo_expire[2]; |
| 384 | u64 cfq_slice[2]; |
| 385 | u64 cfq_slice_idle; |
| 386 | u64 cfq_group_idle; |
| 387 | u64 cfq_target_latency; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 388 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Fallback dummy cfqq for extreme OOM conditions |
| 391 | */ |
| 392 | struct cfq_queue oom_cfqq; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 393 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 394 | u64 last_delayed_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 397 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd); |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 398 | static void cfq_put_queue(struct cfq_queue *cfqq); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 399 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 400 | static struct cfq_rb_root *st_for(struct cfq_group *cfqg, |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 401 | enum wl_class_t class, |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 402 | enum wl_type_t type) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 403 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 404 | if (!cfqg) |
| 405 | return NULL; |
| 406 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 407 | if (class == IDLE_WORKLOAD) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 408 | return &cfqg->service_tree_idle; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 409 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 410 | return &cfqg->service_trees[class][type]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 413 | enum cfqq_state_flags { |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 414 | CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ |
| 415 | CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 416 | CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 417 | CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 418 | CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ |
| 419 | CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ |
| 420 | CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 421 | CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 422 | CFQ_CFQQ_FLAG_sync, /* synchronous queue */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 423 | CFQ_CFQQ_FLAG_coop, /* cfqq is shared */ |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 424 | CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */ |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 425 | CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */ |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 426 | CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | #define CFQ_CFQQ_FNS(name) \ |
| 430 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ |
| 431 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 432 | (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 433 | } \ |
| 434 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ |
| 435 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 436 | (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 437 | } \ |
| 438 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ |
| 439 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 440 | return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | CFQ_CFQQ_FNS(on_rr); |
| 444 | CFQ_CFQQ_FNS(wait_request); |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 445 | CFQ_CFQQ_FNS(must_dispatch); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 446 | CFQ_CFQQ_FNS(must_alloc_slice); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 447 | CFQ_CFQQ_FNS(fifo_expire); |
| 448 | CFQ_CFQQ_FNS(idle_window); |
| 449 | CFQ_CFQQ_FNS(prio_changed); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 450 | CFQ_CFQQ_FNS(slice_new); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 451 | CFQ_CFQQ_FNS(sync); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 452 | CFQ_CFQQ_FNS(coop); |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 453 | CFQ_CFQQ_FNS(split_coop); |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 454 | CFQ_CFQQ_FNS(deep); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 455 | CFQ_CFQQ_FNS(wait_busy); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 456 | #undef CFQ_CFQQ_FNS |
| 457 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 458 | #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 459 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 460 | /* cfqg stats flags */ |
| 461 | enum cfqg_stats_flags { |
| 462 | CFQG_stats_waiting = 0, |
| 463 | CFQG_stats_idling, |
| 464 | CFQG_stats_empty, |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 467 | #define CFQG_FLAG_FNS(name) \ |
| 468 | static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 469 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 470 | stats->flags |= (1 << CFQG_stats_##name); \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 471 | } \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 472 | static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 473 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 474 | stats->flags &= ~(1 << CFQG_stats_##name); \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 475 | } \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 476 | static inline int cfqg_stats_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 477 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 478 | return (stats->flags & (1 << CFQG_stats_##name)) != 0; \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 479 | } \ |
| 480 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 481 | CFQG_FLAG_FNS(waiting) |
| 482 | CFQG_FLAG_FNS(idling) |
| 483 | CFQG_FLAG_FNS(empty) |
| 484 | #undef CFQG_FLAG_FNS |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 485 | |
| 486 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 487 | static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 488 | { |
| 489 | unsigned long long now; |
| 490 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 491 | if (!cfqg_stats_waiting(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 492 | return; |
| 493 | |
| 494 | now = sched_clock(); |
| 495 | if (time_after64(now, stats->start_group_wait_time)) |
| 496 | blkg_stat_add(&stats->group_wait_time, |
| 497 | now - stats->start_group_wait_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 498 | cfqg_stats_clear_waiting(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 502 | static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, |
| 503 | struct cfq_group *curr_cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 504 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 505 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 506 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 507 | if (cfqg_stats_waiting(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 508 | return; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 509 | if (cfqg == curr_cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 510 | return; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 511 | stats->start_group_wait_time = sched_clock(); |
| 512 | cfqg_stats_mark_waiting(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 516 | static void cfqg_stats_end_empty_time(struct cfqg_stats *stats) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 517 | { |
| 518 | unsigned long long now; |
| 519 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 520 | if (!cfqg_stats_empty(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 521 | return; |
| 522 | |
| 523 | now = sched_clock(); |
| 524 | if (time_after64(now, stats->start_empty_time)) |
| 525 | blkg_stat_add(&stats->empty_time, |
| 526 | now - stats->start_empty_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 527 | cfqg_stats_clear_empty(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 530 | static void cfqg_stats_update_dequeue(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 531 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 532 | blkg_stat_add(&cfqg->stats.dequeue, 1); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 535 | static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 536 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 537 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 538 | |
Tejun Heo | 4d5e80a | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 539 | if (blkg_rwstat_total(&stats->queued)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 540 | return; |
| 541 | |
| 542 | /* |
| 543 | * group is already marked empty. This can happen if cfqq got new |
| 544 | * request in parent group and moved to this group while being added |
| 545 | * to service tree. Just ignore the event and move on. |
| 546 | */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 547 | if (cfqg_stats_empty(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 548 | return; |
| 549 | |
| 550 | stats->start_empty_time = sched_clock(); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 551 | cfqg_stats_mark_empty(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 554 | static void cfqg_stats_update_idle_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 555 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 556 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 557 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 558 | if (cfqg_stats_idling(stats)) { |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 559 | unsigned long long now = sched_clock(); |
| 560 | |
| 561 | if (time_after64(now, stats->start_idle_time)) |
| 562 | blkg_stat_add(&stats->idle_time, |
| 563 | now - stats->start_idle_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 564 | cfqg_stats_clear_idling(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 568 | static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 569 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 570 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 571 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 572 | BUG_ON(cfqg_stats_idling(stats)); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 573 | |
| 574 | stats->start_idle_time = sched_clock(); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 575 | cfqg_stats_mark_idling(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 578 | static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 579 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 580 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 581 | |
| 582 | blkg_stat_add(&stats->avg_queue_size_sum, |
Tejun Heo | 4d5e80a | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 583 | blkg_rwstat_total(&stats->queued)); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 584 | blkg_stat_add(&stats->avg_queue_size_samples, 1); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 585 | cfqg_stats_update_group_wait_time(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ |
| 589 | |
Tejun Heo | f48ec1d | 2012-04-13 13:11:25 -0700 | [diff] [blame] | 590 | static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { } |
| 591 | static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { } |
| 592 | static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { } |
| 593 | static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { } |
| 594 | static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { } |
| 595 | static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { } |
| 596 | static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { } |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 597 | |
| 598 | #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ |
| 599 | |
| 600 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 601 | |
Jens Axboe | 4ceab71 | 2015-06-19 10:13:01 -0600 | [diff] [blame] | 602 | static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd) |
| 603 | { |
| 604 | return pd ? container_of(pd, struct cfq_group, pd) : NULL; |
| 605 | } |
| 606 | |
| 607 | static struct cfq_group_data |
| 608 | *cpd_to_cfqgd(struct blkcg_policy_data *cpd) |
| 609 | { |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 610 | return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL; |
Jens Axboe | 4ceab71 | 2015-06-19 10:13:01 -0600 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg) |
| 614 | { |
| 615 | return pd_to_blkg(&cfqg->pd); |
| 616 | } |
| 617 | |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 618 | static struct blkcg_policy blkcg_policy_cfq; |
| 619 | |
| 620 | static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg) |
| 621 | { |
| 622 | return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq)); |
| 623 | } |
| 624 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 625 | static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg) |
| 626 | { |
| 627 | return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq)); |
| 628 | } |
| 629 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 630 | static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 631 | { |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 632 | struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 633 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 634 | return pblkg ? blkg_to_cfqg(pblkg) : NULL; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Jan Kara | 3984aa5 | 2016-01-12 16:24:19 +0100 | [diff] [blame] | 637 | static inline bool cfqg_is_descendant(struct cfq_group *cfqg, |
| 638 | struct cfq_group *ancestor) |
| 639 | { |
| 640 | return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup, |
| 641 | cfqg_to_blkg(ancestor)->blkcg->css.cgroup); |
| 642 | } |
| 643 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 644 | static inline void cfqg_get(struct cfq_group *cfqg) |
| 645 | { |
| 646 | return blkg_get(cfqg_to_blkg(cfqg)); |
| 647 | } |
| 648 | |
| 649 | static inline void cfqg_put(struct cfq_group *cfqg) |
| 650 | { |
| 651 | return blkg_put(cfqg_to_blkg(cfqg)); |
| 652 | } |
| 653 | |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 654 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \ |
| 655 | char __pbuf[128]; \ |
| 656 | \ |
| 657 | blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \ |
Vivek Goyal | b226e5c | 2012-10-03 16:57:01 -0400 | [diff] [blame] | 658 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \ |
| 659 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ |
| 660 | cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 661 | __pbuf, ##args); \ |
| 662 | } while (0) |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 663 | |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 664 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \ |
| 665 | char __pbuf[128]; \ |
| 666 | \ |
| 667 | blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \ |
| 668 | blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \ |
| 669 | } while (0) |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 670 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 671 | static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 672 | struct cfq_group *curr_cfqg, |
| 673 | unsigned int op) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 674 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 675 | blkg_rwstat_add(&cfqg->stats.queued, op, 1); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 676 | cfqg_stats_end_empty_time(&cfqg->stats); |
| 677 | cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 680 | static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 681 | uint64_t time, unsigned long unaccounted_time) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 682 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 683 | blkg_stat_add(&cfqg->stats.time, time); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 684 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 685 | blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 686 | #endif |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 689 | static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, |
| 690 | unsigned int op) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 691 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 692 | blkg_rwstat_add(&cfqg->stats.queued, op, -1); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 695 | static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, |
| 696 | unsigned int op) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 697 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 698 | blkg_rwstat_add(&cfqg->stats.merged, op, 1); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 701 | static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 702 | uint64_t start_time, uint64_t io_start_time, |
| 703 | unsigned int op) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 704 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 705 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 706 | unsigned long long now = sched_clock(); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 707 | |
| 708 | if (time_after64(now, io_start_time)) |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 709 | blkg_rwstat_add(&stats->service_time, op, now - io_start_time); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 710 | if (time_after64(io_start_time, start_time)) |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 711 | blkg_rwstat_add(&stats->wait_time, op, |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 712 | io_start_time - start_time); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Tejun Heo | 689665a | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 715 | /* @stats = 0 */ |
| 716 | static void cfqg_stats_reset(struct cfqg_stats *stats) |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 717 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 718 | /* queued stats shouldn't be cleared */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 719 | blkg_rwstat_reset(&stats->merged); |
| 720 | blkg_rwstat_reset(&stats->service_time); |
| 721 | blkg_rwstat_reset(&stats->wait_time); |
| 722 | blkg_stat_reset(&stats->time); |
| 723 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 724 | blkg_stat_reset(&stats->unaccounted_time); |
| 725 | blkg_stat_reset(&stats->avg_queue_size_sum); |
| 726 | blkg_stat_reset(&stats->avg_queue_size_samples); |
| 727 | blkg_stat_reset(&stats->dequeue); |
| 728 | blkg_stat_reset(&stats->group_wait_time); |
| 729 | blkg_stat_reset(&stats->idle_time); |
| 730 | blkg_stat_reset(&stats->empty_time); |
| 731 | #endif |
| 732 | } |
| 733 | |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 734 | /* @to += @from */ |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 735 | static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from) |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 736 | { |
| 737 | /* queued stats shouldn't be cleared */ |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 738 | blkg_rwstat_add_aux(&to->merged, &from->merged); |
| 739 | blkg_rwstat_add_aux(&to->service_time, &from->service_time); |
| 740 | blkg_rwstat_add_aux(&to->wait_time, &from->wait_time); |
| 741 | blkg_stat_add_aux(&from->time, &from->time); |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 742 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 743 | blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time); |
| 744 | blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum); |
| 745 | blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples); |
| 746 | blkg_stat_add_aux(&to->dequeue, &from->dequeue); |
| 747 | blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time); |
| 748 | blkg_stat_add_aux(&to->idle_time, &from->idle_time); |
| 749 | blkg_stat_add_aux(&to->empty_time, &from->empty_time); |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 750 | #endif |
| 751 | } |
| 752 | |
| 753 | /* |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 754 | * Transfer @cfqg's stats to its parent's aux counts so that the ancestors' |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 755 | * recursive stats can still account for the amount used by this cfqg after |
| 756 | * it's gone. |
| 757 | */ |
| 758 | static void cfqg_stats_xfer_dead(struct cfq_group *cfqg) |
| 759 | { |
| 760 | struct cfq_group *parent = cfqg_parent(cfqg); |
| 761 | |
| 762 | lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock); |
| 763 | |
| 764 | if (unlikely(!parent)) |
| 765 | return; |
| 766 | |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 767 | cfqg_stats_add_aux(&parent->stats, &cfqg->stats); |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 768 | cfqg_stats_reset(&cfqg->stats); |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 771 | #else /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 772 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 773 | static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; } |
Jan Kara | 3984aa5 | 2016-01-12 16:24:19 +0100 | [diff] [blame] | 774 | static inline bool cfqg_is_descendant(struct cfq_group *cfqg, |
| 775 | struct cfq_group *ancestor) |
| 776 | { |
| 777 | return true; |
| 778 | } |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 779 | static inline void cfqg_get(struct cfq_group *cfqg) { } |
| 780 | static inline void cfqg_put(struct cfq_group *cfqg) { } |
| 781 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 782 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
Vivek Goyal | b226e5c | 2012-10-03 16:57:01 -0400 | [diff] [blame] | 783 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \ |
| 784 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ |
| 785 | cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ |
| 786 | ##args) |
Kyungmin Park | 4495a7d | 2011-05-31 10:04:09 +0200 | [diff] [blame] | 787 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0) |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 788 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 789 | static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 790 | struct cfq_group *curr_cfqg, unsigned int op) { } |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 791 | static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 792 | uint64_t time, unsigned long unaccounted_time) { } |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 793 | static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, |
| 794 | unsigned int op) { } |
| 795 | static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, |
| 796 | unsigned int op) { } |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 797 | static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 798 | uint64_t start_time, uint64_t io_start_time, |
| 799 | unsigned int op) { } |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 800 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 801 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 802 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 803 | #define cfq_log(cfqd, fmt, args...) \ |
| 804 | blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) |
| 805 | |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 806 | /* Traverses through cfq group service trees */ |
| 807 | #define for_each_cfqg_st(cfqg, i, j, st) \ |
| 808 | for (i = 0; i <= IDLE_WORKLOAD; i++) \ |
| 809 | for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\ |
| 810 | : &cfqg->service_tree_idle; \ |
| 811 | (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \ |
| 812 | (i == IDLE_WORKLOAD && j == 0); \ |
| 813 | j++, st = i < IDLE_WORKLOAD ? \ |
| 814 | &cfqg->service_trees[i][j]: NULL) \ |
| 815 | |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 816 | static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd, |
| 817 | struct cfq_ttime *ttime, bool group_idle) |
| 818 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 819 | u64 slice; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 820 | if (!sample_valid(ttime->ttime_samples)) |
| 821 | return false; |
| 822 | if (group_idle) |
| 823 | slice = cfqd->cfq_group_idle; |
| 824 | else |
| 825 | slice = cfqd->cfq_slice_idle; |
| 826 | return ttime->ttime_mean > slice; |
| 827 | } |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 828 | |
Vivek Goyal | 02b3508 | 2010-08-23 12:23:53 +0200 | [diff] [blame] | 829 | static inline bool iops_mode(struct cfq_data *cfqd) |
| 830 | { |
| 831 | /* |
| 832 | * If we are not idling on queues and it is a NCQ drive, parallel |
| 833 | * execution of requests is on and measuring time is not possible |
| 834 | * in most of the cases until and unless we drive shallower queue |
| 835 | * depths and that becomes a performance bottleneck. In such cases |
| 836 | * switch to start providing fairness in terms of number of IOs. |
| 837 | */ |
| 838 | if (!cfqd->cfq_slice_idle && cfqd->hw_tag) |
| 839 | return true; |
| 840 | else |
| 841 | return false; |
| 842 | } |
| 843 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 844 | static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 845 | { |
| 846 | if (cfq_class_idle(cfqq)) |
| 847 | return IDLE_WORKLOAD; |
| 848 | if (cfq_class_rt(cfqq)) |
| 849 | return RT_WORKLOAD; |
| 850 | return BE_WORKLOAD; |
| 851 | } |
| 852 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 853 | |
| 854 | static enum wl_type_t cfqq_type(struct cfq_queue *cfqq) |
| 855 | { |
| 856 | if (!cfq_cfqq_sync(cfqq)) |
| 857 | return ASYNC_WORKLOAD; |
| 858 | if (!cfq_cfqq_idle_window(cfqq)) |
| 859 | return SYNC_NOIDLE_WORKLOAD; |
| 860 | return SYNC_WORKLOAD; |
| 861 | } |
| 862 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 863 | static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class, |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 864 | struct cfq_data *cfqd, |
| 865 | struct cfq_group *cfqg) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 866 | { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 867 | if (wl_class == IDLE_WORKLOAD) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 868 | return cfqg->service_tree_idle.count; |
| 869 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 870 | return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count + |
| 871 | cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count + |
| 872 | cfqg->service_trees[wl_class][SYNC_WORKLOAD].count; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 875 | static inline int cfqg_busy_async_queues(struct cfq_data *cfqd, |
| 876 | struct cfq_group *cfqg) |
| 877 | { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 878 | return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count + |
| 879 | cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 880 | } |
| 881 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 882 | static void cfq_dispatch_insert(struct request_queue *, struct request *); |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 883 | static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync, |
Tejun Heo | 2da8de0 | 2015-08-18 14:55:02 -0700 | [diff] [blame] | 884 | struct cfq_io_cq *cic, struct bio *bio); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 885 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 886 | static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq) |
| 887 | { |
| 888 | /* cic->icq is the first member, %NULL will convert to %NULL */ |
| 889 | return container_of(icq, struct cfq_io_cq, icq); |
| 890 | } |
| 891 | |
Tejun Heo | 47fdd4c | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 892 | static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd, |
| 893 | struct io_context *ioc) |
| 894 | { |
| 895 | if (ioc) |
| 896 | return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue)); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 900 | static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 901 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 902 | return cic->cfqq[is_sync]; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 905 | static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq, |
| 906 | bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 907 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 908 | cic->cfqq[is_sync] = cfqq; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 911 | static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic) |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 912 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 913 | return cic->icq.q->elevator->elevator_data; |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 914 | } |
| 915 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 916 | /* |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 917 | * scheduler run of queue, if there are requests pending and no one in the |
| 918 | * driver that will restart queueing |
| 919 | */ |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 920 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 921 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 922 | if (cfqd->busy_queues) { |
| 923 | cfq_log(cfqd, "schedule dispatch"); |
Jens Axboe | 59c3d45 | 2014-04-08 09:15:35 -0600 | [diff] [blame] | 924 | kblockd_schedule_work(&cfqd->unplug_work); |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 925 | } |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | /* |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 929 | * Scale schedule slice based on io priority. Use the sync time slice only |
| 930 | * if a queue is marked sync and has sync io queued. A sync queue with async |
| 931 | * io only, should not get full sync slice length. |
| 932 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 933 | static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync, |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 934 | unsigned short prio) |
| 935 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 936 | u64 base_slice = cfqd->cfq_slice[sync]; |
| 937 | u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 938 | |
| 939 | WARN_ON(prio >= IOPRIO_BE_NR); |
| 940 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 941 | return base_slice + (slice * (4 - prio)); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 944 | static inline u64 |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 945 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 946 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 947 | return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 948 | } |
| 949 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 950 | /** |
| 951 | * cfqg_scale_charge - scale disk time charge according to cfqg weight |
| 952 | * @charge: disk time being charged |
| 953 | * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT |
| 954 | * |
| 955 | * Scale @charge according to @vfraction, which is in range (0, 1]. The |
| 956 | * scaling is inversely proportional. |
| 957 | * |
| 958 | * scaled = charge / vfraction |
| 959 | * |
| 960 | * The result is also in fixed point w/ CFQ_SERVICE_SHIFT. |
| 961 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 962 | static inline u64 cfqg_scale_charge(u64 charge, |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 963 | unsigned int vfraction) |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 964 | { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 965 | u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */ |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 966 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 967 | /* charge / vfraction */ |
| 968 | c <<= CFQ_SERVICE_SHIFT; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 969 | return div_u64(c, vfraction); |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 973 | { |
| 974 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 975 | if (delta > 0) |
| 976 | min_vdisktime = vdisktime; |
| 977 | |
| 978 | return min_vdisktime; |
| 979 | } |
| 980 | |
| 981 | static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 982 | { |
| 983 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 984 | if (delta < 0) |
| 985 | min_vdisktime = vdisktime; |
| 986 | |
| 987 | return min_vdisktime; |
| 988 | } |
| 989 | |
| 990 | static void update_min_vdisktime(struct cfq_rb_root *st) |
| 991 | { |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 992 | struct cfq_group *cfqg; |
| 993 | |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 994 | if (st->left) { |
| 995 | cfqg = rb_entry_cfqg(st->left); |
Gui Jianfeng | a603271 | 2011-03-07 09:28:09 +0100 | [diff] [blame] | 996 | st->min_vdisktime = max_vdisktime(st->min_vdisktime, |
| 997 | cfqg->vdisktime); |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 998 | } |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1001 | /* |
| 1002 | * get averaged number of queues of RT/BE priority. |
| 1003 | * average is updated, with a formula that gives more weight to higher numbers, |
| 1004 | * to quickly follows sudden increases and decrease slowly |
| 1005 | */ |
| 1006 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1007 | static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd, |
| 1008 | struct cfq_group *cfqg, bool rt) |
Jens Axboe | 5869619c | 2009-10-28 09:27:07 +0100 | [diff] [blame] | 1009 | { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1010 | unsigned min_q, max_q; |
| 1011 | unsigned mult = cfq_hist_divisor - 1; |
| 1012 | unsigned round = cfq_hist_divisor / 2; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1013 | unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1014 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1015 | min_q = min(cfqg->busy_queues_avg[rt], busy); |
| 1016 | max_q = max(cfqg->busy_queues_avg[rt], busy); |
| 1017 | cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) / |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1018 | cfq_hist_divisor; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1019 | return cfqg->busy_queues_avg[rt]; |
| 1020 | } |
| 1021 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1022 | static inline u64 |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1023 | cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 1024 | { |
Tejun Heo | 41cad6a | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1025 | return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1028 | static inline u64 |
Vivek Goyal | ba5bd52 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 1029 | cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1030 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1031 | u64 slice = cfq_prio_to_slice(cfqd, cfqq); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1032 | if (cfqd->cfq_latency) { |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1033 | /* |
| 1034 | * interested queues (we consider only the ones with the same |
| 1035 | * priority class in the cfq group) |
| 1036 | */ |
| 1037 | unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg, |
| 1038 | cfq_class_rt(cfqq)); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1039 | u64 sync_slice = cfqd->cfq_slice[1]; |
| 1040 | u64 expect_latency = sync_slice * iq; |
| 1041 | u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg); |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 1042 | |
| 1043 | if (expect_latency > group_slice) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1044 | u64 base_low_slice = 2 * cfqd->cfq_slice_idle; |
| 1045 | u64 low_slice; |
| 1046 | |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1047 | /* scale low_slice according to IO priority |
| 1048 | * and sync vs async */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1049 | low_slice = div64_u64(base_low_slice*slice, sync_slice); |
| 1050 | low_slice = min(slice, low_slice); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1051 | /* the adapted slice value is scaled to fit all iqs |
| 1052 | * into the target latency */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1053 | slice = div64_u64(slice*group_slice, expect_latency); |
| 1054 | slice = max(slice, low_slice); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1055 | } |
| 1056 | } |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 1057 | return slice; |
| 1058 | } |
| 1059 | |
| 1060 | static inline void |
| 1061 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1062 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1063 | u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq); |
| 1064 | u64 now = ktime_get_ns(); |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 1065 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1066 | cfqq->slice_start = now; |
| 1067 | cfqq->slice_end = now + slice; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 1068 | cfqq->allocated_slice = slice; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1069 | cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end |
| 1074 | * isn't valid until the first request from the dispatch is activated |
| 1075 | * and the slice time set. |
| 1076 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1077 | static inline bool cfq_slice_used(struct cfq_queue *cfqq) |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1078 | { |
| 1079 | if (cfq_cfqq_slice_new(cfqq)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1080 | return false; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1081 | if (ktime_get_ns() < cfqq->slice_end) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1082 | return false; |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1083 | |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1084 | return true; |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1088 | * Lifted from AS - choose which of rq1 and rq2 that is best served now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | * We choose the request that is closest to the head right now. Distance |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1090 | * behind the head is penalized and only allowed to a certain extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1092 | static struct request * |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1093 | cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1095 | sector_t s1, s2, d1 = 0, d2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | unsigned long back_max; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1097 | #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ |
| 1098 | #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ |
| 1099 | unsigned wrap = 0; /* bit mask: requests behind the disk head? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1101 | if (rq1 == NULL || rq1 == rq2) |
| 1102 | return rq2; |
| 1103 | if (rq2 == NULL) |
| 1104 | return rq1; |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1105 | |
Namhyung Kim | 229836b | 2011-05-24 10:23:21 +0200 | [diff] [blame] | 1106 | if (rq_is_sync(rq1) != rq_is_sync(rq2)) |
| 1107 | return rq_is_sync(rq1) ? rq1 : rq2; |
| 1108 | |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 1109 | if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO) |
| 1110 | return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2; |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 1111 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1112 | s1 = blk_rq_pos(rq1); |
| 1113 | s2 = blk_rq_pos(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | /* |
| 1116 | * by definition, 1KiB is 2 sectors |
| 1117 | */ |
| 1118 | back_max = cfqd->cfq_back_max * 2; |
| 1119 | |
| 1120 | /* |
| 1121 | * Strict one way elevator _except_ in the case where we allow |
| 1122 | * short backward seeks which are biased as twice the cost of a |
| 1123 | * similar forward seek. |
| 1124 | */ |
| 1125 | if (s1 >= last) |
| 1126 | d1 = s1 - last; |
| 1127 | else if (s1 + back_max >= last) |
| 1128 | d1 = (last - s1) * cfqd->cfq_back_penalty; |
| 1129 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1130 | wrap |= CFQ_RQ1_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | if (s2 >= last) |
| 1133 | d2 = s2 - last; |
| 1134 | else if (s2 + back_max >= last) |
| 1135 | d2 = (last - s2) * cfqd->cfq_back_penalty; |
| 1136 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1137 | wrap |= CFQ_RQ2_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
| 1139 | /* Found required data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1141 | /* |
| 1142 | * By doing switch() on the bit mask "wrap" we avoid having to |
| 1143 | * check two variables for all permutations: --> faster! |
| 1144 | */ |
| 1145 | switch (wrap) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1146 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1147 | if (d1 < d2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1148 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1149 | else if (d2 < d1) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1150 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1151 | else { |
| 1152 | if (s1 >= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1153 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1154 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1155 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | case CFQ_RQ2_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1159 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1160 | case CFQ_RQ1_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1161 | return rq2; |
| 1162 | case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1163 | default: |
| 1164 | /* |
| 1165 | * Since both rqs are wrapped, |
| 1166 | * start with the one that's further behind head |
| 1167 | * (--> only *one* back seek required), |
| 1168 | * since back seek takes more time than forward. |
| 1169 | */ |
| 1170 | if (s1 <= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1171 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1173 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1177 | /* |
| 1178 | * The below is leftmost cache rbtree addon |
| 1179 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1180 | static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1181 | { |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 1182 | /* Service tree is empty */ |
| 1183 | if (!root->count) |
| 1184 | return NULL; |
| 1185 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1186 | if (!root->left) |
| 1187 | root->left = rb_first(&root->rb); |
| 1188 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1189 | if (root->left) |
| 1190 | return rb_entry(root->left, struct cfq_queue, rb_node); |
| 1191 | |
| 1192 | return NULL; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1195 | static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root) |
| 1196 | { |
| 1197 | if (!root->left) |
| 1198 | root->left = rb_first(&root->rb); |
| 1199 | |
| 1200 | if (root->left) |
| 1201 | return rb_entry_cfqg(root->left); |
| 1202 | |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1206 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 1207 | { |
| 1208 | rb_erase(n, root); |
| 1209 | RB_CLEAR_NODE(n); |
| 1210 | } |
| 1211 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1212 | static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) |
| 1213 | { |
| 1214 | if (root->left == n) |
| 1215 | root->left = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1216 | rb_erase_init(n, &root->rb); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1217 | --root->count; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1218 | } |
| 1219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | /* |
| 1221 | * would be nice to take fifo expire time into account as well |
| 1222 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1223 | static struct request * |
| 1224 | cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1225 | struct request *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1227 | struct rb_node *rbnext = rb_next(&last->rb_node); |
| 1228 | struct rb_node *rbprev = rb_prev(&last->rb_node); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1229 | struct request *next = NULL, *prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1231 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
| 1233 | if (rbprev) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1234 | prev = rb_entry_rq(rbprev); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | if (rbnext) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1237 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1238 | else { |
| 1239 | rbnext = rb_first(&cfqq->sort_list); |
| 1240 | if (rbnext && rbnext != &last->rb_node) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1241 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1244 | return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1247 | static u64 cfq_slice_offset(struct cfq_data *cfqd, |
| 1248 | struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1250 | /* |
| 1251 | * just an approximation, should be ok. |
| 1252 | */ |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1253 | return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) - |
Jens Axboe | 464191c | 2009-11-30 09:38:13 +0100 | [diff] [blame] | 1254 | cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1257 | static inline s64 |
| 1258 | cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1259 | { |
| 1260 | return cfqg->vdisktime - st->min_vdisktime; |
| 1261 | } |
| 1262 | |
| 1263 | static void |
| 1264 | __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1265 | { |
| 1266 | struct rb_node **node = &st->rb.rb_node; |
| 1267 | struct rb_node *parent = NULL; |
| 1268 | struct cfq_group *__cfqg; |
| 1269 | s64 key = cfqg_key(st, cfqg); |
| 1270 | int left = 1; |
| 1271 | |
| 1272 | while (*node != NULL) { |
| 1273 | parent = *node; |
| 1274 | __cfqg = rb_entry_cfqg(parent); |
| 1275 | |
| 1276 | if (key < cfqg_key(st, __cfqg)) |
| 1277 | node = &parent->rb_left; |
| 1278 | else { |
| 1279 | node = &parent->rb_right; |
| 1280 | left = 0; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | if (left) |
| 1285 | st->left = &cfqg->rb_node; |
| 1286 | |
| 1287 | rb_link_node(&cfqg->rb_node, parent, node); |
| 1288 | rb_insert_color(&cfqg->rb_node, &st->rb); |
| 1289 | } |
| 1290 | |
Toshiaki Makita | 7b5af5c | 2014-08-28 17:14:58 +0900 | [diff] [blame] | 1291 | /* |
| 1292 | * This has to be called only on activation of cfqg |
| 1293 | */ |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1294 | static void |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1295 | cfq_update_group_weight(struct cfq_group *cfqg) |
| 1296 | { |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1297 | if (cfqg->new_weight) { |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1298 | cfqg->weight = cfqg->new_weight; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1299 | cfqg->new_weight = 0; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1300 | } |
Toshiaki Makita | e15693e | 2014-08-26 20:56:36 +0900 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static void |
| 1304 | cfq_update_group_leaf_weight(struct cfq_group *cfqg) |
| 1305 | { |
| 1306 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1307 | |
| 1308 | if (cfqg->new_leaf_weight) { |
| 1309 | cfqg->leaf_weight = cfqg->new_leaf_weight; |
| 1310 | cfqg->new_leaf_weight = 0; |
| 1311 | } |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | static void |
| 1315 | cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1316 | { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1317 | unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */ |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1318 | struct cfq_group *pos = cfqg; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1319 | struct cfq_group *parent; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1320 | bool propagate; |
| 1321 | |
| 1322 | /* add to the service tree */ |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1323 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); |
| 1324 | |
Toshiaki Makita | 7b5af5c | 2014-08-28 17:14:58 +0900 | [diff] [blame] | 1325 | /* |
| 1326 | * Update leaf_weight. We cannot update weight at this point |
| 1327 | * because cfqg might already have been activated and is |
| 1328 | * contributing its current weight to the parent's child_weight. |
| 1329 | */ |
Toshiaki Makita | e15693e | 2014-08-26 20:56:36 +0900 | [diff] [blame] | 1330 | cfq_update_group_leaf_weight(cfqg); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1331 | __cfq_group_service_tree_add(st, cfqg); |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1332 | |
| 1333 | /* |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1334 | * Activate @cfqg and calculate the portion of vfraction @cfqg is |
| 1335 | * entitled to. vfraction is calculated by walking the tree |
| 1336 | * towards the root calculating the fraction it has at each level. |
| 1337 | * The compounded ratio is how much vfraction @cfqg owns. |
| 1338 | * |
| 1339 | * Start with the proportion tasks in this cfqg has against active |
| 1340 | * children cfqgs - its leaf_weight against children_weight. |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1341 | */ |
| 1342 | propagate = !pos->nr_active++; |
| 1343 | pos->children_weight += pos->leaf_weight; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1344 | vfr = vfr * pos->leaf_weight / pos->children_weight; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1345 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1346 | /* |
| 1347 | * Compound ->weight walking up the tree. Both activation and |
| 1348 | * vfraction calculation are done in the same loop. Propagation |
| 1349 | * stops once an already activated node is met. vfraction |
| 1350 | * calculation should always continue to the root. |
| 1351 | */ |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1352 | while ((parent = cfqg_parent(pos))) { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1353 | if (propagate) { |
Toshiaki Makita | e15693e | 2014-08-26 20:56:36 +0900 | [diff] [blame] | 1354 | cfq_update_group_weight(pos); |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1355 | propagate = !parent->nr_active++; |
| 1356 | parent->children_weight += pos->weight; |
| 1357 | } |
| 1358 | vfr = vfr * pos->weight / parent->children_weight; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1359 | pos = parent; |
| 1360 | } |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1361 | |
| 1362 | cfqg->vfraction = max_t(unsigned, vfr, 1); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static void |
| 1366 | cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1367 | { |
| 1368 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 1369 | struct cfq_group *__cfqg; |
| 1370 | struct rb_node *n; |
| 1371 | |
| 1372 | cfqg->nr_cfqq++; |
Gui Jianfeng | 760701b | 2010-11-30 20:52:47 +0100 | [diff] [blame] | 1373 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1374 | return; |
| 1375 | |
| 1376 | /* |
| 1377 | * Currently put the group at the end. Later implement something |
| 1378 | * so that groups get lesser vtime based on their weights, so that |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1379 | * if group does not loose all if it was not continuously backlogged. |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1380 | */ |
| 1381 | n = rb_last(&st->rb); |
| 1382 | if (n) { |
| 1383 | __cfqg = rb_entry_cfqg(n); |
| 1384 | cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY; |
| 1385 | } else |
| 1386 | cfqg->vdisktime = st->min_vdisktime; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1387 | cfq_group_service_tree_add(st, cfqg); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | static void |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1391 | cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1392 | { |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1393 | struct cfq_group *pos = cfqg; |
| 1394 | bool propagate; |
| 1395 | |
| 1396 | /* |
| 1397 | * Undo activation from cfq_group_service_tree_add(). Deactivate |
| 1398 | * @cfqg and propagate deactivation upwards. |
| 1399 | */ |
| 1400 | propagate = !--pos->nr_active; |
| 1401 | pos->children_weight -= pos->leaf_weight; |
| 1402 | |
| 1403 | while (propagate) { |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1404 | struct cfq_group *parent = cfqg_parent(pos); |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1405 | |
| 1406 | /* @pos has 0 nr_active at this point */ |
| 1407 | WARN_ON_ONCE(pos->children_weight); |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1408 | pos->vfraction = 0; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1409 | |
| 1410 | if (!parent) |
| 1411 | break; |
| 1412 | |
| 1413 | propagate = !--parent->nr_active; |
| 1414 | parent->children_weight -= pos->weight; |
| 1415 | pos = parent; |
| 1416 | } |
| 1417 | |
| 1418 | /* remove from the service tree */ |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1419 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
| 1420 | cfq_rb_erase(&cfqg->rb_node, st); |
| 1421 | } |
| 1422 | |
| 1423 | static void |
| 1424 | cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1425 | { |
| 1426 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 1427 | |
| 1428 | BUG_ON(cfqg->nr_cfqq < 1); |
| 1429 | cfqg->nr_cfqq--; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 1430 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1431 | /* If there are other cfq queues under this group, don't delete it */ |
| 1432 | if (cfqg->nr_cfqq) |
| 1433 | return; |
| 1434 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 1435 | cfq_log_cfqg(cfqd, cfqg, "del_from_rr group"); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1436 | cfq_group_service_tree_del(st, cfqg); |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1437 | cfqg->saved_wl_slice = 0; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1438 | cfqg_stats_update_dequeue(cfqg); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1439 | } |
| 1440 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1441 | static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq, |
| 1442 | u64 *unaccounted_time) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1443 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1444 | u64 slice_used; |
| 1445 | u64 now = ktime_get_ns(); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1446 | |
| 1447 | /* |
| 1448 | * Queue got expired before even a single request completed or |
| 1449 | * got expired immediately after first request completion. |
| 1450 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1451 | if (!cfqq->slice_start || cfqq->slice_start == now) { |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1452 | /* |
| 1453 | * Also charge the seek time incurred to the group, otherwise |
| 1454 | * if there are mutiple queues in the group, each can dispatch |
| 1455 | * a single request on seeky media and cause lots of seek time |
| 1456 | * and group will never know it. |
| 1457 | */ |
Jan Kara | 0b31c10 | 2016-06-28 09:04:02 +0200 | [diff] [blame] | 1458 | slice_used = max_t(u64, (now - cfqq->dispatch_start), |
| 1459 | jiffies_to_nsecs(1)); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1460 | } else { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1461 | slice_used = now - cfqq->slice_start; |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1462 | if (slice_used > cfqq->allocated_slice) { |
| 1463 | *unaccounted_time = slice_used - cfqq->allocated_slice; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 1464 | slice_used = cfqq->allocated_slice; |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1465 | } |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1466 | if (cfqq->slice_start > cfqq->dispatch_start) |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1467 | *unaccounted_time += cfqq->slice_start - |
| 1468 | cfqq->dispatch_start; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1469 | } |
| 1470 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1471 | return slice_used; |
| 1472 | } |
| 1473 | |
| 1474 | static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg, |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 1475 | struct cfq_queue *cfqq) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1476 | { |
| 1477 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1478 | u64 used_sl, charge, unaccounted_sl = 0; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1479 | int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg) |
| 1480 | - cfqg->service_tree_idle.count; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1481 | unsigned int vfr; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1482 | u64 now = ktime_get_ns(); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1483 | |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1484 | BUG_ON(nr_sync < 0); |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1485 | used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl); |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1486 | |
Vivek Goyal | 02b3508 | 2010-08-23 12:23:53 +0200 | [diff] [blame] | 1487 | if (iops_mode(cfqd)) |
| 1488 | charge = cfqq->slice_dispatch; |
| 1489 | else if (!cfq_cfqq_sync(cfqq) && !nr_sync) |
| 1490 | charge = cfqq->allocated_slice; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1491 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1492 | /* |
| 1493 | * Can't update vdisktime while on service tree and cfqg->vfraction |
| 1494 | * is valid only while on it. Cache vfr, leave the service tree, |
| 1495 | * update vdisktime and go back on. The re-addition to the tree |
| 1496 | * will also update the weights as necessary. |
| 1497 | */ |
| 1498 | vfr = cfqg->vfraction; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1499 | cfq_group_service_tree_del(st, cfqg); |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1500 | cfqg->vdisktime += cfqg_scale_charge(charge, vfr); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1501 | cfq_group_service_tree_add(st, cfqg); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1502 | |
| 1503 | /* This group is being expired. Save the context */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1504 | if (cfqd->workload_expires > now) { |
| 1505 | cfqg->saved_wl_slice = cfqd->workload_expires - now; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1506 | cfqg->saved_wl_type = cfqd->serving_wl_type; |
| 1507 | cfqg->saved_wl_class = cfqd->serving_wl_class; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1508 | } else |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1509 | cfqg->saved_wl_slice = 0; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 1510 | |
| 1511 | cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime, |
| 1512 | st->min_vdisktime); |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 1513 | cfq_log_cfqq(cfqq->cfqd, cfqq, |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1514 | "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu", |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 1515 | used_sl, cfqq->slice_dispatch, charge, |
| 1516 | iops_mode(cfqd), cfqq->nr_sectors); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1517 | cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl); |
| 1518 | cfqg_stats_set_start_empty_time(cfqg); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1521 | /** |
| 1522 | * cfq_init_cfqg_base - initialize base part of a cfq_group |
| 1523 | * @cfqg: cfq_group to initialize |
| 1524 | * |
| 1525 | * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED |
| 1526 | * is enabled or not. |
| 1527 | */ |
| 1528 | static void cfq_init_cfqg_base(struct cfq_group *cfqg) |
| 1529 | { |
| 1530 | struct cfq_rb_root *st; |
| 1531 | int i, j; |
| 1532 | |
| 1533 | for_each_cfqg_st(cfqg, i, j, st) |
| 1534 | *st = CFQ_RB_ROOT; |
| 1535 | RB_CLEAR_NODE(&cfqg->rb_node); |
| 1536 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 1537 | cfqg->ttime.last_end_request = ktime_get_ns(); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1540 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1541 | static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val, |
| 1542 | bool on_dfl, bool reset_dev, bool is_leaf_weight); |
| 1543 | |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1544 | static void cfqg_stats_exit(struct cfqg_stats *stats) |
Peter Zijlstra | 90d3839 | 2013-11-12 19:42:14 -0800 | [diff] [blame] | 1545 | { |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1546 | blkg_rwstat_exit(&stats->merged); |
| 1547 | blkg_rwstat_exit(&stats->service_time); |
| 1548 | blkg_rwstat_exit(&stats->wait_time); |
| 1549 | blkg_rwstat_exit(&stats->queued); |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1550 | blkg_stat_exit(&stats->time); |
| 1551 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 1552 | blkg_stat_exit(&stats->unaccounted_time); |
| 1553 | blkg_stat_exit(&stats->avg_queue_size_sum); |
| 1554 | blkg_stat_exit(&stats->avg_queue_size_samples); |
| 1555 | blkg_stat_exit(&stats->dequeue); |
| 1556 | blkg_stat_exit(&stats->group_wait_time); |
| 1557 | blkg_stat_exit(&stats->idle_time); |
| 1558 | blkg_stat_exit(&stats->empty_time); |
| 1559 | #endif |
| 1560 | } |
| 1561 | |
| 1562 | static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp) |
| 1563 | { |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 1564 | if (blkg_rwstat_init(&stats->merged, gfp) || |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1565 | blkg_rwstat_init(&stats->service_time, gfp) || |
| 1566 | blkg_rwstat_init(&stats->wait_time, gfp) || |
| 1567 | blkg_rwstat_init(&stats->queued, gfp) || |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1568 | blkg_stat_init(&stats->time, gfp)) |
| 1569 | goto err; |
Peter Zijlstra | 90d3839 | 2013-11-12 19:42:14 -0800 | [diff] [blame] | 1570 | |
| 1571 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1572 | if (blkg_stat_init(&stats->unaccounted_time, gfp) || |
| 1573 | blkg_stat_init(&stats->avg_queue_size_sum, gfp) || |
| 1574 | blkg_stat_init(&stats->avg_queue_size_samples, gfp) || |
| 1575 | blkg_stat_init(&stats->dequeue, gfp) || |
| 1576 | blkg_stat_init(&stats->group_wait_time, gfp) || |
| 1577 | blkg_stat_init(&stats->idle_time, gfp) || |
| 1578 | blkg_stat_init(&stats->empty_time, gfp)) |
| 1579 | goto err; |
Peter Zijlstra | 90d3839 | 2013-11-12 19:42:14 -0800 | [diff] [blame] | 1580 | #endif |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1581 | return 0; |
| 1582 | err: |
| 1583 | cfqg_stats_exit(stats); |
| 1584 | return -ENOMEM; |
Peter Zijlstra | 90d3839 | 2013-11-12 19:42:14 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1587 | static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp) |
| 1588 | { |
| 1589 | struct cfq_group_data *cgd; |
| 1590 | |
Tejun Heo | ebc4ff6 | 2016-11-10 11:16:37 -0500 | [diff] [blame] | 1591 | cgd = kzalloc(sizeof(*cgd), gfp); |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1592 | if (!cgd) |
| 1593 | return NULL; |
| 1594 | return &cgd->cpd; |
| 1595 | } |
| 1596 | |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 1597 | static void cfq_cpd_init(struct blkcg_policy_data *cpd) |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1598 | { |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 1599 | struct cfq_group_data *cgd = cpd_to_cfqgd(cpd); |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1600 | unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ? |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1601 | CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1602 | |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1603 | if (cpd_to_blkcg(cpd) == &blkcg_root) |
| 1604 | weight *= 2; |
| 1605 | |
| 1606 | cgd->weight = weight; |
| 1607 | cgd->leaf_weight = weight; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1608 | } |
| 1609 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1610 | static void cfq_cpd_free(struct blkcg_policy_data *cpd) |
| 1611 | { |
| 1612 | kfree(cpd_to_cfqgd(cpd)); |
| 1613 | } |
| 1614 | |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1615 | static void cfq_cpd_bind(struct blkcg_policy_data *cpd) |
| 1616 | { |
| 1617 | struct blkcg *blkcg = cpd_to_blkcg(cpd); |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 1618 | bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys); |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1619 | unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL; |
| 1620 | |
| 1621 | if (blkcg == &blkcg_root) |
| 1622 | weight *= 2; |
| 1623 | |
| 1624 | WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false)); |
| 1625 | WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true)); |
| 1626 | } |
| 1627 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1628 | static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node) |
| 1629 | { |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 1630 | struct cfq_group *cfqg; |
| 1631 | |
| 1632 | cfqg = kzalloc_node(sizeof(*cfqg), gfp, node); |
| 1633 | if (!cfqg) |
| 1634 | return NULL; |
| 1635 | |
| 1636 | cfq_init_cfqg_base(cfqg); |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1637 | if (cfqg_stats_init(&cfqg->stats, gfp)) { |
| 1638 | kfree(cfqg); |
| 1639 | return NULL; |
| 1640 | } |
Tejun Heo | b2ce264 | 2015-08-18 14:55:13 -0700 | [diff] [blame] | 1641 | |
| 1642 | return &cfqg->pd; |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1645 | static void cfq_pd_init(struct blkg_policy_data *pd) |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1646 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1647 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
| 1648 | struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1649 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1650 | cfqg->weight = cgd->weight; |
| 1651 | cfqg->leaf_weight = cgd->leaf_weight; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1652 | } |
| 1653 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1654 | static void cfq_pd_offline(struct blkg_policy_data *pd) |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1655 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1656 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 1657 | int i; |
| 1658 | |
| 1659 | for (i = 0; i < IOPRIO_BE_NR; i++) { |
| 1660 | if (cfqg->async_cfqq[0][i]) |
| 1661 | cfq_put_queue(cfqg->async_cfqq[0][i]); |
| 1662 | if (cfqg->async_cfqq[1][i]) |
| 1663 | cfq_put_queue(cfqg->async_cfqq[1][i]); |
| 1664 | } |
| 1665 | |
| 1666 | if (cfqg->async_idle_cfqq) |
| 1667 | cfq_put_queue(cfqg->async_idle_cfqq); |
| 1668 | |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1669 | /* |
| 1670 | * @blkg is going offline and will be ignored by |
| 1671 | * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so |
| 1672 | * that they don't get lost. If IOs complete after this point, the |
| 1673 | * stats for them will be lost. Oh well... |
| 1674 | */ |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 1675 | cfqg_stats_xfer_dead(cfqg); |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1678 | static void cfq_pd_free(struct blkg_policy_data *pd) |
| 1679 | { |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 1680 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
| 1681 | |
| 1682 | cfqg_stats_exit(&cfqg->stats); |
| 1683 | return kfree(cfqg); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1686 | static void cfq_pd_reset_stats(struct blkg_policy_data *pd) |
Tejun Heo | 689665a | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1687 | { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1688 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 689665a | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1689 | |
| 1690 | cfqg_stats_reset(&cfqg->stats); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1691 | } |
| 1692 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1693 | static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd, |
| 1694 | struct blkcg *blkcg) |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1695 | { |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1696 | struct blkcg_gq *blkg; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1697 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 1698 | blkg = blkg_lookup(blkcg, cfqd->queue); |
| 1699 | if (likely(blkg)) |
| 1700 | return blkg_to_cfqg(blkg); |
| 1701 | return NULL; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) |
| 1705 | { |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1706 | cfqq->cfqg = cfqg; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1707 | /* cfqq reference on cfqg */ |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 1708 | cfqg_get(cfqg); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1711 | static u64 cfqg_prfill_weight_device(struct seq_file *sf, |
| 1712 | struct blkg_policy_data *pd, int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1713 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1714 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1715 | |
| 1716 | if (!cfqg->dev_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1717 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1718 | return __blkg_prfill_u64(sf, pd, cfqg->dev_weight); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1721 | static int cfqg_print_weight_device(struct seq_file *sf, void *v) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1722 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1723 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1724 | cfqg_prfill_weight_device, &blkcg_policy_cfq, |
| 1725 | 0, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1729 | static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf, |
| 1730 | struct blkg_policy_data *pd, int off) |
| 1731 | { |
| 1732 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
| 1733 | |
| 1734 | if (!cfqg->dev_leaf_weight) |
| 1735 | return 0; |
| 1736 | return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight); |
| 1737 | } |
| 1738 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1739 | static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1740 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1741 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1742 | cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, |
| 1743 | 0, false); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1744 | return 0; |
| 1745 | } |
| 1746 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1747 | static int cfq_print_weight(struct seq_file *sf, void *v) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1748 | { |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1749 | struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); |
Jens Axboe | 9470e4a | 2015-06-19 10:19:36 -0600 | [diff] [blame] | 1750 | struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); |
| 1751 | unsigned int val = 0; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1752 | |
Jens Axboe | 9470e4a | 2015-06-19 10:19:36 -0600 | [diff] [blame] | 1753 | if (cgd) |
| 1754 | val = cgd->weight; |
| 1755 | |
| 1756 | seq_printf(sf, "%u\n", val); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1760 | static int cfq_print_leaf_weight(struct seq_file *sf, void *v) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1761 | { |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1762 | struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); |
Jens Axboe | 9470e4a | 2015-06-19 10:19:36 -0600 | [diff] [blame] | 1763 | struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); |
| 1764 | unsigned int val = 0; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1765 | |
Jens Axboe | 9470e4a | 2015-06-19 10:19:36 -0600 | [diff] [blame] | 1766 | if (cgd) |
| 1767 | val = cgd->leaf_weight; |
| 1768 | |
| 1769 | seq_printf(sf, "%u\n", val); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1770 | return 0; |
| 1771 | } |
| 1772 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1773 | static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of, |
| 1774 | char *buf, size_t nbytes, loff_t off, |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1775 | bool on_dfl, bool is_leaf_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1776 | { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1777 | unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN; |
| 1778 | unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1779 | struct blkcg *blkcg = css_to_blkcg(of_css(of)); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1780 | struct blkg_conf_ctx ctx; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1781 | struct cfq_group *cfqg; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1782 | struct cfq_group_data *cfqgd; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1783 | int ret; |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1784 | u64 v; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1785 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1786 | ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1787 | if (ret) |
| 1788 | return ret; |
| 1789 | |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1790 | if (sscanf(ctx.body, "%llu", &v) == 1) { |
| 1791 | /* require "default" on dfl */ |
| 1792 | ret = -ERANGE; |
| 1793 | if (!v && on_dfl) |
| 1794 | goto out_finish; |
| 1795 | } else if (!strcmp(strim(ctx.body), "default")) { |
| 1796 | v = 0; |
| 1797 | } else { |
| 1798 | ret = -EINVAL; |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1799 | goto out_finish; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1800 | } |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1801 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1802 | cfqg = blkg_to_cfqg(ctx.blkg); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1803 | cfqgd = blkcg_to_cfqgd(blkcg); |
Jens Axboe | ae994ea | 2015-06-20 10:26:50 -0600 | [diff] [blame] | 1804 | |
Tejun Heo | 20386ce | 2015-08-18 14:55:28 -0700 | [diff] [blame] | 1805 | ret = -ERANGE; |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1806 | if (!v || (v >= min && v <= max)) { |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1807 | if (!is_leaf_weight) { |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1808 | cfqg->dev_weight = v; |
| 1809 | cfqg->new_weight = v ?: cfqgd->weight; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1810 | } else { |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1811 | cfqg->dev_leaf_weight = v; |
| 1812 | cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1813 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1814 | ret = 0; |
| 1815 | } |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 1816 | out_finish: |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1817 | blkg_conf_finish(&ctx); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1818 | return ret ?: nbytes; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1821 | static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of, |
| 1822 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1823 | { |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1824 | return __cfqg_set_weight_device(of, buf, nbytes, off, false, false); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 1827 | static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of, |
| 1828 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1829 | { |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 1830 | return __cfqg_set_weight_device(of, buf, nbytes, off, false, true); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1831 | } |
| 1832 | |
Tejun Heo | dd165eb | 2015-08-18 14:55:33 -0700 | [diff] [blame] | 1833 | static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val, |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1834 | bool on_dfl, bool reset_dev, bool is_leaf_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1835 | { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1836 | unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN; |
| 1837 | unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1838 | struct blkcg *blkcg = css_to_blkcg(css); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1839 | struct blkcg_gq *blkg; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1840 | struct cfq_group_data *cfqgd; |
Jens Axboe | ae994ea | 2015-06-20 10:26:50 -0600 | [diff] [blame] | 1841 | int ret = 0; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1842 | |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1843 | if (val < min || val > max) |
| 1844 | return -ERANGE; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1845 | |
| 1846 | spin_lock_irq(&blkcg->lock); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1847 | cfqgd = blkcg_to_cfqgd(blkcg); |
Jens Axboe | ae994ea | 2015-06-20 10:26:50 -0600 | [diff] [blame] | 1848 | if (!cfqgd) { |
| 1849 | ret = -EINVAL; |
| 1850 | goto out; |
| 1851 | } |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1852 | |
| 1853 | if (!is_leaf_weight) |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1854 | cfqgd->weight = val; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1855 | else |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1856 | cfqgd->leaf_weight = val; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1857 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1858 | hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1859 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1860 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1861 | if (!cfqg) |
| 1862 | continue; |
| 1863 | |
| 1864 | if (!is_leaf_weight) { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1865 | if (reset_dev) |
| 1866 | cfqg->dev_weight = 0; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1867 | if (!cfqg->dev_weight) |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1868 | cfqg->new_weight = cfqgd->weight; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1869 | } else { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1870 | if (reset_dev) |
| 1871 | cfqg->dev_leaf_weight = 0; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1872 | if (!cfqg->dev_leaf_weight) |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 1873 | cfqg->new_leaf_weight = cfqgd->leaf_weight; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1874 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Jens Axboe | ae994ea | 2015-06-20 10:26:50 -0600 | [diff] [blame] | 1877 | out: |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1878 | spin_unlock_irq(&blkcg->lock); |
Jens Axboe | ae994ea | 2015-06-20 10:26:50 -0600 | [diff] [blame] | 1879 | return ret; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1882 | static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft, |
| 1883 | u64 val) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1884 | { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1885 | return __cfq_set_weight(css, val, false, false, false); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1888 | static int cfq_set_leaf_weight(struct cgroup_subsys_state *css, |
| 1889 | struct cftype *cft, u64 val) |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1890 | { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 1891 | return __cfq_set_weight(css, val, false, false, true); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1892 | } |
| 1893 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1894 | static int cfqg_print_stat(struct seq_file *sf, void *v) |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1895 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1896 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat, |
| 1897 | &blkcg_policy_cfq, seq_cft(sf)->private, false); |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1898 | return 0; |
| 1899 | } |
| 1900 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1901 | static int cfqg_print_rwstat(struct seq_file *sf, void *v) |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1902 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1903 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat, |
| 1904 | &blkcg_policy_cfq, seq_cft(sf)->private, true); |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1905 | return 0; |
| 1906 | } |
| 1907 | |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1908 | static u64 cfqg_prfill_stat_recursive(struct seq_file *sf, |
| 1909 | struct blkg_policy_data *pd, int off) |
| 1910 | { |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame] | 1911 | u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd), |
| 1912 | &blkcg_policy_cfq, off); |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1913 | return __blkg_prfill_u64(sf, pd, sum); |
| 1914 | } |
| 1915 | |
| 1916 | static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf, |
| 1917 | struct blkg_policy_data *pd, int off) |
| 1918 | { |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame] | 1919 | struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd), |
| 1920 | &blkcg_policy_cfq, off); |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1921 | return __blkg_prfill_rwstat(sf, pd, &sum); |
| 1922 | } |
| 1923 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1924 | static int cfqg_print_stat_recursive(struct seq_file *sf, void *v) |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1925 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1926 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1927 | cfqg_prfill_stat_recursive, &blkcg_policy_cfq, |
| 1928 | seq_cft(sf)->private, false); |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
| 1931 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1932 | static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v) |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1933 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1934 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1935 | cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq, |
| 1936 | seq_cft(sf)->private, true); |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 1937 | return 0; |
| 1938 | } |
| 1939 | |
Tejun Heo | 702747c | 2015-08-18 14:55:25 -0700 | [diff] [blame] | 1940 | static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd, |
| 1941 | int off) |
| 1942 | { |
| 1943 | u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes); |
| 1944 | |
| 1945 | return __blkg_prfill_u64(sf, pd, sum >> 9); |
| 1946 | } |
| 1947 | |
| 1948 | static int cfqg_print_stat_sectors(struct seq_file *sf, void *v) |
| 1949 | { |
| 1950 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1951 | cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false); |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf, |
| 1956 | struct blkg_policy_data *pd, int off) |
| 1957 | { |
| 1958 | struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL, |
| 1959 | offsetof(struct blkcg_gq, stat_bytes)); |
| 1960 | u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + |
| 1961 | atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); |
| 1962 | |
| 1963 | return __blkg_prfill_u64(sf, pd, sum >> 9); |
| 1964 | } |
| 1965 | |
| 1966 | static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v) |
| 1967 | { |
| 1968 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1969 | cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0, |
| 1970 | false); |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1974 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1975 | static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf, |
| 1976 | struct blkg_policy_data *pd, int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1977 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1978 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1979 | u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1980 | u64 v = 0; |
| 1981 | |
| 1982 | if (samples) { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1983 | v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum); |
Anatol Pomozov | f3cff25 | 2013-09-22 12:43:47 -0600 | [diff] [blame] | 1984 | v = div64_u64(v, samples); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1985 | } |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1986 | __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | /* print avg_queue_size */ |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1991 | static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1992 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 1993 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
| 1994 | cfqg_prfill_avg_queue_size, &blkcg_policy_cfq, |
| 1995 | 0, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1996 | return 0; |
| 1997 | } |
| 1998 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 1999 | |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 2000 | static struct cftype cfq_blkcg_legacy_files[] = { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 2001 | /* on root, weight is mapped to leaf_weight */ |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2002 | { |
| 2003 | .name = "weight_device", |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 2004 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2005 | .seq_show = cfqg_print_leaf_weight_device, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2006 | .write = cfqg_set_leaf_weight_device, |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 2007 | }, |
| 2008 | { |
| 2009 | .name = "weight", |
| 2010 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2011 | .seq_show = cfq_print_leaf_weight, |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 2012 | .write_u64 = cfq_set_leaf_weight, |
| 2013 | }, |
| 2014 | |
| 2015 | /* no such mapping necessary for !roots */ |
| 2016 | { |
| 2017 | .name = "weight_device", |
| 2018 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2019 | .seq_show = cfqg_print_weight_device, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2020 | .write = cfqg_set_weight_device, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2021 | }, |
| 2022 | { |
| 2023 | .name = "weight", |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 2024 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2025 | .seq_show = cfq_print_weight, |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2026 | .write_u64 = cfq_set_weight, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2027 | }, |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 2028 | |
| 2029 | { |
| 2030 | .name = "leaf_weight_device", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2031 | .seq_show = cfqg_print_leaf_weight_device, |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 2032 | .write = cfqg_set_leaf_weight_device, |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 2033 | }, |
| 2034 | { |
| 2035 | .name = "leaf_weight", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2036 | .seq_show = cfq_print_leaf_weight, |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 2037 | .write_u64 = cfq_set_leaf_weight, |
| 2038 | }, |
| 2039 | |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2040 | /* statistics, covers only the tasks in the cfqg */ |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2041 | { |
| 2042 | .name = "time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2043 | .private = offsetof(struct cfq_group, stats.time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2044 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2045 | }, |
| 2046 | { |
| 2047 | .name = "sectors", |
Tejun Heo | 702747c | 2015-08-18 14:55:25 -0700 | [diff] [blame] | 2048 | .seq_show = cfqg_print_stat_sectors, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2049 | }, |
| 2050 | { |
| 2051 | .name = "io_service_bytes", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 2052 | .private = (unsigned long)&blkcg_policy_cfq, |
| 2053 | .seq_show = blkg_print_stat_bytes, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2054 | }, |
| 2055 | { |
| 2056 | .name = "io_serviced", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 2057 | .private = (unsigned long)&blkcg_policy_cfq, |
| 2058 | .seq_show = blkg_print_stat_ios, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2059 | }, |
| 2060 | { |
| 2061 | .name = "io_service_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2062 | .private = offsetof(struct cfq_group, stats.service_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2063 | .seq_show = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2064 | }, |
| 2065 | { |
| 2066 | .name = "io_wait_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2067 | .private = offsetof(struct cfq_group, stats.wait_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2068 | .seq_show = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2069 | }, |
| 2070 | { |
| 2071 | .name = "io_merged", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2072 | .private = offsetof(struct cfq_group, stats.merged), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2073 | .seq_show = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2074 | }, |
| 2075 | { |
| 2076 | .name = "io_queued", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2077 | .private = offsetof(struct cfq_group, stats.queued), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2078 | .seq_show = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2079 | }, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2080 | |
| 2081 | /* the same statictics which cover the cfqg and its descendants */ |
| 2082 | { |
| 2083 | .name = "time_recursive", |
| 2084 | .private = offsetof(struct cfq_group, stats.time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2085 | .seq_show = cfqg_print_stat_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2086 | }, |
| 2087 | { |
| 2088 | .name = "sectors_recursive", |
Tejun Heo | 702747c | 2015-08-18 14:55:25 -0700 | [diff] [blame] | 2089 | .seq_show = cfqg_print_stat_sectors_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2090 | }, |
| 2091 | { |
| 2092 | .name = "io_service_bytes_recursive", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 2093 | .private = (unsigned long)&blkcg_policy_cfq, |
| 2094 | .seq_show = blkg_print_stat_bytes_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2095 | }, |
| 2096 | { |
| 2097 | .name = "io_serviced_recursive", |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 2098 | .private = (unsigned long)&blkcg_policy_cfq, |
| 2099 | .seq_show = blkg_print_stat_ios_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2100 | }, |
| 2101 | { |
| 2102 | .name = "io_service_time_recursive", |
| 2103 | .private = offsetof(struct cfq_group, stats.service_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2104 | .seq_show = cfqg_print_rwstat_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2105 | }, |
| 2106 | { |
| 2107 | .name = "io_wait_time_recursive", |
| 2108 | .private = offsetof(struct cfq_group, stats.wait_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2109 | .seq_show = cfqg_print_rwstat_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2110 | }, |
| 2111 | { |
| 2112 | .name = "io_merged_recursive", |
| 2113 | .private = offsetof(struct cfq_group, stats.merged), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2114 | .seq_show = cfqg_print_rwstat_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2115 | }, |
| 2116 | { |
| 2117 | .name = "io_queued_recursive", |
| 2118 | .private = offsetof(struct cfq_group, stats.queued), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2119 | .seq_show = cfqg_print_rwstat_recursive, |
Tejun Heo | 4311401 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 2120 | }, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2121 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 2122 | { |
| 2123 | .name = "avg_queue_size", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2124 | .seq_show = cfqg_print_avg_queue_size, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2125 | }, |
| 2126 | { |
| 2127 | .name = "group_wait_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2128 | .private = offsetof(struct cfq_group, stats.group_wait_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2129 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2130 | }, |
| 2131 | { |
| 2132 | .name = "idle_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2133 | .private = offsetof(struct cfq_group, stats.idle_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2134 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2135 | }, |
| 2136 | { |
| 2137 | .name = "empty_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2138 | .private = offsetof(struct cfq_group, stats.empty_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2139 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2140 | }, |
| 2141 | { |
| 2142 | .name = "dequeue", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2143 | .private = offsetof(struct cfq_group, stats.dequeue), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2144 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2145 | }, |
| 2146 | { |
| 2147 | .name = "unaccounted_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 2148 | .private = offsetof(struct cfq_group, stats.unaccounted_time), |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2149 | .seq_show = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 2150 | }, |
| 2151 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 2152 | { } /* terminate */ |
| 2153 | }; |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 2154 | |
| 2155 | static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v) |
| 2156 | { |
| 2157 | struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); |
| 2158 | struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg); |
| 2159 | |
| 2160 | seq_printf(sf, "default %u\n", cgd->weight); |
| 2161 | blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device, |
| 2162 | &blkcg_policy_cfq, 0, false); |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of, |
| 2167 | char *buf, size_t nbytes, loff_t off) |
| 2168 | { |
| 2169 | char *endp; |
| 2170 | int ret; |
| 2171 | u64 v; |
| 2172 | |
| 2173 | buf = strim(buf); |
| 2174 | |
| 2175 | /* "WEIGHT" or "default WEIGHT" sets the default weight */ |
| 2176 | v = simple_strtoull(buf, &endp, 0); |
| 2177 | if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) { |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 2178 | ret = __cfq_set_weight(of_css(of), v, true, false, false); |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 2179 | return ret ?: nbytes; |
| 2180 | } |
| 2181 | |
| 2182 | /* "MAJ:MIN WEIGHT" */ |
| 2183 | return __cfqg_set_weight_device(of, buf, nbytes, off, true, false); |
| 2184 | } |
| 2185 | |
| 2186 | static struct cftype cfq_blkcg_files[] = { |
| 2187 | { |
| 2188 | .name = "weight", |
| 2189 | .flags = CFTYPE_NOT_ON_ROOT, |
| 2190 | .seq_show = cfq_print_weight_on_dfl, |
| 2191 | .write = cfq_set_weight_on_dfl, |
| 2192 | }, |
| 2193 | { } /* terminate */ |
| 2194 | }; |
| 2195 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2196 | #else /* GROUP_IOSCHED */ |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 2197 | static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd, |
| 2198 | struct blkcg *blkcg) |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2199 | { |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 2200 | return cfqd->root_group; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2201 | } |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 2202 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2203 | static inline void |
| 2204 | cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) { |
| 2205 | cfqq->cfqg = cfqg; |
| 2206 | } |
| 2207 | |
| 2208 | #endif /* GROUP_IOSCHED */ |
| 2209 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2210 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2211 | * The cfqd->service_trees holds all pending cfq_queue's that have |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2212 | * requests waiting to be processed. It is sorted in the order that |
| 2213 | * we will service the queues. |
| 2214 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2215 | static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2216 | bool add_front) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2217 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2218 | struct rb_node **p, *parent; |
| 2219 | struct cfq_queue *__cfqq; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2220 | u64 rb_key; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2221 | struct cfq_rb_root *st; |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2222 | int left; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2223 | int new_cfqq = 1; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2224 | u64 now = ktime_get_ns(); |
Vivek Goyal | ae30c28 | 2009-12-03 12:59:55 -0500 | [diff] [blame] | 2225 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2226 | st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq)); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2227 | if (cfq_class_idle(cfqq)) { |
| 2228 | rb_key = CFQ_IDLE_DELAY; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2229 | parent = rb_last(&st->rb); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2230 | if (parent && parent != &cfqq->rb_node) { |
| 2231 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 2232 | rb_key += __cfqq->rb_key; |
| 2233 | } else |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2234 | rb_key += now; |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2235 | } else if (!add_front) { |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 2236 | /* |
| 2237 | * Get our rb key offset. Subtract any residual slice |
| 2238 | * value carried from last service. A negative resid |
| 2239 | * count indicates slice overrun, and this should position |
| 2240 | * the next service time further away in the tree. |
| 2241 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2242 | rb_key = cfq_slice_offset(cfqd, cfqq) + now; |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 2243 | rb_key -= cfqq->slice_resid; |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2244 | cfqq->slice_resid = 0; |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 2245 | } else { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2246 | rb_key = -NSEC_PER_SEC; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2247 | __cfqq = cfq_rb_first(st); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2248 | rb_key += __cfqq ? __cfqq->rb_key : now; |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 2249 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2250 | |
| 2251 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2252 | new_cfqq = 0; |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 2253 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2254 | * same position, nothing more to do |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 2255 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2256 | if (rb_key == cfqq->rb_key && cfqq->service_tree == st) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2257 | return; |
Jens Axboe | 53b03744 | 2006-07-28 09:48:51 +0200 | [diff] [blame] | 2258 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 2259 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 2260 | cfqq->service_tree = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2261 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2262 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2263 | left = 1; |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2264 | parent = NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2265 | cfqq->service_tree = st; |
| 2266 | p = &st->rb.rb_node; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2267 | while (*p) { |
| 2268 | parent = *p; |
| 2269 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 2270 | |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 2271 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2272 | * sort by key, that represents service time. |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 2273 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2274 | if (rb_key < __cfqq->rb_key) |
Vivek Goyal | 1f23f12 | 2012-10-03 16:57:00 -0400 | [diff] [blame] | 2275 | p = &parent->rb_left; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2276 | else { |
Vivek Goyal | 1f23f12 | 2012-10-03 16:57:00 -0400 | [diff] [blame] | 2277 | p = &parent->rb_right; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 2278 | left = 0; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2279 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 2282 | if (left) |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2283 | st->left = &cfqq->rb_node; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 2284 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2285 | cfqq->rb_key = rb_key; |
| 2286 | rb_link_node(&cfqq->rb_node, parent, p); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2287 | rb_insert_color(&cfqq->rb_node, &st->rb); |
| 2288 | st->count++; |
Namhyung Kim | 20359f2 | 2011-05-24 10:23:22 +0200 | [diff] [blame] | 2289 | if (add_front || !new_cfqq) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2290 | return; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 2291 | cfq_group_notify_queue_add(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2294 | static struct cfq_queue * |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2295 | cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root, |
| 2296 | sector_t sector, struct rb_node **ret_parent, |
| 2297 | struct rb_node ***rb_link) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2298 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2299 | struct rb_node **p, *parent; |
| 2300 | struct cfq_queue *cfqq = NULL; |
| 2301 | |
| 2302 | parent = NULL; |
| 2303 | p = &root->rb_node; |
| 2304 | while (*p) { |
| 2305 | struct rb_node **n; |
| 2306 | |
| 2307 | parent = *p; |
| 2308 | cfqq = rb_entry(parent, struct cfq_queue, p_node); |
| 2309 | |
| 2310 | /* |
| 2311 | * Sort strictly based on sector. Smallest to the left, |
| 2312 | * largest to the right. |
| 2313 | */ |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 2314 | if (sector > blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2315 | n = &(*p)->rb_right; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 2316 | else if (sector < blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2317 | n = &(*p)->rb_left; |
| 2318 | else |
| 2319 | break; |
| 2320 | p = n; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 2321 | cfqq = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | *ret_parent = parent; |
| 2325 | if (rb_link) |
| 2326 | *rb_link = p; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 2327 | return cfqq; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2331 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2332 | struct rb_node **p, *parent; |
| 2333 | struct cfq_queue *__cfqq; |
| 2334 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2335 | if (cfqq->p_root) { |
| 2336 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 2337 | cfqq->p_root = NULL; |
| 2338 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2339 | |
| 2340 | if (cfq_class_idle(cfqq)) |
| 2341 | return; |
| 2342 | if (!cfqq->next_rq) |
| 2343 | return; |
| 2344 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2345 | cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio]; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 2346 | __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root, |
| 2347 | blk_rq_pos(cfqq->next_rq), &parent, &p); |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 2348 | if (!__cfqq) { |
| 2349 | rb_link_node(&cfqq->p_node, parent, p); |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2350 | rb_insert_color(&cfqq->p_node, cfqq->p_root); |
| 2351 | } else |
| 2352 | cfqq->p_root = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2355 | /* |
| 2356 | * Update cfqq's position in the service tree. |
| 2357 | */ |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2358 | static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2359 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2360 | /* |
| 2361 | * Resorting requires the cfqq to be on the RR list already. |
| 2362 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2363 | if (cfq_cfqq_on_rr(cfqq)) { |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2364 | cfq_service_tree_add(cfqd, cfqq, 0); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2365 | cfq_prio_tree_add(cfqd, cfqq); |
| 2366 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | /* |
| 2370 | * add to busy list of queues for service, trying to be fair in ordering |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2371 | * the pending list according to last request service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2373 | static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2375 | cfq_log_cfqq(cfqd, cfqq, "add_to_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2376 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
| 2377 | cfq_mark_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | cfqd->busy_queues++; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 2379 | if (cfq_cfqq_sync(cfqq)) |
| 2380 | cfqd->busy_sync_queues++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2382 | cfq_resort_rr_list(cfqd, cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | } |
| 2384 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2385 | /* |
| 2386 | * Called when the cfqq no longer has requests pending, remove it from |
| 2387 | * the service tree. |
| 2388 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2389 | static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2391 | cfq_log_cfqq(cfqd, cfqq, "del_from_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2392 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
| 2393 | cfq_clear_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 2395 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
| 2396 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 2397 | cfqq->service_tree = NULL; |
| 2398 | } |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2399 | if (cfqq->p_root) { |
| 2400 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 2401 | cfqq->p_root = NULL; |
| 2402 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2403 | |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 2404 | cfq_group_notify_queue_del(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | BUG_ON(!cfqd->busy_queues); |
| 2406 | cfqd->busy_queues--; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 2407 | if (cfq_cfqq_sync(cfqq)) |
| 2408 | cfqd->busy_sync_queues--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | /* |
| 2412 | * rb tree support functions |
| 2413 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2414 | static void cfq_del_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2416 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2417 | const int sync = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2419 | BUG_ON(!cfqq->queued[sync]); |
| 2420 | cfqq->queued[sync]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2422 | elv_rb_del(&cfqq->sort_list, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2424 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) { |
| 2425 | /* |
| 2426 | * Queue will be deleted from service tree when we actually |
| 2427 | * expire it later. Right now just remove it from prio tree |
| 2428 | * as it is empty. |
| 2429 | */ |
| 2430 | if (cfqq->p_root) { |
| 2431 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 2432 | cfqq->p_root = NULL; |
| 2433 | } |
| 2434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2437 | static void cfq_add_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2439 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | struct cfq_data *cfqd = cfqq->cfqd; |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 2441 | struct request *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2443 | cfqq->queued[rq_is_sync(rq)]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 2445 | elv_rb_add(&cfqq->sort_list, rq); |
Jens Axboe | 5fccbf6 | 2006-10-31 14:21:55 +0100 | [diff] [blame] | 2446 | |
| 2447 | if (!cfq_cfqq_on_rr(cfqq)) |
| 2448 | cfq_add_cfqq_rr(cfqd, cfqq); |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 2449 | |
| 2450 | /* |
| 2451 | * check if this request is a better next-serve candidate |
| 2452 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2453 | prev = cfqq->next_rq; |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2454 | cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2455 | |
| 2456 | /* |
| 2457 | * adjust priority tree position, if ->next_rq changes |
| 2458 | */ |
| 2459 | if (prev != cfqq->next_rq) |
| 2460 | cfq_prio_tree_add(cfqd, cfqq); |
| 2461 | |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 2462 | BUG_ON(!cfqq->next_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2465 | static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | { |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2467 | elv_rb_del(&cfqq->sort_list, rq); |
| 2468 | cfqq->queued[rq_is_sync(rq)]--; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 2469 | cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2470 | cfq_add_rq_rb(rq); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2471 | cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 2472 | rq->cmd_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2475 | static struct request * |
| 2476 | cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | { |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2478 | struct task_struct *tsk = current; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2479 | struct cfq_io_cq *cic; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2480 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2482 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2483 | if (!cic) |
| 2484 | return NULL; |
| 2485 | |
Christoph Hellwig | aa39ebd | 2016-11-01 07:40:02 -0600 | [diff] [blame] | 2486 | cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf)); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2487 | if (cfqq) |
| 2488 | return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | return NULL; |
| 2491 | } |
| 2492 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2493 | static void cfq_activate_request(struct request_queue *q, struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2494 | { |
| 2495 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2496 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2497 | cfqd->rq_in_driver++; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2498 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2499 | cfqd->rq_in_driver); |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 2500 | |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 2501 | cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2502 | } |
| 2503 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2504 | static void cfq_deactivate_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2506 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2508 | WARN_ON(!cfqd->rq_in_driver); |
| 2509 | cfqd->rq_in_driver--; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2510 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2511 | cfqd->rq_in_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | } |
| 2513 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2514 | static void cfq_remove_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2516 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2517 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2518 | if (cfqq->next_rq == rq) |
| 2519 | cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2521 | list_del_init(&rq->queuelist); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2522 | cfq_del_rq_rb(rq); |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 2523 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 2524 | cfqq->cfqd->rq_queued--; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 2525 | cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 2526 | if (rq->cmd_flags & REQ_PRIO) { |
| 2527 | WARN_ON(!cfqq->prio_pending); |
| 2528 | cfqq->prio_pending--; |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 2529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 2532 | static enum elv_merge cfq_merge(struct request_queue *q, struct request **req, |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2533 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | { |
| 2535 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2536 | struct request *__rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2538 | __rq = cfq_find_rq_fmerge(cfqd, bio); |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 2539 | if (__rq && elv_bio_merge_ok(__rq, bio)) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 2540 | *req = __rq; |
| 2541 | return ELEVATOR_FRONT_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | return ELEVATOR_NO_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2547 | static void cfq_merged_request(struct request_queue *q, struct request *req, |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 2548 | enum elv_merge type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2550 | if (type == ELEVATOR_FRONT_MERGE) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2551 | struct cfq_queue *cfqq = RQ_CFQQ(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2553 | cfq_reposition_rq_rb(cfqq, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 2557 | static void cfq_bio_merged(struct request_queue *q, struct request *req, |
| 2558 | struct bio *bio) |
| 2559 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 2560 | cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | static void |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2564 | cfq_merged_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | struct request *next) |
| 2566 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2567 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Shaohua Li | 4a0b75c | 2011-12-16 14:00:22 +0100 | [diff] [blame] | 2568 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2569 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2570 | /* |
| 2571 | * reposition in fifo if next is older than rq |
| 2572 | */ |
| 2573 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2574 | next->fifo_time < rq->fifo_time && |
Shaohua Li | 3d106fba | 2012-11-06 12:39:51 +0100 | [diff] [blame] | 2575 | cfqq == RQ_CFQQ(next)) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2576 | list_move(&rq->queuelist, &next->queuelist); |
Jan Kara | 8b4922d | 2014-02-24 16:39:52 +0100 | [diff] [blame] | 2577 | rq->fifo_time = next->fifo_time; |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 2578 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2579 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2580 | if (cfqq->next_rq == next) |
| 2581 | cfqq->next_rq = rq; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2582 | cfq_remove_request(next); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 2583 | cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags); |
Shaohua Li | 4a0b75c | 2011-12-16 14:00:22 +0100 | [diff] [blame] | 2584 | |
| 2585 | cfqq = RQ_CFQQ(next); |
| 2586 | /* |
| 2587 | * all requests of this queue are merged to other queues, delete it |
| 2588 | * from the service tree. If it's the active_queue, |
| 2589 | * cfq_dispatch_requests() will choose to expire it or do idle |
| 2590 | */ |
| 2591 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) && |
| 2592 | cfqq != cfqd->active_queue) |
| 2593 | cfq_del_cfqq_rr(cfqd, cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2594 | } |
| 2595 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 2596 | static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq, |
| 2597 | struct bio *bio) |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2598 | { |
| 2599 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Christoph Hellwig | aa39ebd | 2016-11-01 07:40:02 -0600 | [diff] [blame] | 2600 | bool is_sync = op_is_sync(bio->bi_opf); |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2601 | struct cfq_io_cq *cic; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2602 | struct cfq_queue *cfqq; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2603 | |
| 2604 | /* |
Jens Axboe | ec8acb6 | 2007-01-02 18:32:11 +0100 | [diff] [blame] | 2605 | * Disallow merge of a sync bio into an async request. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2606 | */ |
Christoph Hellwig | aa39ebd | 2016-11-01 07:40:02 -0600 | [diff] [blame] | 2607 | if (is_sync && !rq_is_sync(rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2608 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* |
Tejun Heo | f1a4f4d | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 2611 | * Lookup the cfqq that this bio will be queued with and allow |
Tejun Heo | 07c2bd3 | 2012-02-08 09:19:42 +0100 | [diff] [blame] | 2612 | * merge only if rq is queued there. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2613 | */ |
Tejun Heo | 07c2bd3 | 2012-02-08 09:19:42 +0100 | [diff] [blame] | 2614 | cic = cfq_cic_lookup(cfqd, current->io_context); |
| 2615 | if (!cic) |
| 2616 | return false; |
Jens Axboe | 719d340 | 2006-12-22 09:38:53 +0100 | [diff] [blame] | 2617 | |
Christoph Hellwig | aa39ebd | 2016-11-01 07:40:02 -0600 | [diff] [blame] | 2618 | cfqq = cic_to_cfqq(cic, is_sync); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2619 | return cfqq == RQ_CFQQ(rq); |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2620 | } |
| 2621 | |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 2622 | static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq, |
| 2623 | struct request *next) |
| 2624 | { |
| 2625 | return RQ_CFQQ(rq) == RQ_CFQQ(next); |
| 2626 | } |
| 2627 | |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2628 | static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2629 | { |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 2630 | hrtimer_try_to_cancel(&cfqd->idle_slice_timer); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2631 | cfqg_stats_update_idle_time(cfqq->cfqg); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2634 | static void __cfq_set_active_queue(struct cfq_data *cfqd, |
| 2635 | struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2636 | { |
| 2637 | if (cfqq) { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2638 | cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d", |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2639 | cfqd->serving_wl_class, cfqd->serving_wl_type); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2640 | cfqg_stats_update_avg_queue_size(cfqq->cfqg); |
Justin TerAvest | 62a37f6 | 2011-03-23 08:25:44 +0100 | [diff] [blame] | 2641 | cfqq->slice_start = 0; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2642 | cfqq->dispatch_start = ktime_get_ns(); |
Justin TerAvest | 62a37f6 | 2011-03-23 08:25:44 +0100 | [diff] [blame] | 2643 | cfqq->allocated_slice = 0; |
| 2644 | cfqq->slice_end = 0; |
| 2645 | cfqq->slice_dispatch = 0; |
| 2646 | cfqq->nr_sectors = 0; |
| 2647 | |
| 2648 | cfq_clear_cfqq_wait_request(cfqq); |
| 2649 | cfq_clear_cfqq_must_dispatch(cfqq); |
| 2650 | cfq_clear_cfqq_must_alloc_slice(cfqq); |
| 2651 | cfq_clear_cfqq_fifo_expire(cfqq); |
| 2652 | cfq_mark_cfqq_slice_new(cfqq); |
| 2653 | |
| 2654 | cfq_del_timer(cfqd, cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | cfqd->active_queue = cfqq; |
| 2658 | } |
| 2659 | |
| 2660 | /* |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2661 | * current cfqq expired its slice (or was too idle), select new one |
| 2662 | */ |
| 2663 | static void |
| 2664 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2665 | bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2666 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2667 | cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out); |
| 2668 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2669 | if (cfq_cfqq_wait_request(cfqq)) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2670 | cfq_del_timer(cfqd, cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2671 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2672 | cfq_clear_cfqq_wait_request(cfqq); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 2673 | cfq_clear_cfqq_wait_busy(cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2674 | |
| 2675 | /* |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 2676 | * If this cfqq is shared between multiple processes, check to |
| 2677 | * make sure that those processes are still issuing I/Os within |
| 2678 | * the mean seek distance. If not, it may be time to break the |
| 2679 | * queues apart again. |
| 2680 | */ |
| 2681 | if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq)) |
| 2682 | cfq_mark_cfqq_split_coop(cfqq); |
| 2683 | |
| 2684 | /* |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2685 | * store what was left of this slice, if the queue idled/timed out |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2686 | */ |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 2687 | if (timed_out) { |
| 2688 | if (cfq_cfqq_slice_new(cfqq)) |
Vivek Goyal | ba5bd52 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 2689 | cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq); |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 2690 | else |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2691 | cfqq->slice_resid = cfqq->slice_end - ktime_get_ns(); |
Jan Kara | 93fdf14 | 2016-06-28 09:04:00 +0200 | [diff] [blame] | 2692 | cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid); |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2693 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2694 | |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2695 | cfq_group_served(cfqd, cfqq->cfqg, cfqq); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2696 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2697 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 2698 | cfq_del_cfqq_rr(cfqd, cfqq); |
| 2699 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2700 | cfq_resort_rr_list(cfqd, cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2701 | |
| 2702 | if (cfqq == cfqd->active_queue) |
| 2703 | cfqd->active_queue = NULL; |
| 2704 | |
| 2705 | if (cfqd->active_cic) { |
Tejun Heo | 11a3122 | 2012-02-07 07:51:30 +0100 | [diff] [blame] | 2706 | put_io_context(cfqd->active_cic->icq.ioc); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2707 | cfqd->active_cic = NULL; |
| 2708 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2709 | } |
| 2710 | |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2711 | static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2712 | { |
| 2713 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 2714 | |
| 2715 | if (cfqq) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2716 | __cfq_slice_expired(cfqd, cfqq, timed_out); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2719 | /* |
| 2720 | * Get next queue for service. Unless we have a queue preemption, |
| 2721 | * we'll simply select the first cfqq in the service tree. |
| 2722 | */ |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2723 | static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2724 | { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2725 | struct cfq_rb_root *st = st_for(cfqd->serving_group, |
| 2726 | cfqd->serving_wl_class, cfqd->serving_wl_type); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2727 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2728 | if (!cfqd->rq_queued) |
| 2729 | return NULL; |
| 2730 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2731 | /* There is nothing to dispatch */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2732 | if (!st) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2733 | return NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2734 | if (RB_EMPTY_ROOT(&st->rb)) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2735 | return NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2736 | return cfq_rb_first(st); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2737 | } |
| 2738 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2739 | static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd) |
| 2740 | { |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2741 | struct cfq_group *cfqg; |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2742 | struct cfq_queue *cfqq; |
| 2743 | int i, j; |
| 2744 | struct cfq_rb_root *st; |
| 2745 | |
| 2746 | if (!cfqd->rq_queued) |
| 2747 | return NULL; |
| 2748 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2749 | cfqg = cfq_get_next_cfqg(cfqd); |
| 2750 | if (!cfqg) |
| 2751 | return NULL; |
| 2752 | |
Markus Elfring | 1cf4175 | 2017-01-21 22:44:07 +0100 | [diff] [blame] | 2753 | for_each_cfqg_st(cfqg, i, j, st) { |
| 2754 | cfqq = cfq_rb_first(st); |
| 2755 | if (cfqq) |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2756 | return cfqq; |
Markus Elfring | 1cf4175 | 2017-01-21 22:44:07 +0100 | [diff] [blame] | 2757 | } |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2758 | return NULL; |
| 2759 | } |
| 2760 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2761 | /* |
| 2762 | * Get and set a new active queue for service. |
| 2763 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2764 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, |
| 2765 | struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2766 | { |
Jens Axboe | e00ef79 | 2009-11-04 08:54:55 +0100 | [diff] [blame] | 2767 | if (!cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2768 | cfqq = cfq_get_next_queue(cfqd); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2769 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2770 | __cfq_set_active_queue(cfqd, cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2771 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2774 | static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, |
| 2775 | struct request *rq) |
| 2776 | { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 2777 | if (blk_rq_pos(rq) >= cfqd->last_position) |
| 2778 | return blk_rq_pos(rq) - cfqd->last_position; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2779 | else |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 2780 | return cfqd->last_position - blk_rq_pos(rq); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2781 | } |
| 2782 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2783 | static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2784 | struct request *rq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2785 | { |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2786 | return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2789 | static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, |
| 2790 | struct cfq_queue *cur_cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2791 | { |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2792 | struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio]; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2793 | struct rb_node *parent, *node; |
| 2794 | struct cfq_queue *__cfqq; |
| 2795 | sector_t sector = cfqd->last_position; |
| 2796 | |
| 2797 | if (RB_EMPTY_ROOT(root)) |
| 2798 | return NULL; |
| 2799 | |
| 2800 | /* |
| 2801 | * First, if we find a request starting at the end of the last |
| 2802 | * request, choose it. |
| 2803 | */ |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2804 | __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2805 | if (__cfqq) |
| 2806 | return __cfqq; |
| 2807 | |
| 2808 | /* |
| 2809 | * If the exact sector wasn't found, the parent of the NULL leaf |
| 2810 | * will contain the closest sector. |
| 2811 | */ |
| 2812 | __cfqq = rb_entry(parent, struct cfq_queue, p_node); |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2813 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2814 | return __cfqq; |
| 2815 | |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 2816 | if (blk_rq_pos(__cfqq->next_rq) < sector) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2817 | node = rb_next(&__cfqq->p_node); |
| 2818 | else |
| 2819 | node = rb_prev(&__cfqq->p_node); |
| 2820 | if (!node) |
| 2821 | return NULL; |
| 2822 | |
| 2823 | __cfqq = rb_entry(node, struct cfq_queue, p_node); |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2824 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2825 | return __cfqq; |
| 2826 | |
| 2827 | return NULL; |
| 2828 | } |
| 2829 | |
| 2830 | /* |
| 2831 | * cfqd - obvious |
| 2832 | * cur_cfqq - passed in so that we don't decide that the current queue is |
| 2833 | * closely cooperating with itself. |
| 2834 | * |
| 2835 | * So, basically we're assuming that that cur_cfqq has dispatched at least |
| 2836 | * one request, and that cfqd->last_position reflects a position on the disk |
| 2837 | * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid |
| 2838 | * assumption. |
| 2839 | */ |
| 2840 | static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 2841 | struct cfq_queue *cur_cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2842 | { |
| 2843 | struct cfq_queue *cfqq; |
| 2844 | |
Divyesh Shah | 39c01b2 | 2010-03-25 15:45:57 +0100 | [diff] [blame] | 2845 | if (cfq_class_idle(cur_cfqq)) |
| 2846 | return NULL; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2847 | if (!cfq_cfqq_sync(cur_cfqq)) |
| 2848 | return NULL; |
| 2849 | if (CFQQ_SEEKY(cur_cfqq)) |
| 2850 | return NULL; |
| 2851 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2852 | /* |
Gui Jianfeng | b9d8f4c | 2009-12-08 08:54:17 +0100 | [diff] [blame] | 2853 | * Don't search priority tree if it's the only queue in the group. |
| 2854 | */ |
| 2855 | if (cur_cfqq->cfqg->nr_cfqq == 1) |
| 2856 | return NULL; |
| 2857 | |
| 2858 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2859 | * We should notice if some of the queues are cooperating, eg |
| 2860 | * working closely on the same area of the disk. In that case, |
| 2861 | * we can group them together and don't waste time idling. |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2862 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2863 | cfqq = cfqq_close(cfqd, cur_cfqq); |
| 2864 | if (!cfqq) |
| 2865 | return NULL; |
| 2866 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 2867 | /* If new queue belongs to different cfq_group, don't choose it */ |
| 2868 | if (cur_cfqq->cfqg != cfqq->cfqg) |
| 2869 | return NULL; |
| 2870 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2871 | /* |
| 2872 | * It only makes sense to merge sync queues. |
| 2873 | */ |
| 2874 | if (!cfq_cfqq_sync(cfqq)) |
| 2875 | return NULL; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2876 | if (CFQQ_SEEKY(cfqq)) |
| 2877 | return NULL; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2878 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2879 | /* |
| 2880 | * Do not merge queues of different priority classes |
| 2881 | */ |
| 2882 | if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq)) |
| 2883 | return NULL; |
| 2884 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2885 | return cfqq; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2886 | } |
| 2887 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2888 | /* |
| 2889 | * Determine whether we should enforce idle window for this queue. |
| 2890 | */ |
| 2891 | |
| 2892 | static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2893 | { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2894 | enum wl_class_t wl_class = cfqq_class(cfqq); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2895 | struct cfq_rb_root *st = cfqq->service_tree; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2896 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2897 | BUG_ON(!st); |
| 2898 | BUG_ON(!st->count); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2899 | |
Vivek Goyal | b6508c1 | 2010-08-23 12:23:33 +0200 | [diff] [blame] | 2900 | if (!cfqd->cfq_slice_idle) |
| 2901 | return false; |
| 2902 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2903 | /* We never do for idle class queues. */ |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2904 | if (wl_class == IDLE_WORKLOAD) |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2905 | return false; |
| 2906 | |
| 2907 | /* We do for queues that were marked with idle window flag. */ |
Shaohua Li | 3c764b7 | 2009-12-04 13:12:06 +0100 | [diff] [blame] | 2908 | if (cfq_cfqq_idle_window(cfqq) && |
| 2909 | !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)) |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2910 | return true; |
| 2911 | |
| 2912 | /* |
| 2913 | * Otherwise, we do only if they are the last ones |
| 2914 | * in their service tree. |
| 2915 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2916 | if (st->count == 1 && cfq_cfqq_sync(cfqq) && |
| 2917 | !cfq_io_thinktime_big(cfqd, &st->ttime, false)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 2918 | return true; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2919 | cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count); |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 2920 | return false; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2923 | static void cfq_arm_slice_timer(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2924 | { |
Jens Axboe | 1792669 | 2007-01-19 11:59:30 +1100 | [diff] [blame] | 2925 | struct cfq_queue *cfqq = cfqd->active_queue; |
Jan Kara | e795421 | 2016-01-12 16:24:15 +0100 | [diff] [blame] | 2926 | struct cfq_rb_root *st = cfqq->service_tree; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2927 | struct cfq_io_cq *cic; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2928 | u64 sl, group_idle = 0; |
| 2929 | u64 now = ktime_get_ns(); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2930 | |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2931 | /* |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 2932 | * SSD device without seek penalty, disable idling. But only do so |
| 2933 | * for devices that support queuing, otherwise we still have a problem |
| 2934 | * with sync vs async workloads. |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2935 | */ |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 2936 | if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag) |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2937 | return; |
| 2938 | |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 2939 | WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2940 | WARN_ON(cfq_cfqq_slice_new(cfqq)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2941 | |
| 2942 | /* |
| 2943 | * idle is disabled, either manually or by past process history |
| 2944 | */ |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2945 | if (!cfq_should_idle(cfqd, cfqq)) { |
| 2946 | /* no queue idling. Check for group idling */ |
| 2947 | if (cfqd->cfq_group_idle) |
| 2948 | group_idle = cfqd->cfq_group_idle; |
| 2949 | else |
| 2950 | return; |
| 2951 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2952 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2953 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2954 | * still active requests from this queue, don't idle |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2955 | */ |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2956 | if (cfqq->dispatched) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2957 | return; |
| 2958 | |
| 2959 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2960 | * task has exited, don't wait |
| 2961 | */ |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2962 | cic = cfqd->active_cic; |
Tejun Heo | f6e8d01 | 2012-03-05 13:15:26 -0800 | [diff] [blame] | 2963 | if (!cic || !atomic_read(&cic->icq.ioc->active_ref)) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2964 | return; |
| 2965 | |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2966 | /* |
| 2967 | * If our average think time is larger than the remaining time |
| 2968 | * slice, then don't idle. This avoids overrunning the allotted |
| 2969 | * time slice. |
| 2970 | */ |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 2971 | if (sample_valid(cic->ttime.ttime_samples) && |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2972 | (cfqq->slice_end - now < cic->ttime.ttime_mean)) { |
| 2973 | cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu", |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 2974 | cic->ttime.ttime_mean); |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2975 | return; |
Divyesh Shah | b1ffe73 | 2010-03-25 15:45:03 +0100 | [diff] [blame] | 2976 | } |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2977 | |
Jan Kara | e795421 | 2016-01-12 16:24:15 +0100 | [diff] [blame] | 2978 | /* |
| 2979 | * There are other queues in the group or this is the only group and |
| 2980 | * it has too big thinktime, don't do group idle. |
| 2981 | */ |
| 2982 | if (group_idle && |
| 2983 | (cfqq->cfqg->nr_cfqq > 1 || |
| 2984 | cfq_io_thinktime_big(cfqd, &st->ttime, true))) |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2985 | return; |
| 2986 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2987 | cfq_mark_cfqq_wait_request(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2988 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2989 | if (group_idle) |
| 2990 | sl = cfqd->cfq_group_idle; |
| 2991 | else |
| 2992 | sl = cfqd->cfq_slice_idle; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2993 | |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 2994 | hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl), |
| 2995 | HRTIMER_MODE_REL); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2996 | cfqg_stats_set_start_idle_time(cfqq->cfqg); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 2997 | cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl, |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2998 | group_idle ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 3001 | /* |
| 3002 | * Move request from internal lists to the request queue dispatch list. |
| 3003 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3004 | static void cfq_dispatch_insert(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | { |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3006 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3007 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3008 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3009 | cfq_log_cfqq(cfqd, cfqq, "dispatch_insert"); |
| 3010 | |
Jeff Moyer | 06d2188 | 2009-09-11 17:08:59 +0200 | [diff] [blame] | 3011 | cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq); |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 3012 | cfq_remove_request(rq); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3013 | cfqq->dispatched++; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3014 | (RQ_CFQG(rq))->dispatched++; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 3015 | elv_dispatch_sort(q, rq); |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3016 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3017 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++; |
Vivek Goyal | c4e7893 | 2010-08-23 12:25:03 +0200 | [diff] [blame] | 3018 | cfqq->nr_sectors += blk_rq_sectors(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | /* |
| 3022 | * return expired entry, or NULL to just start from scratch in rbtree |
| 3023 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 3024 | static struct request *cfq_check_fifo(struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | { |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 3026 | struct request *rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3028 | if (cfq_cfqq_fifo_expire(cfqq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | return NULL; |
Jens Axboe | cb88741 | 2007-01-19 12:01:16 +1100 | [diff] [blame] | 3030 | |
| 3031 | cfq_mark_cfqq_fifo_expire(cfqq); |
| 3032 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3033 | if (list_empty(&cfqq->fifo)) |
| 3034 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3036 | rq = rq_entry_fifo(cfqq->fifo.next); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3037 | if (ktime_get_ns() < rq->fifo_time) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3038 | rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3040 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3043 | static inline int |
| 3044 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3045 | { |
| 3046 | const int base_rq = cfqd->cfq_slice_async_rq; |
| 3047 | |
| 3048 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
| 3049 | |
Namhyung Kim | b9f8ce0 | 2011-05-24 10:23:21 +0200 | [diff] [blame] | 3050 | return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3054 | * Must be called with the queue_lock held. |
| 3055 | */ |
| 3056 | static int cfqq_process_refs(struct cfq_queue *cfqq) |
| 3057 | { |
| 3058 | int process_refs, io_refs; |
| 3059 | |
| 3060 | io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE]; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3061 | process_refs = cfqq->ref - io_refs; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3062 | BUG_ON(process_refs < 0); |
| 3063 | return process_refs; |
| 3064 | } |
| 3065 | |
| 3066 | static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq) |
| 3067 | { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3068 | int process_refs, new_process_refs; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3069 | struct cfq_queue *__cfqq; |
| 3070 | |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 3071 | /* |
| 3072 | * If there are no process references on the new_cfqq, then it is |
| 3073 | * unsafe to follow the ->new_cfqq chain as other cfqq's in the |
| 3074 | * chain may have dropped their last reference (not just their |
| 3075 | * last process reference). |
| 3076 | */ |
| 3077 | if (!cfqq_process_refs(new_cfqq)) |
| 3078 | return; |
| 3079 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3080 | /* Avoid a circular list and skip interim queue merges */ |
| 3081 | while ((__cfqq = new_cfqq->new_cfqq)) { |
| 3082 | if (__cfqq == cfqq) |
| 3083 | return; |
| 3084 | new_cfqq = __cfqq; |
| 3085 | } |
| 3086 | |
| 3087 | process_refs = cfqq_process_refs(cfqq); |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 3088 | new_process_refs = cfqq_process_refs(new_cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3089 | /* |
| 3090 | * If the process for the cfqq has gone away, there is no |
| 3091 | * sense in merging the queues. |
| 3092 | */ |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 3093 | if (process_refs == 0 || new_process_refs == 0) |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3094 | return; |
| 3095 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3096 | /* |
| 3097 | * Merge in the direction of the lesser amount of work. |
| 3098 | */ |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3099 | if (new_process_refs >= process_refs) { |
| 3100 | cfqq->new_cfqq = new_cfqq; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3101 | new_cfqq->ref += process_refs; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3102 | } else { |
| 3103 | new_cfqq->new_cfqq = cfqq; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3104 | cfqq->ref += new_process_refs; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 3105 | } |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3106 | } |
| 3107 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 3108 | static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd, |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 3109 | struct cfq_group *cfqg, enum wl_class_t wl_class) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3110 | { |
| 3111 | struct cfq_queue *queue; |
| 3112 | int i; |
| 3113 | bool key_valid = false; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3114 | u64 lowest_key = 0; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3115 | enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD; |
| 3116 | |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 3117 | for (i = 0; i <= SYNC_WORKLOAD; ++i) { |
| 3118 | /* select the one with lowest rb_key */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3119 | queue = cfq_rb_first(st_for(cfqg, wl_class, i)); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3120 | if (queue && |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3121 | (!key_valid || queue->rb_key < lowest_key)) { |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3122 | lowest_key = queue->rb_key; |
| 3123 | cur_best = i; |
| 3124 | key_valid = true; |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | return cur_best; |
| 3129 | } |
| 3130 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 3131 | static void |
| 3132 | choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3133 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3134 | u64 slice; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3135 | unsigned count; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3136 | struct cfq_rb_root *st; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3137 | u64 group_slice; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3138 | enum wl_class_t original_class = cfqd->serving_wl_class; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3139 | u64 now = ktime_get_ns(); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3140 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3141 | /* Choose next priority. RT > BE > IDLE */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 3142 | if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3143 | cfqd->serving_wl_class = RT_WORKLOAD; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 3144 | else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3145 | cfqd->serving_wl_class = BE_WORKLOAD; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3146 | else { |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3147 | cfqd->serving_wl_class = IDLE_WORKLOAD; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3148 | cfqd->workload_expires = now + jiffies_to_nsecs(1); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3149 | return; |
| 3150 | } |
| 3151 | |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3152 | if (original_class != cfqd->serving_wl_class) |
Shaohua Li writes | e4ea0c1 | 2010-12-13 14:32:22 +0100 | [diff] [blame] | 3153 | goto new_workload; |
| 3154 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3155 | /* |
| 3156 | * For RT and BE, we have to choose also the type |
| 3157 | * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload |
| 3158 | * expiration time |
| 3159 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3160 | st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3161 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 3164 | * check workload expiration, and that we still have other queues ready |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3165 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3166 | if (count && !(now > cfqd->workload_expires)) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3167 | return; |
| 3168 | |
Shaohua Li writes | e4ea0c1 | 2010-12-13 14:32:22 +0100 | [diff] [blame] | 3169 | new_workload: |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3170 | /* otherwise select new workload type */ |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 3171 | cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg, |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3172 | cfqd->serving_wl_class); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3173 | st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3174 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3175 | |
| 3176 | /* |
| 3177 | * the workload slice is computed as a fraction of target latency |
| 3178 | * proportional to the number of queues in that workload, over |
| 3179 | * all the queues in the same priority class |
| 3180 | */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 3181 | group_slice = cfq_group_slice(cfqd, cfqg); |
| 3182 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3183 | slice = div_u64(group_slice * count, |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3184 | max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class], |
| 3185 | cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd, |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3186 | cfqg))); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3187 | |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3188 | if (cfqd->serving_wl_type == ASYNC_WORKLOAD) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3189 | u64 tmp; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 3190 | |
| 3191 | /* |
| 3192 | * Async queues are currently system wide. Just taking |
| 3193 | * proportion of queues with-in same group will lead to higher |
| 3194 | * async ratio system wide as generally root group is going |
| 3195 | * to have higher weight. A more accurate thing would be to |
| 3196 | * calculate system wide asnc/sync ratio. |
| 3197 | */ |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 3198 | tmp = cfqd->cfq_target_latency * |
| 3199 | cfqg_busy_async_queues(cfqd, cfqg); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3200 | tmp = div_u64(tmp, cfqd->busy_queues); |
| 3201 | slice = min_t(u64, slice, tmp); |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 3202 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3203 | /* async workload slice is scaled down according to |
| 3204 | * the sync/async slice ratio. */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3205 | slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]); |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 3206 | } else |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3207 | /* sync workload slice is at least 2 * cfq_slice_idle */ |
| 3208 | slice = max(slice, 2 * cfqd->cfq_slice_idle); |
| 3209 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3210 | slice = max_t(u64, slice, CFQ_MIN_TT); |
| 3211 | cfq_log(cfqd, "workload slice:%llu", slice); |
| 3212 | cfqd->workload_expires = now + slice; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3215 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd) |
| 3216 | { |
| 3217 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 3218 | struct cfq_group *cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3219 | |
| 3220 | if (RB_EMPTY_ROOT(&st->rb)) |
| 3221 | return NULL; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 3222 | cfqg = cfq_rb_first_group(st); |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 3223 | update_min_vdisktime(st); |
| 3224 | return cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3225 | } |
| 3226 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3227 | static void cfq_choose_cfqg(struct cfq_data *cfqd) |
| 3228 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3229 | struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3230 | u64 now = ktime_get_ns(); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 3231 | |
| 3232 | cfqd->serving_group = cfqg; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 3233 | |
| 3234 | /* Restore the workload type data */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3235 | if (cfqg->saved_wl_slice) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3236 | cfqd->workload_expires = now + cfqg->saved_wl_slice; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3237 | cfqd->serving_wl_type = cfqg->saved_wl_type; |
| 3238 | cfqd->serving_wl_class = cfqg->saved_wl_class; |
Gui Jianfeng | 66ae291 | 2009-12-15 10:08:45 +0100 | [diff] [blame] | 3239 | } else |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3240 | cfqd->workload_expires = now - 1; |
Gui Jianfeng | 66ae291 | 2009-12-15 10:08:45 +0100 | [diff] [blame] | 3241 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 3242 | choose_wl_class_and_type(cfqd, cfqg); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3243 | } |
| 3244 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3245 | /* |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 3246 | * Select a queue for service. If we have a current active queue, |
| 3247 | * check whether to continue servicing it, or retrieve and set a new one. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3248 | */ |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3249 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3250 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3251 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3252 | u64 now = ktime_get_ns(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3253 | |
| 3254 | cfqq = cfqd->active_queue; |
| 3255 | if (!cfqq) |
| 3256 | goto new_queue; |
| 3257 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3258 | if (!cfqd->rq_queued) |
| 3259 | return NULL; |
Vivek Goyal | c244bb5 | 2009-12-08 17:52:57 -0500 | [diff] [blame] | 3260 | |
| 3261 | /* |
| 3262 | * We were waiting for group to get backlogged. Expire the queue |
| 3263 | */ |
| 3264 | if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 3265 | goto expire; |
| 3266 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3267 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3268 | * The active queue has run out of time, expire it and select new. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3269 | */ |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3270 | if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) { |
| 3271 | /* |
| 3272 | * If slice had not expired at the completion of last request |
| 3273 | * we might not have turned on wait_busy flag. Don't expire |
| 3274 | * the queue yet. Allow the group to get backlogged. |
| 3275 | * |
| 3276 | * The very fact that we have used the slice, that means we |
| 3277 | * have been idling all along on this queue and it should be |
| 3278 | * ok to wait for this request to complete. |
| 3279 | */ |
Vivek Goyal | 82bbbf2 | 2009-12-10 19:25:41 +0100 | [diff] [blame] | 3280 | if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list) |
| 3281 | && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { |
| 3282 | cfqq = NULL; |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3283 | goto keep_queue; |
Vivek Goyal | 82bbbf2 | 2009-12-10 19:25:41 +0100 | [diff] [blame] | 3284 | } else |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3285 | goto check_group_idle; |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3286 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3287 | |
| 3288 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3289 | * The active queue has requests and isn't expired, allow it to |
| 3290 | * dispatch. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3291 | */ |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 3292 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3293 | goto keep_queue; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3294 | |
| 3295 | /* |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3296 | * If another queue has a request waiting within our mean seek |
| 3297 | * distance, let it run. The expire code will check for close |
| 3298 | * cooperators and put the close queue at the front of the service |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3299 | * tree. If possible, merge the expiring queue with the new cfqq. |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3300 | */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 3301 | new_cfqq = cfq_close_cooperator(cfqd, cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3302 | if (new_cfqq) { |
| 3303 | if (!cfqq->new_cfqq) |
| 3304 | cfq_setup_merge(cfqq, new_cfqq); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3305 | goto expire; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3306 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3307 | |
| 3308 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3309 | * No requests pending. If the active queue still has requests in |
| 3310 | * flight or is idling for a new request, allow either of these |
| 3311 | * conditions to happen (or time out) before selecting a new queue. |
| 3312 | */ |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 3313 | if (hrtimer_active(&cfqd->idle_slice_timer)) { |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3314 | cfqq = NULL; |
| 3315 | goto keep_queue; |
| 3316 | } |
| 3317 | |
Shaohua Li | 8e1ac66 | 2010-11-08 15:01:04 +0100 | [diff] [blame] | 3318 | /* |
| 3319 | * This is a deep seek queue, but the device is much faster than |
| 3320 | * the queue can deliver, don't idle |
| 3321 | **/ |
| 3322 | if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) && |
| 3323 | (cfq_cfqq_slice_new(cfqq) || |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3324 | (cfqq->slice_end - now > now - cfqq->slice_start))) { |
Shaohua Li | 8e1ac66 | 2010-11-08 15:01:04 +0100 | [diff] [blame] | 3325 | cfq_clear_cfqq_deep(cfqq); |
| 3326 | cfq_clear_cfqq_idle_window(cfqq); |
| 3327 | } |
| 3328 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3329 | if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { |
| 3330 | cfqq = NULL; |
| 3331 | goto keep_queue; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
| 3335 | * If group idle is enabled and there are requests dispatched from |
| 3336 | * this group, wait for requests to complete. |
| 3337 | */ |
| 3338 | check_group_idle: |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 3339 | if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 && |
| 3340 | cfqq->cfqg->dispatched && |
| 3341 | !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) { |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3342 | cfqq = NULL; |
| 3343 | goto keep_queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3344 | } |
| 3345 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3346 | expire: |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3347 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3348 | new_queue: |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3349 | /* |
| 3350 | * Current queue expired. Check if we have to switch to a new |
| 3351 | * service tree |
| 3352 | */ |
| 3353 | if (!new_cfqq) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3354 | cfq_choose_cfqg(cfqd); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 3355 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3356 | cfqq = cfq_set_active_queue(cfqd, new_cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3357 | keep_queue: |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3358 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3359 | } |
| 3360 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 3361 | static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 3362 | { |
| 3363 | int dispatched = 0; |
| 3364 | |
| 3365 | while (cfqq->next_rq) { |
| 3366 | cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); |
| 3367 | dispatched++; |
| 3368 | } |
| 3369 | |
| 3370 | BUG_ON(!list_empty(&cfqq->fifo)); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3371 | |
| 3372 | /* By default cfqq is not expired if it is empty. Do it explicitly */ |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3373 | __cfq_slice_expired(cfqq->cfqd, cfqq, 0); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 3374 | return dispatched; |
| 3375 | } |
| 3376 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 3377 | /* |
| 3378 | * Drain our current requests. Used for barriers and when switching |
| 3379 | * io schedulers on-the-fly. |
| 3380 | */ |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 3381 | static int cfq_forced_dispatch(struct cfq_data *cfqd) |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3382 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 3383 | struct cfq_queue *cfqq; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 3384 | int dispatched = 0; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3385 | |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 3386 | /* Expire the timeslice of the current active queue first */ |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3387 | cfq_slice_expired(cfqd, 0); |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 3388 | while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) { |
| 3389 | __cfq_set_active_queue(cfqd, cfqq); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3390 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 3391 | } |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3392 | |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3393 | BUG_ON(cfqd->busy_queues); |
| 3394 | |
Jeff Moyer | 6923715 | 2009-06-12 15:29:30 +0200 | [diff] [blame] | 3395 | cfq_log(cfqd, "forced_dispatch=%d", dispatched); |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3396 | return dispatched; |
| 3397 | } |
| 3398 | |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3399 | static inline bool cfq_slice_used_soon(struct cfq_data *cfqd, |
| 3400 | struct cfq_queue *cfqq) |
| 3401 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3402 | u64 now = ktime_get_ns(); |
| 3403 | |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3404 | /* the queue hasn't finished any request, can't estimate */ |
| 3405 | if (cfq_cfqq_slice_new(cfqq)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3406 | return true; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3407 | if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3408 | return true; |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3409 | |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3410 | return false; |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3413 | static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3414 | { |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3415 | unsigned int max_dispatch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | |
Glauber Costa | 3932a86 | 2016-09-22 20:59:59 -0400 | [diff] [blame] | 3417 | if (cfq_cfqq_must_dispatch(cfqq)) |
| 3418 | return true; |
| 3419 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3420 | /* |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3421 | * Drain async requests before we start sync IO |
| 3422 | */ |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3423 | if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC]) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3424 | return false; |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3425 | |
| 3426 | /* |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3427 | * If this is an async queue and we have sync IO in flight, let it wait |
| 3428 | */ |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3429 | if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3430 | return false; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3431 | |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3432 | max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3433 | if (cfq_class_idle(cfqq)) |
| 3434 | max_dispatch = 1; |
| 3435 | |
| 3436 | /* |
| 3437 | * Does this cfqq already have too much IO in flight? |
| 3438 | */ |
| 3439 | if (cfqq->dispatched >= max_dispatch) { |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3440 | bool promote_sync = false; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3441 | /* |
| 3442 | * idle queue must always only have a single IO in flight |
| 3443 | */ |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3444 | if (cfq_class_idle(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3445 | return false; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3446 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3447 | /* |
Li, Shaohua | c4ade94 | 2011-03-23 08:30:34 +0100 | [diff] [blame] | 3448 | * If there is only one sync queue |
| 3449 | * we can ignore async queue here and give the sync |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3450 | * queue no dispatch limit. The reason is a sync queue can |
| 3451 | * preempt async queue, limiting the sync queue doesn't make |
| 3452 | * sense. This is useful for aiostress test. |
| 3453 | */ |
Li, Shaohua | c4ade94 | 2011-03-23 08:30:34 +0100 | [diff] [blame] | 3454 | if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1) |
| 3455 | promote_sync = true; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3456 | |
| 3457 | /* |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3458 | * We have other queues, don't allow more IO from this one |
| 3459 | */ |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3460 | if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) && |
| 3461 | !promote_sync) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3462 | return false; |
Jens Axboe | 9ede209 | 2007-01-19 12:11:44 +1100 | [diff] [blame] | 3463 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3464 | /* |
Shaohua Li | 474b18c | 2009-12-03 12:58:05 +0100 | [diff] [blame] | 3465 | * Sole queue user, no limit |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3466 | */ |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3467 | if (cfqd->busy_queues == 1 || promote_sync) |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3468 | max_dispatch = -1; |
| 3469 | else |
| 3470 | /* |
| 3471 | * Normally we start throttling cfqq when cfq_quantum/2 |
| 3472 | * requests have been dispatched. But we can drive |
| 3473 | * deeper queue depths at the beginning of slice |
| 3474 | * subjected to upper limit of cfq_quantum. |
| 3475 | * */ |
| 3476 | max_dispatch = cfqd->cfq_quantum; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | /* |
| 3480 | * Async queues must wait a bit before being allowed dispatch. |
| 3481 | * We also ramp up the dispatch depth gradually for async IO, |
| 3482 | * based on the last sync IO we serviced |
| 3483 | */ |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3484 | if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3485 | u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3486 | unsigned int depth; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3487 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3488 | depth = div64_u64(last_sync, cfqd->cfq_slice[1]); |
Jens Axboe | e00c54c | 2009-10-04 20:36:19 +0200 | [diff] [blame] | 3489 | if (!depth && !cfqq->dispatched) |
| 3490 | depth = 1; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3491 | if (depth < max_dispatch) |
| 3492 | max_dispatch = depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3495 | /* |
| 3496 | * If we're below the current max, allow a dispatch |
| 3497 | */ |
| 3498 | return cfqq->dispatched < max_dispatch; |
| 3499 | } |
| 3500 | |
| 3501 | /* |
| 3502 | * Dispatch a request from cfqq, moving them to the request queue |
| 3503 | * dispatch list. |
| 3504 | */ |
| 3505 | static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3506 | { |
| 3507 | struct request *rq; |
| 3508 | |
| 3509 | BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); |
| 3510 | |
Glauber Costa | 3932a86 | 2016-09-22 20:59:59 -0400 | [diff] [blame] | 3511 | rq = cfq_check_fifo(cfqq); |
| 3512 | if (rq) |
| 3513 | cfq_mark_cfqq_must_dispatch(cfqq); |
| 3514 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3515 | if (!cfq_may_dispatch(cfqd, cfqq)) |
| 3516 | return false; |
| 3517 | |
| 3518 | /* |
| 3519 | * follow expired path, else get first next available |
| 3520 | */ |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3521 | if (!rq) |
| 3522 | rq = cfqq->next_rq; |
Glauber Costa | 3932a86 | 2016-09-22 20:59:59 -0400 | [diff] [blame] | 3523 | else |
| 3524 | cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq); |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3525 | |
| 3526 | /* |
| 3527 | * insert request into driver dispatch list |
| 3528 | */ |
| 3529 | cfq_dispatch_insert(cfqd->queue, rq); |
| 3530 | |
| 3531 | if (!cfqd->active_cic) { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3532 | struct cfq_io_cq *cic = RQ_CIC(rq); |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3533 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3534 | atomic_long_inc(&cic->icq.ioc->refcount); |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3535 | cfqd->active_cic = cic; |
| 3536 | } |
| 3537 | |
| 3538 | return true; |
| 3539 | } |
| 3540 | |
| 3541 | /* |
| 3542 | * Find the cfqq that we need to service and move a request from that to the |
| 3543 | * dispatch list |
| 3544 | */ |
| 3545 | static int cfq_dispatch_requests(struct request_queue *q, int force) |
| 3546 | { |
| 3547 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 3548 | struct cfq_queue *cfqq; |
| 3549 | |
| 3550 | if (!cfqd->busy_queues) |
| 3551 | return 0; |
| 3552 | |
| 3553 | if (unlikely(force)) |
| 3554 | return cfq_forced_dispatch(cfqd); |
| 3555 | |
| 3556 | cfqq = cfq_select_queue(cfqd); |
| 3557 | if (!cfqq) |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3558 | return 0; |
| 3559 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3560 | /* |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3561 | * Dispatch a request from this cfqq, if it is allowed |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3562 | */ |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3563 | if (!cfq_dispatch_request(cfqd, cfqq)) |
| 3564 | return 0; |
| 3565 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3566 | cfqq->slice_dispatch++; |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 3567 | cfq_clear_cfqq_must_dispatch(cfqq); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3568 | |
| 3569 | /* |
| 3570 | * expire an async queue immediately if it has used up its slice. idle |
| 3571 | * queue always expire after 1 dispatch round. |
| 3572 | */ |
| 3573 | if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && |
| 3574 | cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) || |
| 3575 | cfq_class_idle(cfqq))) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3576 | cfqq->slice_end = ktime_get_ns() + 1; |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3577 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3578 | } |
| 3579 | |
Shan Wei | b217a90 | 2009-09-01 10:06:42 +0200 | [diff] [blame] | 3580 | cfq_log_cfqq(cfqd, cfqq, "dispatched a request"); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3581 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3585 | * task holds one reference to the queue, dropped when task exits. each rq |
| 3586 | * in-flight on this queue also holds a reference, dropped when rq is freed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | * |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3588 | * Each cfq queue took a reference on the parent group. Drop it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | * queue lock must be held here. |
| 3590 | */ |
| 3591 | static void cfq_put_queue(struct cfq_queue *cfqq) |
| 3592 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3593 | struct cfq_data *cfqd = cfqq->cfqd; |
Justin TerAvest | 0bbfeb8 | 2011-03-01 15:05:08 -0500 | [diff] [blame] | 3594 | struct cfq_group *cfqg; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3595 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3596 | BUG_ON(cfqq->ref <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3598 | cfqq->ref--; |
| 3599 | if (cfqq->ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | return; |
| 3601 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3602 | cfq_log_cfqq(cfqd, cfqq, "put_queue"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | BUG_ON(rb_first(&cfqq->sort_list)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3604 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3605 | cfqg = cfqq->cfqg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 3607 | if (unlikely(cfqd->active_queue == cfqq)) { |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3608 | __cfq_slice_expired(cfqd, cfqq, 0); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3609 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 3610 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3611 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3612 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | kmem_cache_free(cfq_pool, cfqq); |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 3614 | cfqg_put(cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 3617 | static void cfq_put_cooperator(struct cfq_queue *cfqq) |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3618 | { |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3619 | struct cfq_queue *__cfqq, *next; |
| 3620 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3621 | /* |
| 3622 | * If this queue was scheduled to merge with another queue, be |
| 3623 | * sure to drop the reference taken on that queue (and others in |
| 3624 | * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs. |
| 3625 | */ |
| 3626 | __cfqq = cfqq->new_cfqq; |
| 3627 | while (__cfqq) { |
| 3628 | if (__cfqq == cfqq) { |
| 3629 | WARN(1, "cfqq->new_cfqq loop detected\n"); |
| 3630 | break; |
| 3631 | } |
| 3632 | next = __cfqq->new_cfqq; |
| 3633 | cfq_put_queue(__cfqq); |
| 3634 | __cfqq = next; |
| 3635 | } |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3639 | { |
| 3640 | if (unlikely(cfqq == cfqd->active_queue)) { |
| 3641 | __cfq_slice_expired(cfqd, cfqq, 0); |
| 3642 | cfq_schedule_dispatch(cfqd); |
| 3643 | } |
| 3644 | |
| 3645 | cfq_put_cooperator(cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3646 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3647 | cfq_put_queue(cfqq); |
| 3648 | } |
| 3649 | |
Tejun Heo | 9b84cac | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 3650 | static void cfq_init_icq(struct io_cq *icq) |
| 3651 | { |
| 3652 | struct cfq_io_cq *cic = icq_to_cic(icq); |
| 3653 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3654 | cic->ttime.last_end_request = ktime_get_ns(); |
Tejun Heo | 9b84cac | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 3655 | } |
| 3656 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3657 | static void cfq_exit_icq(struct io_cq *icq) |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3658 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3659 | struct cfq_io_cq *cic = icq_to_cic(icq); |
Tejun Heo | 283287a | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 3660 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Fabio Checconi | 4faa3c8 | 2008-04-10 08:28:01 +0200 | [diff] [blame] | 3661 | |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3662 | if (cic_to_cfqq(cic, false)) { |
| 3663 | cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false)); |
| 3664 | cic_set_cfqq(cic, NULL, false); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3665 | } |
| 3666 | |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3667 | if (cic_to_cfqq(cic, true)) { |
| 3668 | cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true)); |
| 3669 | cic_set_cfqq(cic, NULL, true); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3670 | } |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3673 | static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3674 | { |
| 3675 | struct task_struct *tsk = current; |
| 3676 | int ioprio_class; |
| 3677 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3678 | if (!cfq_cfqq_prio_changed(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3679 | return; |
| 3680 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3681 | ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3682 | switch (ioprio_class) { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3683 | default: |
| 3684 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); |
| 3685 | case IOPRIO_CLASS_NONE: |
| 3686 | /* |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 3687 | * no prio set, inherit CPU scheduling settings |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3688 | */ |
| 3689 | cfqq->ioprio = task_nice_ioprio(tsk); |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 3690 | cfqq->ioprio_class = task_nice_ioclass(tsk); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3691 | break; |
| 3692 | case IOPRIO_CLASS_RT: |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3693 | cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3694 | cfqq->ioprio_class = IOPRIO_CLASS_RT; |
| 3695 | break; |
| 3696 | case IOPRIO_CLASS_BE: |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3697 | cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3698 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 3699 | break; |
| 3700 | case IOPRIO_CLASS_IDLE: |
| 3701 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; |
| 3702 | cfqq->ioprio = 7; |
| 3703 | cfq_clear_cfqq_idle_window(cfqq); |
| 3704 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | /* |
| 3708 | * keep track of original prio settings in case we have to temporarily |
| 3709 | * elevate the priority of this queue |
| 3710 | */ |
| 3711 | cfqq->org_ioprio = cfqq->ioprio; |
Jens Axboe | b8269db | 2016-06-09 15:47:29 -0600 | [diff] [blame] | 3712 | cfqq->org_ioprio_class = cfqq->ioprio_class; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3713 | cfq_clear_cfqq_prio_changed(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3716 | static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3717 | { |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3718 | int ioprio = cic->icq.ioc->ioprio; |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 3719 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 3720 | struct cfq_queue *cfqq; |
Jens Axboe | 35e6077 | 2006-06-14 09:10:45 +0200 | [diff] [blame] | 3721 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3722 | /* |
| 3723 | * Check whether ioprio has changed. The condition may trigger |
| 3724 | * spuriously on a newly created cic but there's no harm. |
| 3725 | */ |
| 3726 | if (unlikely(!cfqd) || likely(cic->ioprio == ioprio)) |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3727 | return; |
| 3728 | |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3729 | cfqq = cic_to_cfqq(cic, false); |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3730 | if (cfqq) { |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3731 | cfq_put_queue(cfqq); |
Tejun Heo | 2da8de0 | 2015-08-18 14:55:02 -0700 | [diff] [blame] | 3732 | cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio); |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3733 | cic_set_cfqq(cic, cfqq, false); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3734 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3735 | |
Tejun Heo | 563180a | 2015-08-18 14:55:00 -0700 | [diff] [blame] | 3736 | cfqq = cic_to_cfqq(cic, true); |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3737 | if (cfqq) |
| 3738 | cfq_mark_cfqq_prio_changed(cfqq); |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3739 | |
| 3740 | cic->ioprio = ioprio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3741 | } |
| 3742 | |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3743 | static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3744 | pid_t pid, bool is_sync) |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3745 | { |
| 3746 | RB_CLEAR_NODE(&cfqq->rb_node); |
| 3747 | RB_CLEAR_NODE(&cfqq->p_node); |
| 3748 | INIT_LIST_HEAD(&cfqq->fifo); |
| 3749 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3750 | cfqq->ref = 0; |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3751 | cfqq->cfqd = cfqd; |
| 3752 | |
| 3753 | cfq_mark_cfqq_prio_changed(cfqq); |
| 3754 | |
| 3755 | if (is_sync) { |
| 3756 | if (!cfq_class_idle(cfqq)) |
| 3757 | cfq_mark_cfqq_idle_window(cfqq); |
| 3758 | cfq_mark_cfqq_sync(cfqq); |
| 3759 | } |
| 3760 | cfqq->pid = pid; |
| 3761 | } |
| 3762 | |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3763 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 3764 | static bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3765 | { |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 3766 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3767 | struct cfq_queue *cfqq; |
Tejun Heo | f4da807 | 2014-09-08 08:15:20 +0900 | [diff] [blame] | 3768 | uint64_t serial_nr; |
Jens Axboe | 87760e5 | 2016-11-09 12:38:14 -0700 | [diff] [blame] | 3769 | bool nonroot_cg; |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3770 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3771 | rcu_read_lock(); |
Tejun Heo | f4da807 | 2014-09-08 08:15:20 +0900 | [diff] [blame] | 3772 | serial_nr = bio_blkcg(bio)->css.serial_nr; |
Jens Axboe | 87760e5 | 2016-11-09 12:38:14 -0700 | [diff] [blame] | 3773 | nonroot_cg = bio_blkcg(bio) != &blkcg_root; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3774 | rcu_read_unlock(); |
| 3775 | |
| 3776 | /* |
| 3777 | * Check whether blkcg has changed. The condition may trigger |
| 3778 | * spuriously on a newly created cic but there's no harm. |
| 3779 | */ |
Tejun Heo | f4da807 | 2014-09-08 08:15:20 +0900 | [diff] [blame] | 3780 | if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr)) |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 3781 | return nonroot_cg; |
Jens Axboe | 87760e5 | 2016-11-09 12:38:14 -0700 | [diff] [blame] | 3782 | |
| 3783 | /* |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3784 | * Drop reference to queues. New queues will be assigned in new |
| 3785 | * group upon arrival of fresh requests. |
| 3786 | */ |
| 3787 | cfqq = cic_to_cfqq(cic, false); |
| 3788 | if (cfqq) { |
| 3789 | cfq_log_cfqq(cfqd, cfqq, "changed cgroup"); |
| 3790 | cic_set_cfqq(cic, NULL, false); |
| 3791 | cfq_put_queue(cfqq); |
| 3792 | } |
| 3793 | |
| 3794 | cfqq = cic_to_cfqq(cic, true); |
| 3795 | if (cfqq) { |
| 3796 | cfq_log_cfqq(cfqd, cfqq, "changed cgroup"); |
| 3797 | cic_set_cfqq(cic, NULL, true); |
| 3798 | cfq_put_queue(cfqq); |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3799 | } |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3800 | |
Tejun Heo | f4da807 | 2014-09-08 08:15:20 +0900 | [diff] [blame] | 3801 | cic->blkcg_serial_nr = serial_nr; |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 3802 | return nonroot_cg; |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3803 | } |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3804 | #else |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 3805 | static inline bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) |
| 3806 | { |
| 3807 | return false; |
| 3808 | } |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3809 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 3810 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3811 | static struct cfq_queue ** |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3812 | cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio) |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3813 | { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3814 | switch (ioprio_class) { |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3815 | case IOPRIO_CLASS_RT: |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3816 | return &cfqg->async_cfqq[0][ioprio]; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3817 | case IOPRIO_CLASS_NONE: |
| 3818 | ioprio = IOPRIO_NORM; |
| 3819 | /* fall through */ |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3820 | case IOPRIO_CLASS_BE: |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3821 | return &cfqg->async_cfqq[1][ioprio]; |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3822 | case IOPRIO_CLASS_IDLE: |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3823 | return &cfqg->async_idle_cfqq; |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3824 | default: |
| 3825 | BUG(); |
| 3826 | } |
| 3827 | } |
| 3828 | |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3829 | static struct cfq_queue * |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3830 | cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic, |
Tejun Heo | 2da8de0 | 2015-08-18 14:55:02 -0700 | [diff] [blame] | 3831 | struct bio *bio) |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3832 | { |
Jeff Moyer | c6ce194 | 2015-01-12 15:21:01 -0500 | [diff] [blame] | 3833 | int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); |
| 3834 | int ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Tejun Heo | d4aad7f | 2015-08-18 14:55:04 -0700 | [diff] [blame] | 3835 | struct cfq_queue **async_cfqq = NULL; |
Tejun Heo | 4ebc1c6 | 2015-08-18 14:54:57 -0700 | [diff] [blame] | 3836 | struct cfq_queue *cfqq; |
Tejun Heo | 322731e | 2015-08-18 14:55:03 -0700 | [diff] [blame] | 3837 | struct cfq_group *cfqg; |
| 3838 | |
| 3839 | rcu_read_lock(); |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 3840 | cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio)); |
Tejun Heo | 322731e | 2015-08-18 14:55:03 -0700 | [diff] [blame] | 3841 | if (!cfqg) { |
| 3842 | cfqq = &cfqd->oom_cfqq; |
| 3843 | goto out; |
| 3844 | } |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3845 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3846 | if (!is_sync) { |
Jeff Moyer | c6ce194 | 2015-01-12 15:21:01 -0500 | [diff] [blame] | 3847 | if (!ioprio_valid(cic->ioprio)) { |
| 3848 | struct task_struct *tsk = current; |
| 3849 | ioprio = task_nice_ioprio(tsk); |
| 3850 | ioprio_class = task_nice_ioclass(tsk); |
| 3851 | } |
Tejun Heo | 60a8370 | 2015-08-18 14:55:05 -0700 | [diff] [blame] | 3852 | async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3853 | cfqq = *async_cfqq; |
Tejun Heo | 4ebc1c6 | 2015-08-18 14:54:57 -0700 | [diff] [blame] | 3854 | if (cfqq) |
| 3855 | goto out; |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Tejun Heo | e00f4f4 | 2016-11-21 18:03:32 -0500 | [diff] [blame] | 3858 | cfqq = kmem_cache_alloc_node(cfq_pool, |
| 3859 | GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN, |
Tejun Heo | d4aad7f | 2015-08-18 14:55:04 -0700 | [diff] [blame] | 3860 | cfqd->queue->node); |
| 3861 | if (!cfqq) { |
| 3862 | cfqq = &cfqd->oom_cfqq; |
| 3863 | goto out; |
| 3864 | } |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3865 | |
Alexander Potapenko | 4d608ba | 2017-01-23 15:06:43 +0100 | [diff] [blame] | 3866 | /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */ |
| 3867 | cfqq->ioprio_class = IOPRIO_CLASS_NONE; |
Tejun Heo | d4aad7f | 2015-08-18 14:55:04 -0700 | [diff] [blame] | 3868 | cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); |
| 3869 | cfq_init_prio_data(cfqq, cic); |
| 3870 | cfq_link_cfqq_cfqg(cfqq, cfqg); |
| 3871 | cfq_log_cfqq(cfqd, cfqq, "alloced"); |
| 3872 | |
| 3873 | if (async_cfqq) { |
| 3874 | /* a new async queue is created, pin and remember */ |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3875 | cfqq->ref++; |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3876 | *async_cfqq = cfqq; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3877 | } |
Tejun Heo | 4ebc1c6 | 2015-08-18 14:54:57 -0700 | [diff] [blame] | 3878 | out: |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3879 | cfqq->ref++; |
Tejun Heo | 322731e | 2015-08-18 14:55:03 -0700 | [diff] [blame] | 3880 | rcu_read_unlock(); |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3881 | return cfqq; |
| 3882 | } |
| 3883 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3884 | static void |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3885 | __cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3886 | { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3887 | u64 elapsed = ktime_get_ns() - ttime->last_end_request; |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3888 | elapsed = min(elapsed, 2UL * slice_idle); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3889 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3890 | ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 3891 | ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8); |
| 3892 | ttime->ttime_mean = div64_ul(ttime->ttime_total + 128, |
| 3893 | ttime->ttime_samples); |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | static void |
| 3897 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3898 | struct cfq_io_cq *cic) |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3899 | { |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3900 | if (cfq_cfqq_sync(cfqq)) { |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3901 | __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle); |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3902 | __cfq_update_io_thinktime(&cfqq->service_tree->ttime, |
| 3903 | cfqd->cfq_slice_idle); |
| 3904 | } |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 3905 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 3906 | __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle); |
| 3907 | #endif |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3908 | } |
| 3909 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3910 | static void |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3911 | cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3912 | struct request *rq) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3913 | { |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3914 | sector_t sdist = 0; |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 3915 | sector_t n_sec = blk_rq_sectors(rq); |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3916 | if (cfqq->last_request_pos) { |
| 3917 | if (cfqq->last_request_pos < blk_rq_pos(rq)) |
| 3918 | sdist = blk_rq_pos(rq) - cfqq->last_request_pos; |
| 3919 | else |
| 3920 | sdist = cfqq->last_request_pos - blk_rq_pos(rq); |
| 3921 | } |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3922 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3923 | cfqq->seek_history <<= 1; |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 3924 | if (blk_queue_nonrot(cfqd->queue)) |
| 3925 | cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT); |
| 3926 | else |
| 3927 | cfqq->seek_history |= (sdist > CFQQ_SEEK_THR); |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3928 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3929 | |
Christoph Hellwig | a2b8096 | 2016-11-01 07:40:09 -0600 | [diff] [blame] | 3930 | static inline bool req_noidle(struct request *req) |
| 3931 | { |
| 3932 | return req_op(req) == REQ_OP_WRITE && |
| 3933 | (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC; |
| 3934 | } |
| 3935 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3936 | /* |
| 3937 | * Disable idle window if the process thinks too long or seeks so much that |
| 3938 | * it doesn't matter |
| 3939 | */ |
| 3940 | static void |
| 3941 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3942 | struct cfq_io_cq *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3943 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3944 | int old_idle, enable_idle; |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 3945 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 3946 | /* |
| 3947 | * Don't idle for async or idle io prio class |
| 3948 | */ |
| 3949 | if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq)) |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 3950 | return; |
| 3951 | |
Jens Axboe | c265a7f | 2008-06-26 13:49:33 +0200 | [diff] [blame] | 3952 | enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3953 | |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3954 | if (cfqq->queued[0] + cfqq->queued[1] >= 4) |
| 3955 | cfq_mark_cfqq_deep(cfqq); |
| 3956 | |
Christoph Hellwig | a2b8096 | 2016-11-01 07:40:09 -0600 | [diff] [blame] | 3957 | if (cfqq->next_rq && req_noidle(cfqq->next_rq)) |
Corrado Zoccolo | 749ef9f | 2010-09-20 15:24:50 +0200 | [diff] [blame] | 3958 | enable_idle = 0; |
Tejun Heo | f6e8d01 | 2012-03-05 13:15:26 -0800 | [diff] [blame] | 3959 | else if (!atomic_read(&cic->icq.ioc->active_ref) || |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3960 | !cfqd->cfq_slice_idle || |
| 3961 | (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq))) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3962 | enable_idle = 0; |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3963 | else if (sample_valid(cic->ttime.ttime_samples)) { |
| 3964 | if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3965 | enable_idle = 0; |
| 3966 | else |
| 3967 | enable_idle = 1; |
| 3968 | } |
| 3969 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3970 | if (old_idle != enable_idle) { |
| 3971 | cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle); |
| 3972 | if (enable_idle) |
| 3973 | cfq_mark_cfqq_idle_window(cfqq); |
| 3974 | else |
| 3975 | cfq_clear_cfqq_idle_window(cfqq); |
| 3976 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3977 | } |
| 3978 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3979 | /* |
| 3980 | * Check if new_cfqq should preempt the currently active queue. Return 0 for |
| 3981 | * no or if we aren't sure, a 1 will cause a preempt. |
| 3982 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3983 | static bool |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3984 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3985 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3986 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3987 | struct cfq_queue *cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3988 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3989 | cfqq = cfqd->active_queue; |
| 3990 | if (!cfqq) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3991 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3992 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3993 | if (cfq_class_idle(new_cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3994 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3995 | |
| 3996 | if (cfq_class_idle(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3997 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3998 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3999 | /* |
Divyesh Shah | 875feb6 | 2010-01-06 18:58:20 -0800 | [diff] [blame] | 4000 | * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice. |
| 4001 | */ |
| 4002 | if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq)) |
| 4003 | return false; |
| 4004 | |
| 4005 | /* |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 4006 | * if the new request is sync, but the currently running queue is |
| 4007 | * not, let the sync request have priority. |
| 4008 | */ |
Glauber Costa | 3932a86 | 2016-09-22 20:59:59 -0400 | [diff] [blame] | 4009 | if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4010 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 4011 | |
Jan Kara | 3984aa5 | 2016-01-12 16:24:19 +0100 | [diff] [blame] | 4012 | /* |
| 4013 | * Treat ancestors of current cgroup the same way as current cgroup. |
| 4014 | * For anybody else we disallow preemption to guarantee service |
| 4015 | * fairness among cgroups. |
| 4016 | */ |
| 4017 | if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg)) |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 4018 | return false; |
| 4019 | |
| 4020 | if (cfq_slice_used(cfqq)) |
| 4021 | return true; |
| 4022 | |
Jan Kara | 6c80731c | 2016-01-12 16:24:16 +0100 | [diff] [blame] | 4023 | /* |
| 4024 | * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice. |
| 4025 | */ |
| 4026 | if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq)) |
| 4027 | return true; |
| 4028 | |
| 4029 | WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class); |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 4030 | /* Allow preemption only if we are idling on sync-noidle tree */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 4031 | if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD && |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 4032 | cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD && |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 4033 | RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 4034 | return true; |
| 4035 | |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 4036 | /* |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 4037 | * So both queues are sync. Let the new request get disk time if |
| 4038 | * it's a metadata request and the current queue is doing regular IO. |
| 4039 | */ |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 4040 | if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending) |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 4041 | return true; |
| 4042 | |
Shaohua Li | d2d59e1 | 2010-11-08 15:01:03 +0100 | [diff] [blame] | 4043 | /* An idle queue should not be idle now for some reason */ |
| 4044 | if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq)) |
| 4045 | return true; |
| 4046 | |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 4047 | if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4048 | return false; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 4049 | |
| 4050 | /* |
| 4051 | * if this request is as-good as one we would expect from the |
| 4052 | * current cfqq, let it preempt |
| 4053 | */ |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 4054 | if (cfq_rq_close(cfqd, cfqq, rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4055 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 4056 | |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4057 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | /* |
| 4061 | * cfqq preempts the active queue. if we allowed preempt with no slice left, |
| 4062 | * let it have half of its nominal slice. |
| 4063 | */ |
| 4064 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 4065 | { |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 4066 | enum wl_type_t old_type = cfqq_type(cfqd->active_queue); |
| 4067 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 4068 | cfq_log_cfqq(cfqd, cfqq, "preempt"); |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 4069 | cfq_slice_expired(cfqd, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4070 | |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 4071 | /* |
Shaohua Li | f8ae6e3 | 2011-01-14 08:41:02 +0100 | [diff] [blame] | 4072 | * workload type is changed, don't save slice, otherwise preempt |
| 4073 | * doesn't happen |
| 4074 | */ |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 4075 | if (old_type != cfqq_type(cfqq)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 4076 | cfqq->cfqg->saved_wl_slice = 0; |
Shaohua Li | f8ae6e3 | 2011-01-14 08:41:02 +0100 | [diff] [blame] | 4077 | |
| 4078 | /* |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 4079 | * Put the new queue at the front of the of the current list, |
| 4080 | * so we know that it will be selected next. |
| 4081 | */ |
| 4082 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 4083 | |
| 4084 | cfq_service_tree_add(cfqd, cfqq, 1); |
Justin TerAvest | eda5e0c | 2011-03-22 21:26:49 +0100 | [diff] [blame] | 4085 | |
Justin TerAvest | 62a37f6 | 2011-03-23 08:25:44 +0100 | [diff] [blame] | 4086 | cfqq->slice_end = 0; |
| 4087 | cfq_mark_cfqq_slice_new(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4091 | * Called when a new fs request (rq) is added (to cfqq). Check if there's |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4092 | * something we should do about it |
| 4093 | */ |
| 4094 | static void |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4095 | cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 4096 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4097 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4098 | struct cfq_io_cq *cic = RQ_CIC(rq); |
Jens Axboe | 12e9fdd | 2006-06-01 10:09:56 +0200 | [diff] [blame] | 4099 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4100 | cfqd->rq_queued++; |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 4101 | if (rq->cmd_flags & REQ_PRIO) |
| 4102 | cfqq->prio_pending++; |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 4103 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 4104 | cfq_update_io_thinktime(cfqd, cfqq, cic); |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 4105 | cfq_update_io_seektime(cfqd, cfqq, rq); |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 4106 | cfq_update_idle_window(cfqd, cfqq, cic); |
| 4107 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 4108 | cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4109 | |
| 4110 | if (cfqq == cfqd->active_queue) { |
| 4111 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 4112 | * Remember that we saw a request from this process, but |
| 4113 | * don't start queuing just yet. Otherwise we risk seeing lots |
| 4114 | * of tiny requests, because we disrupt the normal plugging |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 4115 | * and merging. If the request is already larger than a single |
| 4116 | * page, let it rip immediately. For that case we assume that |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 4117 | * merging is already done. Ditto for a busy system that |
| 4118 | * has other work pending, don't risk delaying until the |
| 4119 | * idle timer unplug to continue working. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4120 | */ |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 4121 | if (cfq_cfqq_wait_request(cfqq)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4122 | if (blk_rq_bytes(rq) > PAGE_SIZE || |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 4123 | cfqd->busy_queues > 1) { |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 4124 | cfq_del_timer(cfqd, cfqq); |
Gui Jianfeng | 554554f | 2009-12-10 09:38:39 +0100 | [diff] [blame] | 4125 | cfq_clear_cfqq_wait_request(cfqq); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 4126 | __blk_run_queue(cfqd->queue); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 4127 | } else { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 4128 | cfqg_stats_update_idle_time(cfqq->cfqg); |
Vivek Goyal | bf791937 | 2009-12-03 12:59:37 -0500 | [diff] [blame] | 4129 | cfq_mark_cfqq_must_dispatch(cfqq); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 4130 | } |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 4131 | } |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4132 | } else if (cfq_should_preempt(cfqd, cfqq, rq)) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4133 | /* |
| 4134 | * not the active queue - expire current slice if it is |
| 4135 | * idle and has expired it's mean thinktime or this new queue |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 4136 | * has some old slice time left and is of higher priority or |
| 4137 | * this new queue is RT and the current one is BE |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4138 | */ |
| 4139 | cfq_preempt_queue(cfqd, cfqq); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 4140 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4141 | } |
| 4142 | } |
| 4143 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4144 | static void cfq_insert_request(struct request_queue *q, struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4145 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4146 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4147 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4148 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 4149 | cfq_log_cfqq(cfqd, cfqq, "insert_request"); |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 4150 | cfq_init_prio_data(cfqq, RQ_CIC(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4152 | rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)]; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4153 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 4154 | cfq_add_rq_rb(rq); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4155 | cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group, |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 4156 | rq->cmd_flags); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4157 | cfq_rq_enqueued(cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | } |
| 4159 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4160 | /* |
| 4161 | * Update hw_tag based on peak queue depth over 50 samples under |
| 4162 | * sufficient load. |
| 4163 | */ |
| 4164 | static void cfq_update_hw_tag(struct cfq_data *cfqd) |
| 4165 | { |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 4166 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 4167 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4168 | if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth) |
| 4169 | cfqd->hw_tag_est_depth = cfqd->rq_in_driver; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 4170 | |
| 4171 | if (cfqd->hw_tag == 1) |
| 4172 | return; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4173 | |
| 4174 | if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4175 | cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4176 | return; |
| 4177 | |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 4178 | /* |
| 4179 | * If active queue hasn't enough requests and can idle, cfq might not |
| 4180 | * dispatch sufficient requests to hardware. Don't zero hw_tag in this |
| 4181 | * case |
| 4182 | */ |
| 4183 | if (cfqq && cfq_cfqq_idle_window(cfqq) && |
| 4184 | cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] < |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4185 | CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN) |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 4186 | return; |
| 4187 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4188 | if (cfqd->hw_tag_samples++ < 50) |
| 4189 | return; |
| 4190 | |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 4191 | if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4192 | cfqd->hw_tag = 1; |
| 4193 | else |
| 4194 | cfqd->hw_tag = 0; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4195 | } |
| 4196 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4197 | static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 4198 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4199 | struct cfq_io_cq *cic = cfqd->active_cic; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4200 | u64 now = ktime_get_ns(); |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4201 | |
Justin TerAvest | 02a8f01 | 2011-02-09 14:20:03 +0100 | [diff] [blame] | 4202 | /* If the queue already has requests, don't wait */ |
| 4203 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 4204 | return false; |
| 4205 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4206 | /* If there are other queues in the group, don't wait */ |
| 4207 | if (cfqq->cfqg->nr_cfqq > 1) |
| 4208 | return false; |
| 4209 | |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 4210 | /* the only queue in the group, but think time is big */ |
| 4211 | if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) |
| 4212 | return false; |
| 4213 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4214 | if (cfq_slice_used(cfqq)) |
| 4215 | return true; |
| 4216 | |
| 4217 | /* if slice left is less than think time, wait busy */ |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 4218 | if (cic && sample_valid(cic->ttime.ttime_samples) |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4219 | && (cfqq->slice_end - now < cic->ttime.ttime_mean)) |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4220 | return true; |
| 4221 | |
| 4222 | /* |
| 4223 | * If think times is less than a jiffy than ttime_mean=0 and above |
| 4224 | * will not be true. It might happen that slice has not expired yet |
| 4225 | * but will expire soon (4-5 ns) during select_queue(). To cover the |
| 4226 | * case where think time is less than a jiffy, mark the queue wait |
| 4227 | * busy if only 1 jiffy is left in the slice. |
| 4228 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4229 | if (cfqq->slice_end - now <= jiffies_to_nsecs(1)) |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4230 | return true; |
| 4231 | |
| 4232 | return false; |
| 4233 | } |
| 4234 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4235 | static void cfq_completed_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4237 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4238 | struct cfq_data *cfqd = cfqq->cfqd; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 4239 | const int sync = rq_is_sync(rq); |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4240 | u64 now = ktime_get_ns(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | |
Christoph Hellwig | a2b8096 | 2016-11-01 07:40:09 -0600 | [diff] [blame] | 4242 | cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 4244 | cfq_update_hw_tag(cfqd); |
| 4245 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4246 | WARN_ON(!cfqd->rq_in_driver); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 4247 | WARN_ON(!cfqq->dispatched); |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4248 | cfqd->rq_in_driver--; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 4249 | cfqq->dispatched--; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4250 | (RQ_CFQG(rq))->dispatched--; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 4251 | cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq), |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4252 | rq_io_start_time_ns(rq), rq->cmd_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4254 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 4255 | |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 4256 | if (sync) { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 4257 | struct cfq_rb_root *st; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 4258 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 4259 | RQ_CIC(rq)->ttime.last_end_request = now; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 4260 | |
| 4261 | if (cfq_cfqq_on_rr(cfqq)) |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 4262 | st = cfqq->service_tree; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 4263 | else |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 4264 | st = st_for(cfqq->cfqg, cfqq_class(cfqq), |
| 4265 | cfqq_type(cfqq)); |
| 4266 | |
| 4267 | st->ttime.last_end_request = now; |
Jan Kara | 149321a | 2016-06-28 09:04:01 +0200 | [diff] [blame] | 4268 | /* |
| 4269 | * We have to do this check in jiffies since start_time is in |
| 4270 | * jiffies and it is not trivial to convert to ns. If |
| 4271 | * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test |
| 4272 | * will become problematic but so far we are fine (the default |
| 4273 | * is 128 ms). |
| 4274 | */ |
| 4275 | if (!time_after(rq->start_time + |
| 4276 | nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]), |
| 4277 | jiffies)) |
Corrado Zoccolo | 573412b | 2009-12-06 11:48:52 +0100 | [diff] [blame] | 4278 | cfqd->last_delayed_sync = now; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 4279 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 4280 | |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 4281 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 4282 | cfqq->cfqg->ttime.last_end_request = now; |
| 4283 | #endif |
| 4284 | |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 4285 | /* |
| 4286 | * If this is the active queue, check if it needs to be expired, |
| 4287 | * or if we want to idle in case it has no pending requests. |
| 4288 | */ |
| 4289 | if (cfqd->active_queue == cfqq) { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 4290 | const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list); |
| 4291 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 4292 | if (cfq_cfqq_slice_new(cfqq)) { |
| 4293 | cfq_set_prio_slice(cfqd, cfqq); |
| 4294 | cfq_clear_cfqq_slice_new(cfqq); |
| 4295 | } |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 4296 | |
| 4297 | /* |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4298 | * Should we wait for next request to come in before we expire |
| 4299 | * the queue. |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 4300 | */ |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 4301 | if (cfq_should_wait_busy(cfqd, cfqq)) { |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4302 | u64 extend_sl = cfqd->cfq_slice_idle; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4303 | if (!cfqd->cfq_slice_idle) |
| 4304 | extend_sl = cfqd->cfq_group_idle; |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4305 | cfqq->slice_end = now + extend_sl; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 4306 | cfq_mark_cfqq_wait_busy(cfqq); |
Divyesh Shah | b1ffe73 | 2010-03-25 15:45:03 +0100 | [diff] [blame] | 4307 | cfq_log_cfqq(cfqd, cfqq, "will busy wait"); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 4308 | } |
| 4309 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 4310 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 4311 | * Idling is not enabled on: |
| 4312 | * - expired queues |
| 4313 | * - idle-priority queues |
| 4314 | * - async queues |
| 4315 | * - queues with still some requests queued |
| 4316 | * - when there is a close cooperator |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 4317 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 4318 | if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 4319 | cfq_slice_expired(cfqd, 1); |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 4320 | else if (sync && cfqq_empty && |
| 4321 | !cfq_close_cooperator(cfqd, cfqq)) { |
Corrado Zoccolo | 749ef9f | 2010-09-20 15:24:50 +0200 | [diff] [blame] | 4322 | cfq_arm_slice_timer(cfqd); |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 4323 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 4324 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 4325 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 4326 | if (!cfqd->rq_in_driver) |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4327 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | } |
| 4329 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4330 | static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op) |
Jens Axboe | b8269db | 2016-06-09 15:47:29 -0600 | [diff] [blame] | 4331 | { |
| 4332 | /* |
| 4333 | * If REQ_PRIO is set, boost class and prio level, if it's below |
| 4334 | * BE/NORM. If prio is not set, restore the potentially boosted |
| 4335 | * class/prio level. |
| 4336 | */ |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4337 | if (!(op & REQ_PRIO)) { |
Jens Axboe | b8269db | 2016-06-09 15:47:29 -0600 | [diff] [blame] | 4338 | cfqq->ioprio_class = cfqq->org_ioprio_class; |
| 4339 | cfqq->ioprio = cfqq->org_ioprio; |
| 4340 | } else { |
| 4341 | if (cfq_class_idle(cfqq)) |
| 4342 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 4343 | if (cfqq->ioprio > IOPRIO_NORM) |
| 4344 | cfqq->ioprio = IOPRIO_NORM; |
| 4345 | } |
| 4346 | } |
| 4347 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 4348 | static inline int __cfq_may_queue(struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4349 | { |
Jens Axboe | 1b379d8 | 2009-08-11 08:26:11 +0200 | [diff] [blame] | 4350 | if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4351 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4352 | return ELV_MQUEUE_MUST; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4353 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4354 | |
| 4355 | return ELV_MQUEUE_MAY; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4356 | } |
| 4357 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4358 | static int cfq_may_queue(struct request_queue *q, unsigned int op) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4359 | { |
| 4360 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 4361 | struct task_struct *tsk = current; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4362 | struct cfq_io_cq *cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4363 | struct cfq_queue *cfqq; |
| 4364 | |
| 4365 | /* |
| 4366 | * don't force setup of a queue from here, as a call to may_queue |
| 4367 | * does not necessarily imply that a request actually will be queued. |
| 4368 | * so just lookup a possibly existing queue, or return 'may queue' |
| 4369 | * if that fails |
| 4370 | */ |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 4371 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4372 | if (!cic) |
| 4373 | return ELV_MQUEUE_MAY; |
| 4374 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4375 | cfqq = cic_to_cfqq(cic, op_is_sync(op)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4376 | if (cfqq) { |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 4377 | cfq_init_prio_data(cfqq, cic); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 4378 | cfqq_boost_on_prio(cfqq, op); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4379 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 4380 | return __cfq_may_queue(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | return ELV_MQUEUE_MAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | /* |
| 4387 | * queue lock held here |
| 4388 | */ |
Jens Axboe | bb37b94 | 2006-12-01 10:42:33 +0100 | [diff] [blame] | 4389 | static void cfq_put_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4391 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4393 | if (cfqq) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4394 | const int rw = rq_data_dir(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4396 | BUG_ON(!cfqq->allocated[rw]); |
| 4397 | cfqq->allocated[rw]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 4399 | /* Put down rq reference on cfqg */ |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 4400 | cfqg_put(RQ_CFQG(rq)); |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4401 | rq->elv.priv[0] = NULL; |
| 4402 | rq->elv.priv[1] = NULL; |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 4403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | cfq_put_queue(cfqq); |
| 4405 | } |
| 4406 | } |
| 4407 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4408 | static struct cfq_queue * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4409 | cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic, |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4410 | struct cfq_queue *cfqq) |
| 4411 | { |
| 4412 | cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq); |
| 4413 | cic_set_cfqq(cic, cfqq->new_cfqq, 1); |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 4414 | cfq_mark_cfqq_coop(cfqq->new_cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4415 | cfq_put_queue(cfqq); |
| 4416 | return cic_to_cfqq(cic, 1); |
| 4417 | } |
| 4418 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4419 | /* |
| 4420 | * Returns NULL if a new cfqq should be allocated, or the old cfqq if this |
| 4421 | * was the last process referring to said cfqq. |
| 4422 | */ |
| 4423 | static struct cfq_queue * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4424 | split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq) |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4425 | { |
| 4426 | if (cfqq_process_refs(cfqq) == 1) { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4427 | cfqq->pid = current->pid; |
| 4428 | cfq_clear_cfqq_coop(cfqq); |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 4429 | cfq_clear_cfqq_split_coop(cfqq); |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4430 | return cfqq; |
| 4431 | } |
| 4432 | |
| 4433 | cic_set_cfqq(cic, NULL, 1); |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 4434 | |
| 4435 | cfq_put_cooperator(cfqq); |
| 4436 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4437 | cfq_put_queue(cfqq); |
| 4438 | return NULL; |
| 4439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4441 | * Allocate cfq data structures associated with this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4443 | static int |
Tejun Heo | 852c788 | 2012-03-05 13:15:27 -0800 | [diff] [blame] | 4444 | cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio, |
| 4445 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | { |
| 4447 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Tejun Heo | f1f8cc9 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4448 | struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | const int rw = rq_data_dir(rq); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4450 | const bool is_sync = rq_is_sync(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4451 | struct cfq_queue *cfqq; |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 4452 | bool disable_wbt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | |
Tejun Heo | 216284c3 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 4454 | spin_lock_irq(q->queue_lock); |
Tejun Heo | f1f8cc9 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4455 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 4456 | check_ioprio_changed(cic, bio); |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 4457 | disable_wbt = check_blkcg_changed(cic, bio); |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4458 | new_queue: |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4459 | cfqq = cic_to_cfqq(cic, is_sync); |
Vivek Goyal | 32f2e80 | 2009-07-09 22:13:16 +0200 | [diff] [blame] | 4460 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
Tejun Heo | bce6133 | 2015-08-18 14:54:59 -0700 | [diff] [blame] | 4461 | if (cfqq) |
| 4462 | cfq_put_queue(cfqq); |
Tejun Heo | 2da8de0 | 2015-08-18 14:55:02 -0700 | [diff] [blame] | 4463 | cfqq = cfq_get_queue(cfqd, is_sync, cic, bio); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4464 | cic_set_cfqq(cic, cfqq, is_sync); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4465 | } else { |
| 4466 | /* |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4467 | * If the queue was seeky for too long, break it apart. |
| 4468 | */ |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 4469 | if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4470 | cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq"); |
| 4471 | cfqq = split_cfqq(cic, cfqq); |
| 4472 | if (!cfqq) |
| 4473 | goto new_queue; |
| 4474 | } |
| 4475 | |
| 4476 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4477 | * Check to see if this queue is scheduled to merge with |
| 4478 | * another, closely cooperating queue. The merging of |
| 4479 | * queues happens here as it must be done in process context. |
| 4480 | * The reference on new_cfqq was taken in merge_cfqqs. |
| 4481 | */ |
| 4482 | if (cfqq->new_cfqq) |
| 4483 | cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | |
| 4486 | cfqq->allocated[rw]++; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4487 | |
Jens Axboe | 6fae9c2 | 2011-03-01 15:04:39 -0500 | [diff] [blame] | 4488 | cfqq->ref++; |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 4489 | cfqg_get(cfqq->cfqg); |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4490 | rq->elv.priv[0] = cfqq; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4491 | rq->elv.priv[1] = cfqq->cfqg; |
Tejun Heo | 216284c3 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 4492 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 5d7f5ce | 2017-02-16 07:57:33 -0700 | [diff] [blame] | 4493 | |
| 4494 | if (disable_wbt) |
| 4495 | wbt_disable_default(q); |
| 4496 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4497 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | } |
| 4499 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4500 | static void cfq_kick_queue(struct work_struct *work) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4501 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4502 | struct cfq_data *cfqd = |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4503 | container_of(work, struct cfq_data, unplug_work); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4504 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4505 | |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 4506 | spin_lock_irq(q->queue_lock); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 4507 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 4508 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | /* |
| 4512 | * Timer running if the active_queue is currently idling inside its time slice |
| 4513 | */ |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 4514 | static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4515 | { |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 4516 | struct cfq_data *cfqd = container_of(timer, struct cfq_data, |
| 4517 | idle_slice_timer); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4518 | struct cfq_queue *cfqq; |
| 4519 | unsigned long flags; |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 4520 | int timed_out = 1; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4521 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 4522 | cfq_log(cfqd, "idle timer fired"); |
| 4523 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4524 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 4525 | |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4526 | cfqq = cfqd->active_queue; |
| 4527 | if (cfqq) { |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 4528 | timed_out = 0; |
| 4529 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4530 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 4531 | * We saw a request before the queue expired, let it through |
| 4532 | */ |
| 4533 | if (cfq_cfqq_must_dispatch(cfqq)) |
| 4534 | goto out_kick; |
| 4535 | |
| 4536 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4537 | * expired |
| 4538 | */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 4539 | if (cfq_slice_used(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4540 | goto expire; |
| 4541 | |
| 4542 | /* |
| 4543 | * only expire and reinvoke request handler, if there are |
| 4544 | * other queues with pending requests |
| 4545 | */ |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 4546 | if (!cfqd->busy_queues) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4547 | goto out_cont; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4548 | |
| 4549 | /* |
| 4550 | * not expired and it has a request pending, let it dispatch |
| 4551 | */ |
Jens Axboe | 75e5098 | 2009-04-07 08:56:14 +0200 | [diff] [blame] | 4552 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4553 | goto out_kick; |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 4554 | |
| 4555 | /* |
| 4556 | * Queue depth flag is reset only when the idle didn't succeed |
| 4557 | */ |
| 4558 | cfq_clear_cfqq_deep(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4559 | } |
| 4560 | expire: |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 4561 | cfq_slice_expired(cfqd, timed_out); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4562 | out_kick: |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4563 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4564 | out_cont: |
| 4565 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 4566 | return HRTIMER_NORESTART; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4567 | } |
| 4568 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4569 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
| 4570 | { |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 4571 | hrtimer_cancel(&cfqd->idle_slice_timer); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4572 | cancel_work_sync(&cfqd->unplug_work); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4573 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4574 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4575 | static void cfq_exit_queue(struct elevator_queue *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4577 | struct cfq_data *cfqd = e->elevator_data; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4578 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4579 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4580 | cfq_shutdown_timer_wq(cfqd); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4581 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 4582 | spin_lock_irq(q->queue_lock); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4583 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 4584 | if (cfqd->active_queue) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 4585 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4586 | |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 4587 | spin_unlock_irq(q->queue_lock); |
| 4588 | |
Al Viro | a90d742 | 2006-03-18 12:05:37 -0500 | [diff] [blame] | 4589 | cfq_shutdown_timer_wq(cfqd); |
| 4590 | |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4591 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 4592 | blkcg_deactivate_policy(q, &blkcg_policy_cfq); |
| 4593 | #else |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4594 | kfree(cfqd->root_group); |
Vivek Goyal | 2abae55 | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 4595 | #endif |
Vivek Goyal | 56edf7d | 2011-05-19 15:38:22 -0400 | [diff] [blame] | 4596 | kfree(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4599 | static int cfq_init_queue(struct request_queue *q, struct elevator_type *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | { |
| 4601 | struct cfq_data *cfqd; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4602 | struct blkcg_gq *blkg __maybe_unused; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4603 | int i, ret; |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4604 | struct elevator_queue *eq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4606 | eq = elevator_alloc(q, e); |
| 4607 | if (!eq) |
Tejun Heo | b2fab5a | 2012-03-05 13:14:57 -0800 | [diff] [blame] | 4608 | return -ENOMEM; |
Konstantin Khlebnikov | 80b15c7 | 2010-05-20 23:21:41 +0400 | [diff] [blame] | 4609 | |
Joe Perches | c1b511e | 2013-08-29 15:21:42 -0700 | [diff] [blame] | 4610 | cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node); |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4611 | if (!cfqd) { |
| 4612 | kobject_put(&eq->kobj); |
| 4613 | return -ENOMEM; |
| 4614 | } |
| 4615 | eq->elevator_data = cfqd; |
| 4616 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4617 | cfqd->queue = q; |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4618 | spin_lock_irq(q->queue_lock); |
| 4619 | q->elevator = eq; |
| 4620 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4621 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 4622 | /* Init root service tree */ |
| 4623 | cfqd->grp_service_tree = CFQ_RB_ROOT; |
| 4624 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4625 | /* Init root group and prefer root group over other groups by default */ |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 4626 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4627 | ret = blkcg_activate_policy(q, &blkcg_policy_cfq); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4628 | if (ret) |
| 4629 | goto out_free; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4630 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4631 | cfqd->root_group = blkg_to_cfqg(q->root_blkg); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4632 | #else |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4633 | ret = -ENOMEM; |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4634 | cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group), |
| 4635 | GFP_KERNEL, cfqd->queue->node); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4636 | if (!cfqd->root_group) |
| 4637 | goto out_free; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4638 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4639 | cfq_init_cfqg_base(cfqd->root_group); |
Tejun Heo | 3ecca62 | 2015-08-18 14:55:35 -0700 | [diff] [blame] | 4640 | cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL; |
| 4641 | cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL; |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 4642 | #endif |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4643 | |
Jens Axboe | 26a2ac0 | 2009-04-23 12:13:27 +0200 | [diff] [blame] | 4644 | /* |
| 4645 | * Not strictly needed (since RB_ROOT just clears the node and we |
| 4646 | * zeroed cfqd on alloc), but better be safe in case someone decides |
| 4647 | * to add magic to the rb code |
| 4648 | */ |
| 4649 | for (i = 0; i < CFQ_PRIO_LISTS; i++) |
| 4650 | cfqd->prio_trees[i] = RB_ROOT; |
| 4651 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 4652 | /* |
Tejun Heo | d4aad7f | 2015-08-18 14:55:04 -0700 | [diff] [blame] | 4653 | * Our fallback cfqq if cfq_get_queue() runs into OOM issues. |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 4654 | * Grab a permanent reference to it, so that the normal code flow |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4655 | * will not attempt to free it. oom_cfqq is linked to root_group |
| 4656 | * but shouldn't hold a reference as it'll never be unlinked. Lose |
| 4657 | * the reference from linking right away. |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 4658 | */ |
| 4659 | cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 4660 | cfqd->oom_cfqq.ref++; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4661 | |
| 4662 | spin_lock_irq(q->queue_lock); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4663 | cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group); |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 4664 | cfqg_put(cfqd->root_group); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4665 | spin_unlock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | |
Jan Kara | 9114832 | 2016-06-08 15:11:39 +0200 | [diff] [blame] | 4667 | hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC, |
| 4668 | HRTIMER_MODE_REL); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4669 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4670 | |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4671 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | cfqd->cfq_quantum = cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4674 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
| 4675 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | cfqd->cfq_back_max = cfq_back_max; |
| 4677 | cfqd->cfq_back_penalty = cfq_back_penalty; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4678 | cfqd->cfq_slice[0] = cfq_slice_async; |
| 4679 | cfqd->cfq_slice[1] = cfq_slice_sync; |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4680 | cfqd->cfq_target_latency = cfq_target_latency; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4681 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; |
Jens Axboe | 0bb9794 | 2015-06-10 08:01:20 -0600 | [diff] [blame] | 4682 | cfqd->cfq_slice_idle = cfq_slice_idle; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4683 | cfqd->cfq_group_idle = cfq_group_idle; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4684 | cfqd->cfq_latency = 1; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 4685 | cfqd->hw_tag = -1; |
Corrado Zoccolo | edc7113 | 2009-12-09 20:56:04 +0100 | [diff] [blame] | 4686 | /* |
| 4687 | * we optimistically start assuming sync ops weren't delayed in last |
| 4688 | * second, in order to have larger depth for async operations. |
| 4689 | */ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4690 | cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC; |
Tejun Heo | b2fab5a | 2012-03-05 13:14:57 -0800 | [diff] [blame] | 4691 | return 0; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4692 | |
| 4693 | out_free: |
| 4694 | kfree(cfqd); |
Jianpeng Ma | d50235b | 2013-07-03 13:25:24 +0200 | [diff] [blame] | 4695 | kobject_put(&eq->kobj); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4696 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | } |
| 4698 | |
Jens Axboe | 0bb9794 | 2015-06-10 08:01:20 -0600 | [diff] [blame] | 4699 | static void cfq_registered_queue(struct request_queue *q) |
| 4700 | { |
| 4701 | struct elevator_queue *e = q->elevator; |
| 4702 | struct cfq_data *cfqd = e->elevator_data; |
| 4703 | |
| 4704 | /* |
| 4705 | * Default to IOPS mode with no idling for SSDs |
| 4706 | */ |
| 4707 | if (blk_queue_nonrot(q)) |
| 4708 | cfqd->cfq_slice_idle = 0; |
| 4709 | } |
| 4710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | /* |
| 4712 | * sysfs parts below --> |
| 4713 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | static ssize_t |
| 4715 | cfq_var_show(unsigned int var, char *page) |
| 4716 | { |
Masanari Iida | 176167a | 2014-04-28 12:38:34 +0900 | [diff] [blame] | 4717 | return sprintf(page, "%u\n", var); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | static ssize_t |
| 4721 | cfq_var_store(unsigned int *var, const char *page, size_t count) |
| 4722 | { |
| 4723 | char *p = (char *) page; |
| 4724 | |
| 4725 | *var = simple_strtoul(p, &p, 10); |
| 4726 | return count; |
| 4727 | } |
| 4728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4730 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4732 | struct cfq_data *cfqd = e->elevator_data; \ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4733 | u64 __data = __VAR; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | if (__CONV) \ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4735 | __data = div_u64(__data, NSEC_PER_MSEC); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | return cfq_var_show(__data, (page)); \ |
| 4737 | } |
| 4738 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4739 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
| 4740 | SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4741 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
| 4742 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4743 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4744 | SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4745 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); |
| 4746 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); |
| 4747 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4748 | SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4749 | SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | #undef SHOW_FUNCTION |
| 4751 | |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4752 | #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \ |
| 4753 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
| 4754 | { \ |
| 4755 | struct cfq_data *cfqd = e->elevator_data; \ |
| 4756 | u64 __data = __VAR; \ |
| 4757 | __data = div_u64(__data, NSEC_PER_USEC); \ |
| 4758 | return cfq_var_show(__data, (page)); \ |
| 4759 | } |
| 4760 | USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle); |
| 4761 | USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle); |
| 4762 | USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]); |
| 4763 | USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]); |
| 4764 | USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency); |
| 4765 | #undef USEC_SHOW_FUNCTION |
| 4766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4768 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4770 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | unsigned int __data; \ |
| 4772 | int ret = cfq_var_store(&__data, (page), count); \ |
| 4773 | if (__data < (MIN)) \ |
| 4774 | __data = (MIN); \ |
| 4775 | else if (__data > (MAX)) \ |
| 4776 | __data = (MAX); \ |
| 4777 | if (__CONV) \ |
Jeff Moyer | 9a7f38c | 2016-06-08 08:55:34 -0600 | [diff] [blame] | 4778 | *(__PTR) = (u64)__data * NSEC_PER_MSEC; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | else \ |
| 4780 | *(__PTR) = __data; \ |
| 4781 | return ret; \ |
| 4782 | } |
| 4783 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4784 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, |
| 4785 | UINT_MAX, 1); |
| 4786 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, |
| 4787 | UINT_MAX, 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4788 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4789 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, |
| 4790 | UINT_MAX, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4791 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4792 | STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4793 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); |
| 4794 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4795 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, |
| 4796 | UINT_MAX, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4797 | STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4798 | STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | #undef STORE_FUNCTION |
| 4800 | |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4801 | #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ |
| 4802 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
| 4803 | { \ |
| 4804 | struct cfq_data *cfqd = e->elevator_data; \ |
| 4805 | unsigned int __data; \ |
| 4806 | int ret = cfq_var_store(&__data, (page), count); \ |
| 4807 | if (__data < (MIN)) \ |
| 4808 | __data = (MIN); \ |
| 4809 | else if (__data > (MAX)) \ |
| 4810 | __data = (MAX); \ |
| 4811 | *(__PTR) = (u64)__data * NSEC_PER_USEC; \ |
| 4812 | return ret; \ |
| 4813 | } |
| 4814 | USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX); |
| 4815 | USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX); |
| 4816 | USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX); |
| 4817 | USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX); |
| 4818 | USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX); |
| 4819 | #undef USEC_STORE_FUNCTION |
| 4820 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4821 | #define CFQ_ATTR(name) \ |
| 4822 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4823 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4824 | static struct elv_fs_entry cfq_attrs[] = { |
| 4825 | CFQ_ATTR(quantum), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4826 | CFQ_ATTR(fifo_expire_sync), |
| 4827 | CFQ_ATTR(fifo_expire_async), |
| 4828 | CFQ_ATTR(back_seek_max), |
| 4829 | CFQ_ATTR(back_seek_penalty), |
| 4830 | CFQ_ATTR(slice_sync), |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4831 | CFQ_ATTR(slice_sync_us), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4832 | CFQ_ATTR(slice_async), |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4833 | CFQ_ATTR(slice_async_us), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4834 | CFQ_ATTR(slice_async_rq), |
| 4835 | CFQ_ATTR(slice_idle), |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4836 | CFQ_ATTR(slice_idle_us), |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4837 | CFQ_ATTR(group_idle), |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4838 | CFQ_ATTR(group_idle_us), |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4839 | CFQ_ATTR(low_latency), |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4840 | CFQ_ATTR(target_latency), |
Jeff Moyer | d2d481d | 2016-06-08 15:11:38 +0200 | [diff] [blame] | 4841 | CFQ_ATTR(target_latency_us), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4842 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | }; |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | static struct elevator_type iosched_cfq = { |
Jens Axboe | c51ca6c | 2016-12-10 15:13:59 -0700 | [diff] [blame] | 4846 | .ops.sq = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | .elevator_merge_fn = cfq_merge, |
| 4848 | .elevator_merged_fn = cfq_merged_request, |
| 4849 | .elevator_merge_req_fn = cfq_merged_requests, |
Tahsin Erdogan | 72ef799 | 2016-07-07 11:48:22 -0700 | [diff] [blame] | 4850 | .elevator_allow_bio_merge_fn = cfq_allow_bio_merge, |
| 4851 | .elevator_allow_rq_merge_fn = cfq_allow_rq_merge, |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 4852 | .elevator_bio_merged_fn = cfq_bio_merged, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4853 | .elevator_dispatch_fn = cfq_dispatch_requests, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | .elevator_add_req_fn = cfq_insert_request, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4855 | .elevator_activate_req_fn = cfq_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | .elevator_completed_req_fn = cfq_completed_request, |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 4858 | .elevator_former_req_fn = elv_rb_former_request, |
| 4859 | .elevator_latter_req_fn = elv_rb_latter_request, |
Tejun Heo | 9b84cac | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4860 | .elevator_init_icq_fn = cfq_init_icq, |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4861 | .elevator_exit_icq_fn = cfq_exit_icq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | .elevator_set_req_fn = cfq_set_request, |
| 4863 | .elevator_put_req_fn = cfq_put_request, |
| 4864 | .elevator_may_queue_fn = cfq_may_queue, |
| 4865 | .elevator_init_fn = cfq_init_queue, |
| 4866 | .elevator_exit_fn = cfq_exit_queue, |
Jens Axboe | 0bb9794 | 2015-06-10 08:01:20 -0600 | [diff] [blame] | 4867 | .elevator_registered_fn = cfq_registered_queue, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | }, |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4869 | .icq_size = sizeof(struct cfq_io_cq), |
| 4870 | .icq_align = __alignof__(struct cfq_io_cq), |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4871 | .elevator_attrs = cfq_attrs, |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4872 | .elevator_name = "cfq", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | .elevator_owner = THIS_MODULE, |
| 4874 | }; |
| 4875 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4876 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4877 | static struct blkcg_policy blkcg_policy_cfq = { |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 4878 | .dfl_cftypes = cfq_blkcg_files, |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 4879 | .legacy_cftypes = cfq_blkcg_legacy_files, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 4880 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 4881 | .cpd_alloc_fn = cfq_cpd_alloc, |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 4882 | .cpd_init_fn = cfq_cpd_init, |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 4883 | .cpd_free_fn = cfq_cpd_free, |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 4884 | .cpd_bind_fn = cfq_cpd_bind, |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 4885 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 4886 | .pd_alloc_fn = cfq_pd_alloc, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 4887 | .pd_init_fn = cfq_pd_init, |
Tejun Heo | 0b39920 | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 4888 | .pd_offline_fn = cfq_pd_offline, |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 4889 | .pd_free_fn = cfq_pd_free, |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 4890 | .pd_reset_stats_fn = cfq_pd_reset_stats, |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4891 | }; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4892 | #endif |
| 4893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | static int __init cfq_init(void) |
| 4895 | { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4896 | int ret; |
| 4897 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4898 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4899 | ret = blkcg_policy_register(&blkcg_policy_cfq); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4900 | if (ret) |
| 4901 | return ret; |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4902 | #else |
| 4903 | cfq_group_idle = 0; |
| 4904 | #endif |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4905 | |
Tejun Heo | fd79495 | 2012-06-04 10:01:38 +0200 | [diff] [blame] | 4906 | ret = -ENOMEM; |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4907 | cfq_pool = KMEM_CACHE(cfq_queue, 0); |
| 4908 | if (!cfq_pool) |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4909 | goto err_pol_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4911 | ret = elv_register(&iosched_cfq); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4912 | if (ret) |
| 4913 | goto err_free_pool; |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4914 | |
Adrian Bunk | 2fdd82b | 2007-12-12 18:51:56 +0100 | [diff] [blame] | 4915 | return 0; |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4916 | |
| 4917 | err_free_pool: |
| 4918 | kmem_cache_destroy(cfq_pool); |
| 4919 | err_pol_unreg: |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4920 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4921 | blkcg_policy_unregister(&blkcg_policy_cfq); |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4922 | #endif |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4923 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | static void __exit cfq_exit(void) |
| 4927 | { |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4928 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4929 | blkcg_policy_unregister(&blkcg_policy_cfq); |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4930 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | elv_unregister(&iosched_cfq); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4932 | kmem_cache_destroy(cfq_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | module_init(cfq_init); |
| 4936 | module_exit(cfq_exit); |
| 4937 | |
| 4938 | MODULE_AUTHOR("Jens Axboe"); |
| 4939 | MODULE_LICENSE("GPL"); |
| 4940 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |