blob: 8fc71eeafa86df2a211c3740e5a172661f1a0ecd [file] [log] [blame]
Tatyana Brokhman16349062012-09-20 10:46:10 +03001/*
2 * ROW (Read Over Write) I/O scheduler.
3 *
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +02004 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Tatyana Brokhman16349062012-09-20 10:46:10 +03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16/* See Documentation/block/row-iosched.txt */
17
18#include <linux/kernel.h>
19#include <linux/fs.h>
20#include <linux/blkdev.h>
21#include <linux/elevator.h>
22#include <linux/bio.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/compiler.h>
27#include <linux/blktrace_api.h>
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +020028#include <linux/hrtimer.h>
Tatyana Brokhman16349062012-09-20 10:46:10 +030029
30/*
31 * enum row_queue_prio - Priorities of the ROW queues
32 *
33 * This enum defines the priorities (and the number of queues)
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +020034 * the requests will be distributed to. The higher priority -
35 * the bigger is the "bus time" (or the dispatch quantum) given
36 * to that queue.
Tatyana Brokhman16349062012-09-20 10:46:10 +030037 * ROWQ_PRIO_HIGH_READ - is the higher priority queue.
38 *
39 */
40enum row_queue_prio {
41 ROWQ_PRIO_HIGH_READ = 0,
Tatyana Brokhman16349062012-09-20 10:46:10 +030042 ROWQ_PRIO_HIGH_SWRITE,
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +020043 ROWQ_PRIO_REG_READ,
Tatyana Brokhman16349062012-09-20 10:46:10 +030044 ROWQ_PRIO_REG_SWRITE,
45 ROWQ_PRIO_REG_WRITE,
46 ROWQ_PRIO_LOW_READ,
47 ROWQ_PRIO_LOW_SWRITE,
48 ROWQ_MAX_PRIO,
49};
50
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +020051/*
52 * The following indexes define the distribution of ROW queues according to
53 * priorities. Each index defines the first queue in that priority group.
54 */
55#define ROWQ_HIGH_PRIO_IDX ROWQ_PRIO_HIGH_READ
56#define ROWQ_REG_PRIO_IDX ROWQ_PRIO_REG_READ
57#define ROWQ_LOW_PRIO_IDX ROWQ_PRIO_LOW_READ
58
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +020059/**
60 * struct row_queue_params - ROW queue parameters
61 * @idling_enabled: Flag indicating whether idling is enable on
62 * the queue
63 * @quantum: Number of requests to be dispatched from this queue
64 * in a dispatch cycle
65 * @is_urgent: Flags indicating whether the queue can notify on
66 * urgent requests
67 *
68 */
69struct row_queue_params {
70 bool idling_enabled;
71 int quantum;
72 bool is_urgent;
Tatyana Brokhman16349062012-09-20 10:46:10 +030073};
74
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +020075/*
76 * This array holds the default values of the different configurables
77 * for each ROW queue. Each row of the array holds the following values:
78 * {idling_enabled, quantum, is_urgent}
79 * Each row corresponds to a queue with the same index (according to
80 * enum row_queue_prio)
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +020081 * Note: The quantums are valid inside their priority type. For example:
82 * For every 10 high priority read requests, 1 high priority sync
83 * write will be dispatched.
84 * For every 100 regular read requests 1 regular write request will
85 * be dispatched.
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +020086 */
87static const struct row_queue_params row_queues_def[] = {
88/* idling_enabled, quantum, is_urgent */
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +020089 {true, 10, true}, /* ROWQ_PRIO_HIGH_READ */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +020090 {false, 1, false}, /* ROWQ_PRIO_HIGH_SWRITE */
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +020091 {true, 100, true}, /* ROWQ_PRIO_REG_READ */
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +020092 {false, 1, false}, /* ROWQ_PRIO_REG_SWRITE */
93 {false, 1, false}, /* ROWQ_PRIO_REG_WRITE */
94 {false, 1, false}, /* ROWQ_PRIO_LOW_READ */
95 {false, 1, false} /* ROWQ_PRIO_LOW_SWRITE */
Tatyana Brokhman16349062012-09-20 10:46:10 +030096};
97
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +020098/* Default values for idling on read queues (in msec) */
99#define ROW_IDLE_TIME_MSEC 5
100#define ROW_READ_FREQ_MSEC 20
Tatyana Brokhman16349062012-09-20 10:46:10 +0300101
102/**
103 * struct rowq_idling_data - parameters for idling on the queue
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200104 * @last_insert_time: time the last request was inserted
105 * to the queue
Tatyana Brokhman16349062012-09-20 10:46:10 +0300106 * @begin_idling: flag indicating wether we should idle
107 *
108 */
109struct rowq_idling_data {
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200110 ktime_t last_insert_time;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300111 bool begin_idling;
112};
113
114/**
115 * struct row_queue - requests grouping structure
116 * @rdata: parent row_data structure
117 * @fifo: fifo of requests
118 * @prio: queue priority (enum row_queue_prio)
119 * @nr_dispatched: number of requests already dispatched in
120 * the current dispatch cycle
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200121 * @nr_req: number of requests in queue
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200122 * @dispatch quantum: number of requests this queue may
123 * dispatch in a dispatch cycle
Tatyana Brokhman16349062012-09-20 10:46:10 +0300124 * @idle_data: data for idling on queues
125 *
126 */
127struct row_queue {
128 struct row_data *rdata;
129 struct list_head fifo;
130 enum row_queue_prio prio;
131
132 unsigned int nr_dispatched;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300133
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200134 unsigned int nr_req;
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200135 int disp_quantum;
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200136
Tatyana Brokhman16349062012-09-20 10:46:10 +0300137 /* used only for READ queues */
138 struct rowq_idling_data idle_data;
139};
140
141/**
142 * struct idling_data - data for idling on empty rqueue
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200143 * @idle_time_ms: idling duration (msec)
144 * @freq_ms: min time between two requests that
Tatyana Brokhman16349062012-09-20 10:46:10 +0300145 * triger idling (msec)
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200146 * @hr_timer: idling timer
147 * @idle_work: the work to be scheduled when idling timer expires
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200148 * @idling_queue_idx: index of the queues we're idling on
Tatyana Brokhman16349062012-09-20 10:46:10 +0300149 *
150 */
151struct idling_data {
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200152 s64 idle_time_ms;
153 s64 freq_ms;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300154
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200155 struct hrtimer hr_timer;
156 struct work_struct idle_work;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200157 enum row_queue_prio idling_queue_idx;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300158};
159
160/**
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200161 * struct starvation_data - data for starvation management
162 * @starvation_limit: number of times this priority class
163 * can tolerate being starved
164 * @starvation_counter: number of requests from higher
165 * priority classes that were dispatched while this
166 * priority request were pending
167 *
168 */
169struct starvation_data {
170 int starvation_limit;
171 int starvation_counter;
172};
173
174/**
Tatyana Brokhman16349062012-09-20 10:46:10 +0300175 * struct row_queue - Per block device rqueue structure
176 * @dispatch_queue: dispatch rqueue
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200177 * @row_queues: array of priority request queues
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200178 * @rd_idle_data: data for idling after READ request
Tatyana Brokhman16349062012-09-20 10:46:10 +0300179 * @nr_reqs: nr_reqs[0] holds the number of all READ requests in
180 * scheduler, nr_reqs[1] holds the number of all WRITE
181 * requests in scheduler
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200182 * @urgent_in_flight: flag indicating that there is an urgent
183 * request that was dispatched to driver and is yet to
184 * complete.
185 * @pending_urgent_rq: pointer to the pending urgent request
186 * @last_served_ioprio_class: I/O priority class that was last dispatched from
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200187 * @reg_prio_starvation: starvation data for REGULAR priority queues
188 * @low_prio_starvation: starvation data for LOW priority queues
Tatyana Brokhman16349062012-09-20 10:46:10 +0300189 * @cycle_flags: used for marking unserved queueus
190 *
191 */
192struct row_data {
193 struct request_queue *dispatch_queue;
194
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200195 struct row_queue row_queues[ROWQ_MAX_PRIO];
Tatyana Brokhman16349062012-09-20 10:46:10 +0300196
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200197 struct idling_data rd_idle_data;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300198 unsigned int nr_reqs[2];
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200199 bool urgent_in_flight;
200 struct request *pending_urgent_rq;
201 int last_served_ioprio_class;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200202
203#define ROW_REG_STARVATION_TOLLERANCE 50
204 struct starvation_data reg_prio_starvation;
205#define ROW_LOW_STARVATION_TOLLERANCE 1000
206 struct starvation_data low_prio_starvation;
207
Tatyana Brokhman16349062012-09-20 10:46:10 +0300208 unsigned int cycle_flags;
209};
210
211#define RQ_ROWQ(rq) ((struct row_queue *) ((rq)->elv.priv[0]))
212
213#define row_log(q, fmt, args...) \
214 blk_add_trace_msg(q, "%s():" fmt , __func__, ##args)
215#define row_log_rowq(rdata, rowq_id, fmt, args...) \
216 blk_add_trace_msg(rdata->dispatch_queue, "rowq%d " fmt, \
217 rowq_id, ##args)
218
219static inline void row_mark_rowq_unserved(struct row_data *rd,
220 enum row_queue_prio qnum)
221{
222 rd->cycle_flags |= (1 << qnum);
223}
224
225static inline void row_clear_rowq_unserved(struct row_data *rd,
226 enum row_queue_prio qnum)
227{
228 rd->cycle_flags &= ~(1 << qnum);
229}
230
231static inline int row_rowq_unserved(struct row_data *rd,
232 enum row_queue_prio qnum)
233{
234 return rd->cycle_flags & (1 << qnum);
235}
236
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200237static inline void __maybe_unused row_dump_queues_stat(struct row_data *rd)
238{
239 int i;
240
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200241 row_log(rd->dispatch_queue, " Queues status:");
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200242 for (i = 0; i < ROWQ_MAX_PRIO; i++)
243 row_log(rd->dispatch_queue,
244 "queue%d: dispatched= %d, nr_req=%d", i,
245 rd->row_queues[i].nr_dispatched,
246 rd->row_queues[i].nr_req);
247}
248
Tatyana Brokhman16349062012-09-20 10:46:10 +0300249/******************** Static helper functions ***********************/
Tatyana Brokhman16349062012-09-20 10:46:10 +0300250static void kick_queue(struct work_struct *work)
251{
Tatyana Brokhman16349062012-09-20 10:46:10 +0300252 struct idling_data *read_data =
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200253 container_of(work, struct idling_data, idle_work);
254 struct row_data *rd =
255 container_of(read_data, struct row_data, rd_idle_data);
256
257 blk_run_queue(rd->dispatch_queue);
258}
259
260
261static enum hrtimer_restart row_idle_hrtimer_fn(struct hrtimer *hr_timer)
262{
263 struct idling_data *read_data =
264 container_of(hr_timer, struct idling_data, hr_timer);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300265 struct row_data *rd =
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200266 container_of(read_data, struct row_data, rd_idle_data);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300267
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200268 row_log_rowq(rd, rd->rd_idle_data.idling_queue_idx,
269 "Performing delayed work");
Tatyana Brokhman16349062012-09-20 10:46:10 +0300270 /* Mark idling process as done */
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200271 rd->row_queues[rd->rd_idle_data.idling_queue_idx].
272 idle_data.begin_idling = false;
273 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300274
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200275 if (!rd->nr_reqs[READ] && !rd->nr_reqs[WRITE])
Tatyana Brokhman16349062012-09-20 10:46:10 +0300276 row_log(rd->dispatch_queue, "No requests in scheduler");
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200277 else
278 kblockd_schedule_work(rd->dispatch_queue,
279 &read_data->idle_work);
280 return HRTIMER_NORESTART;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300281}
282
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200283/*
284 * row_regular_req_pending() - Check if there are REGULAR priority requests
285 * Pending in scheduler
286 * @rd: pointer to struct row_data
287 *
288 * Returns True if there are REGULAR priority requests in scheduler queues.
289 * False, otherwise.
290 */
291static inline bool row_regular_req_pending(struct row_data *rd)
292{
293 int i;
294
295 for (i = ROWQ_REG_PRIO_IDX; i < ROWQ_LOW_PRIO_IDX; i++)
296 if (!list_empty(&rd->row_queues[i].fifo))
297 return true;
298 return false;
299}
300
301/*
302 * row_low_req_pending() - Check if there are LOW priority requests
303 * Pending in scheduler
304 * @rd: pointer to struct row_data
305 *
306 * Returns True if there are LOW priority requests in scheduler queues.
307 * False, otherwise.
308 */
309static inline bool row_low_req_pending(struct row_data *rd)
310{
311 int i;
312
313 for (i = ROWQ_LOW_PRIO_IDX; i < ROWQ_MAX_PRIO; i++)
314 if (!list_empty(&rd->row_queues[i].fifo))
315 return true;
316 return false;
317}
318
Tatyana Brokhman16349062012-09-20 10:46:10 +0300319/******************* Elevator callback functions *********************/
320
321/*
322 * row_add_request() - Add request to the scheduler
323 * @q: requests queue
324 * @rq: request to add
325 *
326 */
327static void row_add_request(struct request_queue *q,
328 struct request *rq)
329{
330 struct row_data *rd = (struct row_data *)q->elevator->elevator_data;
331 struct row_queue *rqueue = RQ_ROWQ(rq);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200332 s64 diff_ms;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200333 bool queue_was_empty = list_empty(&rqueue->fifo);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300334
335 list_add_tail(&rq->queuelist, &rqueue->fifo);
336 rd->nr_reqs[rq_data_dir(rq)]++;
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200337 rqueue->nr_req++;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300338 rq_set_fifo_time(rq, jiffies); /* for statistics*/
339
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200340 if (rq->cmd_flags & REQ_URGENT) {
341 WARN_ON(1);
342 blk_dump_rq_flags(rq, "");
343 rq->cmd_flags &= ~REQ_URGENT;
344 }
345
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +0200346 if (row_queues_def[rqueue->prio].idling_enabled) {
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200347 if (rd->rd_idle_data.idling_queue_idx == rqueue->prio &&
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200348 hrtimer_active(&rd->rd_idle_data.hr_timer)) {
349 (void)hrtimer_cancel(&rd->rd_idle_data.hr_timer);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200350 row_log_rowq(rd, rqueue->prio,
351 "Canceled delayed work on %d",
352 rd->rd_idle_data.idling_queue_idx);
353 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
354 }
355 diff_ms = ktime_to_ms(ktime_sub(ktime_get(),
356 rqueue->idle_data.last_insert_time));
357 if (unlikely(diff_ms < 0)) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200358 pr_err("%s(): time delta error: diff_ms < 0",
359 __func__);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200360 rqueue->idle_data.begin_idling = false;
361 return;
362 }
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200363 if (diff_ms < rd->rd_idle_data.freq_ms) {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300364 rqueue->idle_data.begin_idling = true;
365 row_log_rowq(rd, rqueue->prio, "Enable idling");
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200366 } else {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300367 rqueue->idle_data.begin_idling = false;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200368 row_log_rowq(rd, rqueue->prio, "Disable idling (%ldms)",
369 (long)diff_ms);
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200370 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300371
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200372 rqueue->idle_data.last_insert_time = ktime_get();
Tatyana Brokhman16349062012-09-20 10:46:10 +0300373 }
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +0200374 if (row_queues_def[rqueue->prio].is_urgent &&
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200375 !rd->pending_urgent_rq && !rd->urgent_in_flight) {
376 /* Handle High Priority queues */
377 if (rqueue->prio < ROWQ_REG_PRIO_IDX &&
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200378 rd->last_served_ioprio_class != IOPRIO_CLASS_RT &&
379 queue_was_empty) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200380 row_log_rowq(rd, rqueue->prio,
381 "added (high prio) urgent request");
382 rq->cmd_flags |= REQ_URGENT;
383 rd->pending_urgent_rq = rq;
384 } else if (row_rowq_unserved(rd, rqueue->prio)) {
385 /* Handle Regular priotity queues */
386 row_log_rowq(rd, rqueue->prio,
387 "added urgent request (total on queue=%d)",
388 rqueue->nr_req);
389 rq->cmd_flags |= REQ_URGENT;
390 WARN_ON(rqueue->nr_req > 1);
391 rd->pending_urgent_rq = rq;
392 }
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200393 } else
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200394 row_log_rowq(rd, rqueue->prio,
395 "added request (total on queue=%d)", rqueue->nr_req);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300396}
397
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200398/**
399 * row_reinsert_req() - Reinsert request back to the scheduler
400 * @q: requests queue
401 * @rq: request to add
402 *
403 * Reinsert the given request back to the queue it was
404 * dispatched from as if it was never dispatched.
405 *
406 * Returns 0 on success, error code otherwise
407 */
408static int row_reinsert_req(struct request_queue *q,
409 struct request *rq)
410{
411 struct row_data *rd = q->elevator->elevator_data;
412 struct row_queue *rqueue = RQ_ROWQ(rq);
413
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200414 if (!rqueue || rqueue->prio >= ROWQ_MAX_PRIO)
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200415 return -EIO;
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200416
417 list_add(&rq->queuelist, &rqueue->fifo);
418 rd->nr_reqs[rq_data_dir(rq)]++;
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200419 rqueue->nr_req++;
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200420
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200421 row_log_rowq(rd, rqueue->prio,
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200422 "%s request reinserted (total on queue=%d)",
423 (rq_data_dir(rq) == READ ? "READ" : "write"), rqueue->nr_req);
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200424
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200425 if (rq->cmd_flags & REQ_URGENT) {
426 /*
427 * It's not compliant with the design to re-insert
428 * urgent requests. We want to be able to track this
429 * down.
430 */
431 WARN_ON(1);
432 if (!rd->urgent_in_flight) {
433 pr_err("%s(): no urgent in flight", __func__);
434 } else {
435 rd->urgent_in_flight = false;
436 pr_err("%s(): reinserting URGENT %s req",
437 __func__,
438 (rq_data_dir(rq) == READ ? "READ" : "WRITE"));
439 if (rd->pending_urgent_rq) {
440 pr_err("%s(): urgent rq is pending",
441 __func__);
442 rd->pending_urgent_rq->cmd_flags &= ~REQ_URGENT;
443 }
444 rd->pending_urgent_rq = rq;
445 }
446 }
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200447 return 0;
448}
449
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200450static void row_completed_req(struct request_queue *q, struct request *rq)
451{
452 struct row_data *rd = q->elevator->elevator_data;
453
454 if (rq->cmd_flags & REQ_URGENT) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200455 if (!rd->urgent_in_flight) {
456 WARN_ON(1);
457 pr_err("%s(): URGENT req but urgent_in_flight = F",
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200458 __func__);
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200459 }
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200460 rd->urgent_in_flight = false;
461 rq->cmd_flags &= ~REQ_URGENT;
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200462 }
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200463 row_log(q, "completed %s %s req.",
464 (rq->cmd_flags & REQ_URGENT ? "URGENT" : "regular"),
465 (rq_data_dir(rq) == READ ? "READ" : "WRITE"));
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200466}
467
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200468/**
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200469 * row_urgent_pending() - Return TRUE if there is an urgent
470 * request on scheduler
471 * @q: requests queue
472 */
473static bool row_urgent_pending(struct request_queue *q)
474{
475 struct row_data *rd = q->elevator->elevator_data;
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200476
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200477 if (rd->urgent_in_flight) {
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200478 row_log(rd->dispatch_queue, "%d urgent requests in flight",
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200479 rd->urgent_in_flight);
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200480 return false;
481 }
482
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200483 if (rd->pending_urgent_rq) {
484 row_log(rd->dispatch_queue, "Urgent request pending");
485 return true;
486 }
Shashank Babu Chinta Venkata3df69bf2013-02-26 17:33:55 -0800487
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200488 row_log(rd->dispatch_queue, "no urgent request pending/in flight");
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200489 return false;
490}
491
492/**
Tatyana Brokhman16349062012-09-20 10:46:10 +0300493 * row_remove_request() - Remove given request from scheduler
494 * @q: requests queue
495 * @rq: request to remove
496 *
497 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200498static void row_remove_request(struct row_data *rd,
Tatyana Brokhman16349062012-09-20 10:46:10 +0300499 struct request *rq)
500{
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200501 struct row_queue *rqueue = RQ_ROWQ(rq);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300502
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200503 list_del_init(&(rq)->queuelist);
504 if (rd->pending_urgent_rq == rq)
505 rd->pending_urgent_rq = NULL;
506 else
507 BUG_ON(rq->cmd_flags & REQ_URGENT);
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200508 rqueue->nr_req--;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300509 rd->nr_reqs[rq_data_dir(rq)]--;
510}
511
512/*
513 * row_dispatch_insert() - move request to dispatch queue
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200514 * @rd: pointer to struct row_data
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200515 * @rq: the request to dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300516 *
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200517 * This function moves the given request to the dispatch queue
Tatyana Brokhman16349062012-09-20 10:46:10 +0300518 *
519 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200520static void row_dispatch_insert(struct row_data *rd, struct request *rq)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300521{
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200522 struct row_queue *rqueue = RQ_ROWQ(rq);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300523
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200524 row_remove_request(rd, rq);
525 elv_dispatch_sort(rd->dispatch_queue, rq);
526 if (rq->cmd_flags & REQ_URGENT) {
527 WARN_ON(rd->urgent_in_flight);
528 rd->urgent_in_flight = true;
529 }
530 rqueue->nr_dispatched++;
531 row_clear_rowq_unserved(rd, rqueue->prio);
532 row_log_rowq(rd, rqueue->prio,
533 " Dispatched request %p nr_disp = %d", rq,
534 rqueue->nr_dispatched);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200535 if (rqueue->prio < ROWQ_REG_PRIO_IDX) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200536 rd->last_served_ioprio_class = IOPRIO_CLASS_RT;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200537 if (row_regular_req_pending(rd))
538 rd->reg_prio_starvation.starvation_counter++;
539 if (row_low_req_pending(rd))
540 rd->low_prio_starvation.starvation_counter++;
541 } else if (rqueue->prio < ROWQ_LOW_PRIO_IDX) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200542 rd->last_served_ioprio_class = IOPRIO_CLASS_BE;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200543 rd->reg_prio_starvation.starvation_counter = 0;
544 if (row_low_req_pending(rd))
545 rd->low_prio_starvation.starvation_counter++;
546 } else {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200547 rd->last_served_ioprio_class = IOPRIO_CLASS_IDLE;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200548 rd->low_prio_starvation.starvation_counter = 0;
549 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300550}
551
552/*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200553 * row_get_ioprio_class_to_serve() - Return the next I/O priority
554 * class to dispatch requests from
Tatyana Brokhman16349062012-09-20 10:46:10 +0300555 * @rd: pointer to struct row_data
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200556 * @force: flag indicating if forced dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300557 *
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200558 * This function returns the next I/O priority class to serve
559 * {IOPRIO_CLASS_NONE, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE}.
560 * If there are no more requests in scheduler or if we're idling on some queue
561 * IOPRIO_CLASS_NONE will be returned.
562 * If idling is scheduled on a lower priority queue than the one that needs
563 * to be served, it will be canceled.
Tatyana Brokhman16349062012-09-20 10:46:10 +0300564 *
565 */
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200566static int row_get_ioprio_class_to_serve(struct row_data *rd, int force)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300567{
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200568 int i;
569 int ret = IOPRIO_CLASS_NONE;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300570
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200571 if (!rd->nr_reqs[READ] && !rd->nr_reqs[WRITE]) {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300572 row_log(rd->dispatch_queue, "No more requests in scheduler");
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200573 goto check_idling;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300574 }
575
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200576 /* First, go over the high priority queues */
577 for (i = 0; i < ROWQ_REG_PRIO_IDX; i++) {
578 if (!list_empty(&rd->row_queues[i].fifo)) {
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200579 if (hrtimer_active(&rd->rd_idle_data.hr_timer)) {
580 (void)hrtimer_cancel(
581 &rd->rd_idle_data.hr_timer);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200582 row_log_rowq(rd,
583 rd->rd_idle_data.idling_queue_idx,
584 "Canceling delayed work on %d. RT pending",
585 rd->rd_idle_data.idling_queue_idx);
586 rd->rd_idle_data.idling_queue_idx =
587 ROWQ_MAX_PRIO;
588 }
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200589
590 if (row_regular_req_pending(rd) &&
591 (rd->reg_prio_starvation.starvation_counter >=
592 rd->reg_prio_starvation.starvation_limit))
593 ret = IOPRIO_CLASS_BE;
594 else if (row_low_req_pending(rd) &&
595 (rd->low_prio_starvation.starvation_counter >=
596 rd->low_prio_starvation.starvation_limit))
597 ret = IOPRIO_CLASS_IDLE;
598 else
599 ret = IOPRIO_CLASS_RT;
600
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200601 goto done;
602 }
603 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300604
605 /*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200606 * At the moment idling is implemented only for READ queues.
607 * If enabled on WRITE, this needs updating
Tatyana Brokhman16349062012-09-20 10:46:10 +0300608 */
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200609 if (hrtimer_active(&rd->rd_idle_data.hr_timer)) {
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200610 row_log(rd->dispatch_queue, "Delayed work pending. Exiting");
611 goto done;
612 }
613check_idling:
614 /* Check for (high priority) idling and enable if needed */
615 for (i = 0; i < ROWQ_REG_PRIO_IDX && !force; i++) {
616 if (rd->row_queues[i].idle_data.begin_idling &&
617 row_queues_def[i].idling_enabled)
618 goto initiate_idling;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300619 }
620
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200621 /* Regular priority queues */
622 for (i = ROWQ_REG_PRIO_IDX; i < ROWQ_LOW_PRIO_IDX; i++) {
623 if (list_empty(&rd->row_queues[i].fifo)) {
624 /* We can idle only if this is not a forced dispatch */
625 if (rd->row_queues[i].idle_data.begin_idling &&
626 !force && row_queues_def[i].idling_enabled)
627 goto initiate_idling;
628 } else {
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200629 if (row_low_req_pending(rd) &&
630 (rd->low_prio_starvation.starvation_counter >=
631 rd->low_prio_starvation.starvation_limit))
632 ret = IOPRIO_CLASS_IDLE;
633 else
634 ret = IOPRIO_CLASS_BE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200635 goto done;
636 }
637 }
638
639 if (rd->nr_reqs[READ] || rd->nr_reqs[WRITE])
640 ret = IOPRIO_CLASS_IDLE;
641 goto done;
642
643initiate_idling:
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200644 hrtimer_start(&rd->rd_idle_data.hr_timer,
645 ktime_set(0, rd->rd_idle_data.idle_time_ms * NSEC_PER_MSEC),
646 HRTIMER_MODE_REL);
647
648 rd->rd_idle_data.idling_queue_idx = i;
649 row_log_rowq(rd, i, "Scheduled delayed work on %d. exiting", i);
650
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200651done:
652 return ret;
653}
654
655static void row_restart_cycle(struct row_data *rd,
656 int start_idx, int end_idx)
657{
658 int i;
659
660 row_dump_queues_stat(rd);
661 for (i = start_idx; i < end_idx; i++) {
662 if (rd->row_queues[i].nr_dispatched <
663 rd->row_queues[i].disp_quantum)
664 row_mark_rowq_unserved(rd, i);
665 rd->row_queues[i].nr_dispatched = 0;
666 }
667 row_log(rd->dispatch_queue, "Restarting cycle for class @ %d-%d",
668 start_idx, end_idx);
669}
670
671/*
672 * row_get_next_queue() - selects the next queue to dispatch from
673 * @q: requests queue
674 * @rd: pointer to struct row_data
675 * @start_idx/end_idx: indexes in the row_queues array to select a queue
676 * from.
677 *
678 * Return index of the queues to dispatch from. Error code if fails.
679 *
680 */
681static int row_get_next_queue(struct request_queue *q, struct row_data *rd,
682 int start_idx, int end_idx)
683{
684 int i = start_idx;
685 bool restart = true;
686 int ret = -EIO;
687
688 do {
689 if (list_empty(&rd->row_queues[i].fifo) ||
690 rd->row_queues[i].nr_dispatched >=
691 rd->row_queues[i].disp_quantum) {
692 i++;
693 if (i == end_idx && restart) {
694 /* Restart cycle for this priority class */
695 row_restart_cycle(rd, start_idx, end_idx);
696 i = start_idx;
697 restart = false;
698 }
699 } else {
700 ret = i;
701 break;
702 }
703 } while (i < end_idx);
704
705 return ret;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300706}
707
708/*
709 * row_dispatch_requests() - selects the next request to dispatch
710 * @q: requests queue
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200711 * @force: flag indicating if forced dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300712 *
713 * Return 0 if no requests were moved to the dispatch queue.
714 * 1 otherwise
715 *
716 */
717static int row_dispatch_requests(struct request_queue *q, int force)
718{
719 struct row_data *rd = (struct row_data *)q->elevator->elevator_data;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200720 int ret = 0, currq, ioprio_class_to_serve, start_idx, end_idx;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300721
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200722 if (force && hrtimer_active(&rd->rd_idle_data.hr_timer)) {
723 (void)hrtimer_cancel(&rd->rd_idle_data.hr_timer);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200724 row_log_rowq(rd, rd->rd_idle_data.idling_queue_idx,
725 "Canceled delayed work on %d - forced dispatch",
726 rd->rd_idle_data.idling_queue_idx);
727 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300728 }
729
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200730 if (rd->pending_urgent_rq) {
731 row_log(rd->dispatch_queue, "dispatching urgent request");
732 row_dispatch_insert(rd, rd->pending_urgent_rq);
733 ret = 1;
734 goto done;
735 }
736
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200737 ioprio_class_to_serve = row_get_ioprio_class_to_serve(rd, force);
738 row_log(rd->dispatch_queue, "Dispatching from %d priority class",
739 ioprio_class_to_serve);
740
741 switch (ioprio_class_to_serve) {
742 case IOPRIO_CLASS_NONE:
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200743 rd->last_served_ioprio_class = IOPRIO_CLASS_NONE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200744 goto done;
745 case IOPRIO_CLASS_RT:
746 start_idx = ROWQ_HIGH_PRIO_IDX;
747 end_idx = ROWQ_REG_PRIO_IDX;
748 break;
749 case IOPRIO_CLASS_BE:
750 start_idx = ROWQ_REG_PRIO_IDX;
751 end_idx = ROWQ_LOW_PRIO_IDX;
752 break;
753 case IOPRIO_CLASS_IDLE:
754 start_idx = ROWQ_LOW_PRIO_IDX;
755 end_idx = ROWQ_MAX_PRIO;
756 break;
757 default:
758 pr_err("%s(): Invalid I/O priority class", __func__);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300759 goto done;
760 }
761
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200762 currq = row_get_next_queue(q, rd, start_idx, end_idx);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300763
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200764 /* Dispatch */
765 if (currq >= 0) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200766 row_dispatch_insert(rd,
767 rq_entry_fifo(rd->row_queues[currq].fifo.next));
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200768 ret = 1;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300769 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300770done:
771 return ret;
772}
773
774/*
775 * row_init_queue() - Init scheduler data structures
776 * @q: requests queue
777 *
778 * Return pointer to struct row_data to be saved in elevator for
779 * this dispatch queue
780 *
781 */
782static void *row_init_queue(struct request_queue *q)
783{
784
785 struct row_data *rdata;
786 int i;
787
788 rdata = kmalloc_node(sizeof(*rdata),
789 GFP_KERNEL | __GFP_ZERO, q->node);
790 if (!rdata)
791 return NULL;
792
Tatyana Brokhman522778f2013-01-24 16:17:27 +0200793 memset(rdata, 0, sizeof(*rdata));
Tatyana Brokhman16349062012-09-20 10:46:10 +0300794 for (i = 0; i < ROWQ_MAX_PRIO; i++) {
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200795 INIT_LIST_HEAD(&rdata->row_queues[i].fifo);
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +0200796 rdata->row_queues[i].disp_quantum = row_queues_def[i].quantum;
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200797 rdata->row_queues[i].rdata = rdata;
798 rdata->row_queues[i].prio = i;
799 rdata->row_queues[i].idle_data.begin_idling = false;
800 rdata->row_queues[i].idle_data.last_insert_time =
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200801 ktime_set(0, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300802 }
803
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200804 rdata->reg_prio_starvation.starvation_limit =
805 ROW_REG_STARVATION_TOLLERANCE;
806 rdata->low_prio_starvation.starvation_limit =
807 ROW_LOW_STARVATION_TOLLERANCE;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300808 /*
809 * Currently idling is enabled only for READ queues. If we want to
810 * enable it for write queues also, note that idling frequency will
811 * be the same in both cases
812 */
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200813 rdata->rd_idle_data.idle_time_ms = ROW_IDLE_TIME_MSEC;
814 rdata->rd_idle_data.freq_ms = ROW_READ_FREQ_MSEC;
815 hrtimer_init(&rdata->rd_idle_data.hr_timer,
816 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
817 rdata->rd_idle_data.hr_timer.function = &row_idle_hrtimer_fn;
818
819 INIT_WORK(&rdata->rd_idle_data.idle_work, kick_queue);
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200820 rdata->last_served_ioprio_class = IOPRIO_CLASS_NONE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200821 rdata->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300822 rdata->dispatch_queue = q;
823
Tatyana Brokhman16349062012-09-20 10:46:10 +0300824 return rdata;
825}
826
827/*
828 * row_exit_queue() - called on unloading the RAW scheduler
829 * @e: poiner to struct elevator_queue
830 *
831 */
832static void row_exit_queue(struct elevator_queue *e)
833{
834 struct row_data *rd = (struct row_data *)e->elevator_data;
835 int i;
836
837 for (i = 0; i < ROWQ_MAX_PRIO; i++)
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200838 BUG_ON(!list_empty(&rd->row_queues[i].fifo));
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200839 if (hrtimer_cancel(&rd->rd_idle_data.hr_timer))
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200840 pr_err("%s(): idle timer was active!", __func__);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200841 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300842 kfree(rd);
843}
844
845/*
846 * row_merged_requests() - Called when 2 requests are merged
847 * @q: requests queue
848 * @rq: request the two requests were merged into
849 * @next: request that was merged
850 */
851static void row_merged_requests(struct request_queue *q, struct request *rq,
852 struct request *next)
853{
854 struct row_queue *rqueue = RQ_ROWQ(next);
855
856 list_del_init(&next->queuelist);
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200857 rqueue->nr_req--;
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200858 if (rqueue->rdata->pending_urgent_rq == next) {
859 pr_err("\n\nROW_WARNING: merging pending urgent!");
860 rqueue->rdata->pending_urgent_rq = rq;
861 rq->cmd_flags |= REQ_URGENT;
862 WARN_ON(!(next->cmd_flags & REQ_URGENT));
863 next->cmd_flags &= ~REQ_URGENT;
864 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300865 rqueue->rdata->nr_reqs[rq_data_dir(rq)]--;
866}
867
868/*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200869 * row_get_queue_prio() - Get queue priority for a given request
Tatyana Brokhman16349062012-09-20 10:46:10 +0300870 *
871 * This is a helping function which purpose is to determine what
872 * ROW queue the given request should be added to (and
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200873 * dispatched from later on)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300874 *
Tatyana Brokhman16349062012-09-20 10:46:10 +0300875 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200876static enum row_queue_prio row_get_queue_prio(struct request *rq,
877 struct row_data *rd)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300878{
879 const int data_dir = rq_data_dir(rq);
880 const bool is_sync = rq_is_sync(rq);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200881 enum row_queue_prio q_type = ROWQ_MAX_PRIO;
882 int ioprio_class = IOPRIO_PRIO_CLASS(rq->elv.icq->ioc->ioprio);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300883
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200884 switch (ioprio_class) {
885 case IOPRIO_CLASS_RT:
886 if (data_dir == READ)
887 q_type = ROWQ_PRIO_HIGH_READ;
888 else if (is_sync)
889 q_type = ROWQ_PRIO_HIGH_SWRITE;
890 else {
891 pr_err("%s:%s(): got a simple write from RT_CLASS. How???",
892 rq->rq_disk->disk_name, __func__);
893 q_type = ROWQ_PRIO_REG_WRITE;
894 }
895 break;
896 case IOPRIO_CLASS_IDLE:
897 if (data_dir == READ)
898 q_type = ROWQ_PRIO_LOW_READ;
899 else if (is_sync)
900 q_type = ROWQ_PRIO_LOW_SWRITE;
901 else {
902 pr_err("%s:%s(): got a simple write from IDLE_CLASS. How???",
903 rq->rq_disk->disk_name, __func__);
904 q_type = ROWQ_PRIO_REG_WRITE;
905 }
906 break;
907 case IOPRIO_CLASS_NONE:
908 case IOPRIO_CLASS_BE:
909 default:
910 if (data_dir == READ)
911 q_type = ROWQ_PRIO_REG_READ;
912 else if (is_sync)
913 q_type = ROWQ_PRIO_REG_SWRITE;
914 else
915 q_type = ROWQ_PRIO_REG_WRITE;
916 break;
917 }
918
919 return q_type;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300920}
921
922/*
923 * row_set_request() - Set ROW data structures associated with this request.
924 * @q: requests queue
925 * @rq: pointer to the request
926 * @gfp_mask: ignored
927 *
928 */
929static int
930row_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
931{
932 struct row_data *rd = (struct row_data *)q->elevator->elevator_data;
933 unsigned long flags;
934
935 spin_lock_irqsave(q->queue_lock, flags);
936 rq->elv.priv[0] =
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200937 (void *)(&rd->row_queues[row_get_queue_prio(rq, rd)]);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300938 spin_unlock_irqrestore(q->queue_lock, flags);
939
940 return 0;
941}
942
943/********** Helping sysfs functions/defenitions for ROW attributes ******/
944static ssize_t row_var_show(int var, char *page)
945{
946 return snprintf(page, 100, "%d\n", var);
947}
948
949static ssize_t row_var_store(int *var, const char *page, size_t count)
950{
951 int err;
952 err = kstrtoul(page, 10, (unsigned long *)var);
953
954 return count;
955}
956
957#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
958static ssize_t __FUNC(struct elevator_queue *e, char *page) \
959{ \
960 struct row_data *rowd = e->elevator_data; \
961 int __data = __VAR; \
962 if (__CONV) \
963 __data = jiffies_to_msecs(__data); \
964 return row_var_show(__data, (page)); \
965}
966SHOW_FUNCTION(row_hp_read_quantum_show,
967 rowd->row_queues[ROWQ_PRIO_HIGH_READ].disp_quantum, 0);
968SHOW_FUNCTION(row_rp_read_quantum_show,
969 rowd->row_queues[ROWQ_PRIO_REG_READ].disp_quantum, 0);
970SHOW_FUNCTION(row_hp_swrite_quantum_show,
971 rowd->row_queues[ROWQ_PRIO_HIGH_SWRITE].disp_quantum, 0);
972SHOW_FUNCTION(row_rp_swrite_quantum_show,
973 rowd->row_queues[ROWQ_PRIO_REG_SWRITE].disp_quantum, 0);
974SHOW_FUNCTION(row_rp_write_quantum_show,
975 rowd->row_queues[ROWQ_PRIO_REG_WRITE].disp_quantum, 0);
976SHOW_FUNCTION(row_lp_read_quantum_show,
977 rowd->row_queues[ROWQ_PRIO_LOW_READ].disp_quantum, 0);
978SHOW_FUNCTION(row_lp_swrite_quantum_show,
979 rowd->row_queues[ROWQ_PRIO_LOW_SWRITE].disp_quantum, 0);
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200980SHOW_FUNCTION(row_rd_idle_data_show, rowd->rd_idle_data.idle_time_ms, 0);
981SHOW_FUNCTION(row_rd_idle_data_freq_show, rowd->rd_idle_data.freq_ms, 0);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200982SHOW_FUNCTION(row_reg_starv_limit_show,
983 rowd->reg_prio_starvation.starvation_limit, 0);
984SHOW_FUNCTION(row_low_starv_limit_show,
985 rowd->low_prio_starvation.starvation_limit, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300986#undef SHOW_FUNCTION
987
988#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
989static ssize_t __FUNC(struct elevator_queue *e, \
990 const char *page, size_t count) \
991{ \
992 struct row_data *rowd = e->elevator_data; \
993 int __data; \
994 int ret = row_var_store(&__data, (page), count); \
995 if (__CONV) \
996 __data = (int)msecs_to_jiffies(__data); \
997 if (__data < (MIN)) \
998 __data = (MIN); \
999 else if (__data > (MAX)) \
1000 __data = (MAX); \
1001 *(__PTR) = __data; \
1002 return ret; \
1003}
1004STORE_FUNCTION(row_hp_read_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001005&rowd->row_queues[ROWQ_PRIO_HIGH_READ].disp_quantum, 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001006STORE_FUNCTION(row_rp_read_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001007 &rowd->row_queues[ROWQ_PRIO_REG_READ].disp_quantum,
1008 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001009STORE_FUNCTION(row_hp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001010 &rowd->row_queues[ROWQ_PRIO_HIGH_SWRITE].disp_quantum,
1011 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001012STORE_FUNCTION(row_rp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001013 &rowd->row_queues[ROWQ_PRIO_REG_SWRITE].disp_quantum,
1014 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001015STORE_FUNCTION(row_rp_write_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001016 &rowd->row_queues[ROWQ_PRIO_REG_WRITE].disp_quantum,
1017 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001018STORE_FUNCTION(row_lp_read_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001019 &rowd->row_queues[ROWQ_PRIO_LOW_READ].disp_quantum,
1020 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001021STORE_FUNCTION(row_lp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001022 &rowd->row_queues[ROWQ_PRIO_LOW_SWRITE].disp_quantum,
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001023 1, INT_MAX, 0);
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +02001024STORE_FUNCTION(row_rd_idle_data_store, &rowd->rd_idle_data.idle_time_ms,
1025 1, INT_MAX, 0);
1026STORE_FUNCTION(row_rd_idle_data_freq_store, &rowd->rd_idle_data.freq_ms,
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001027 1, INT_MAX, 0);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +02001028STORE_FUNCTION(row_reg_starv_limit_store,
1029 &rowd->reg_prio_starvation.starvation_limit,
1030 1, INT_MAX, 0);
1031STORE_FUNCTION(row_low_starv_limit_store,
1032 &rowd->low_prio_starvation.starvation_limit,
1033 1, INT_MAX, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001034
1035#undef STORE_FUNCTION
1036
1037#define ROW_ATTR(name) \
1038 __ATTR(name, S_IRUGO|S_IWUSR, row_##name##_show, \
1039 row_##name##_store)
1040
1041static struct elv_fs_entry row_attrs[] = {
1042 ROW_ATTR(hp_read_quantum),
1043 ROW_ATTR(rp_read_quantum),
1044 ROW_ATTR(hp_swrite_quantum),
1045 ROW_ATTR(rp_swrite_quantum),
1046 ROW_ATTR(rp_write_quantum),
1047 ROW_ATTR(lp_read_quantum),
1048 ROW_ATTR(lp_swrite_quantum),
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001049 ROW_ATTR(rd_idle_data),
1050 ROW_ATTR(rd_idle_data_freq),
Tatyana Brokhmaneec49472013-03-21 13:02:07 +02001051 ROW_ATTR(reg_starv_limit),
1052 ROW_ATTR(low_starv_limit),
Tatyana Brokhman16349062012-09-20 10:46:10 +03001053 __ATTR_NULL
1054};
1055
1056static struct elevator_type iosched_row = {
1057 .ops = {
1058 .elevator_merge_req_fn = row_merged_requests,
1059 .elevator_dispatch_fn = row_dispatch_requests,
1060 .elevator_add_req_fn = row_add_request,
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +02001061 .elevator_reinsert_req_fn = row_reinsert_req,
Tatyana Brokhman0ef81432012-12-20 19:23:58 +02001062 .elevator_is_urgent_fn = row_urgent_pending,
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +02001063 .elevator_completed_req_fn = row_completed_req,
Tatyana Brokhman16349062012-09-20 10:46:10 +03001064 .elevator_former_req_fn = elv_rb_former_request,
1065 .elevator_latter_req_fn = elv_rb_latter_request,
1066 .elevator_set_req_fn = row_set_request,
1067 .elevator_init_fn = row_init_queue,
1068 .elevator_exit_fn = row_exit_queue,
1069 },
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001070 .icq_size = sizeof(struct io_cq),
1071 .icq_align = __alignof__(struct io_cq),
Tatyana Brokhman16349062012-09-20 10:46:10 +03001072 .elevator_attrs = row_attrs,
1073 .elevator_name = "row",
1074 .elevator_owner = THIS_MODULE,
1075};
1076
1077static int __init row_init(void)
1078{
1079 elv_register(&iosched_row);
1080 return 0;
1081}
1082
1083static void __exit row_exit(void)
1084{
1085 elv_unregister(&iosched_row);
1086}
1087
1088module_init(row_init);
1089module_exit(row_exit);
1090
1091MODULE_LICENSE("GPLv2");
1092MODULE_DESCRIPTION("Read Over Write IO scheduler");