blob: 9b397418e51039d234ab999c52e3425c9d1b888a [file] [log] [blame]
Tatyana Brokhman16349062012-09-20 10:46:10 +03001/*
2 * ROW (Read Over Write) I/O scheduler.
3 *
Tanya Brokhman8feb5972014-06-22 16:11:03 +03004 * Copyright (c) 2012-2014, 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
Lee Susmand2439092013-06-23 16:27:40 +0300100#define ROW_READ_FREQ_MSEC 5
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
Maya Erez7fd5fad2013-04-14 15:19:52 +0300203#define ROW_REG_STARVATION_TOLLERANCE 5000
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200204 struct starvation_data reg_prio_starvation;
Maya Erez7fd5fad2013-04-14 15:19:52 +0300205#define ROW_LOW_STARVATION_TOLLERANCE 10000
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200206 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)) {
Tatyana Brokhmana1d6f9e2013-07-02 14:43:13 +0300349 if (hrtimer_try_to_cancel(
350 &rd->rd_idle_data.hr_timer) >= 0) {
351 row_log_rowq(rd, rqueue->prio,
352 "Canceled delayed work on %d",
353 rd->rd_idle_data.idling_queue_idx);
354 rd->rd_idle_data.idling_queue_idx =
355 ROWQ_MAX_PRIO;
356 }
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200357 }
358 diff_ms = ktime_to_ms(ktime_sub(ktime_get(),
359 rqueue->idle_data.last_insert_time));
360 if (unlikely(diff_ms < 0)) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200361 pr_err("%s(): time delta error: diff_ms < 0",
362 __func__);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200363 rqueue->idle_data.begin_idling = false;
364 return;
365 }
Tanya Brokhman8feb5972014-06-22 16:11:03 +0300366 if (diff_ms < rd->rd_idle_data.freq_ms) {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300367 rqueue->idle_data.begin_idling = true;
368 row_log_rowq(rd, rqueue->prio, "Enable idling");
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200369 } else {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300370 rqueue->idle_data.begin_idling = false;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200371 row_log_rowq(rd, rqueue->prio, "Disable idling (%ldms)",
372 (long)diff_ms);
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200373 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300374
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200375 rqueue->idle_data.last_insert_time = ktime_get();
Tatyana Brokhman16349062012-09-20 10:46:10 +0300376 }
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +0200377 if (row_queues_def[rqueue->prio].is_urgent &&
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200378 !rd->pending_urgent_rq && !rd->urgent_in_flight) {
379 /* Handle High Priority queues */
380 if (rqueue->prio < ROWQ_REG_PRIO_IDX &&
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200381 rd->last_served_ioprio_class != IOPRIO_CLASS_RT &&
382 queue_was_empty) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200383 row_log_rowq(rd, rqueue->prio,
384 "added (high prio) urgent request");
385 rq->cmd_flags |= REQ_URGENT;
386 rd->pending_urgent_rq = rq;
387 } else if (row_rowq_unserved(rd, rqueue->prio)) {
388 /* Handle Regular priotity queues */
389 row_log_rowq(rd, rqueue->prio,
390 "added urgent request (total on queue=%d)",
391 rqueue->nr_req);
392 rq->cmd_flags |= REQ_URGENT;
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200393 rd->pending_urgent_rq = rq;
394 }
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200395 } else
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200396 row_log_rowq(rd, rqueue->prio,
397 "added request (total on queue=%d)", rqueue->nr_req);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300398}
399
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200400/**
401 * row_reinsert_req() - Reinsert request back to the scheduler
402 * @q: requests queue
403 * @rq: request to add
404 *
405 * Reinsert the given request back to the queue it was
406 * dispatched from as if it was never dispatched.
407 *
408 * Returns 0 on success, error code otherwise
409 */
410static int row_reinsert_req(struct request_queue *q,
411 struct request *rq)
412{
413 struct row_data *rd = q->elevator->elevator_data;
414 struct row_queue *rqueue = RQ_ROWQ(rq);
415
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200416 if (!rqueue || rqueue->prio >= ROWQ_MAX_PRIO)
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200417 return -EIO;
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200418
419 list_add(&rq->queuelist, &rqueue->fifo);
420 rd->nr_reqs[rq_data_dir(rq)]++;
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200421 rqueue->nr_req++;
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200422
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200423 row_log_rowq(rd, rqueue->prio,
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200424 "%s request reinserted (total on queue=%d)",
425 (rq_data_dir(rq) == READ ? "READ" : "write"), rqueue->nr_req);
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200426
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200427 if (rq->cmd_flags & REQ_URGENT) {
428 /*
429 * It's not compliant with the design to re-insert
430 * urgent requests. We want to be able to track this
431 * down.
432 */
433 WARN_ON(1);
434 if (!rd->urgent_in_flight) {
435 pr_err("%s(): no urgent in flight", __func__);
436 } else {
437 rd->urgent_in_flight = false;
438 pr_err("%s(): reinserting URGENT %s req",
439 __func__,
440 (rq_data_dir(rq) == READ ? "READ" : "WRITE"));
441 if (rd->pending_urgent_rq) {
442 pr_err("%s(): urgent rq is pending",
443 __func__);
444 rd->pending_urgent_rq->cmd_flags &= ~REQ_URGENT;
445 }
446 rd->pending_urgent_rq = rq;
447 }
448 }
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200449 return 0;
450}
451
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200452static void row_completed_req(struct request_queue *q, struct request *rq)
453{
454 struct row_data *rd = q->elevator->elevator_data;
455
456 if (rq->cmd_flags & REQ_URGENT) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200457 if (!rd->urgent_in_flight) {
458 WARN_ON(1);
459 pr_err("%s(): URGENT req but urgent_in_flight = F",
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200460 __func__);
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200461 }
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200462 rd->urgent_in_flight = false;
463 rq->cmd_flags &= ~REQ_URGENT;
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200464 }
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200465 row_log(q, "completed %s %s req.",
466 (rq->cmd_flags & REQ_URGENT ? "URGENT" : "regular"),
467 (rq_data_dir(rq) == READ ? "READ" : "WRITE"));
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200468}
469
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +0200470/**
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200471 * row_urgent_pending() - Return TRUE if there is an urgent
472 * request on scheduler
473 * @q: requests queue
474 */
475static bool row_urgent_pending(struct request_queue *q)
476{
477 struct row_data *rd = q->elevator->elevator_data;
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200478
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200479 if (rd->urgent_in_flight) {
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200480 row_log(rd->dispatch_queue, "%d urgent requests in flight",
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200481 rd->urgent_in_flight);
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +0200482 return false;
483 }
484
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200485 if (rd->pending_urgent_rq) {
486 row_log(rd->dispatch_queue, "Urgent request pending");
487 return true;
488 }
Shashank Babu Chinta Venkata3df69bf2013-02-26 17:33:55 -0800489
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200490 row_log(rd->dispatch_queue, "no urgent request pending/in flight");
Tatyana Brokhman0ef81432012-12-20 19:23:58 +0200491 return false;
492}
493
494/**
Tatyana Brokhman16349062012-09-20 10:46:10 +0300495 * row_remove_request() - Remove given request from scheduler
496 * @q: requests queue
497 * @rq: request to remove
498 *
499 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200500static void row_remove_request(struct row_data *rd,
Tatyana Brokhman16349062012-09-20 10:46:10 +0300501 struct request *rq)
502{
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200503 struct row_queue *rqueue = RQ_ROWQ(rq);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300504
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200505 list_del_init(&(rq)->queuelist);
506 if (rd->pending_urgent_rq == rq)
507 rd->pending_urgent_rq = NULL;
508 else
509 BUG_ON(rq->cmd_flags & REQ_URGENT);
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200510 rqueue->nr_req--;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300511 rd->nr_reqs[rq_data_dir(rq)]--;
512}
513
514/*
515 * row_dispatch_insert() - move request to dispatch queue
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200516 * @rd: pointer to struct row_data
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200517 * @rq: the request to dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300518 *
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200519 * This function moves the given request to the dispatch queue
Tatyana Brokhman16349062012-09-20 10:46:10 +0300520 *
521 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200522static void row_dispatch_insert(struct row_data *rd, struct request *rq)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300523{
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200524 struct row_queue *rqueue = RQ_ROWQ(rq);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300525
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200526 row_remove_request(rd, rq);
527 elv_dispatch_sort(rd->dispatch_queue, rq);
528 if (rq->cmd_flags & REQ_URGENT) {
529 WARN_ON(rd->urgent_in_flight);
530 rd->urgent_in_flight = true;
531 }
532 rqueue->nr_dispatched++;
533 row_clear_rowq_unserved(rd, rqueue->prio);
534 row_log_rowq(rd, rqueue->prio,
535 " Dispatched request %p nr_disp = %d", rq,
536 rqueue->nr_dispatched);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200537 if (rqueue->prio < ROWQ_REG_PRIO_IDX) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200538 rd->last_served_ioprio_class = IOPRIO_CLASS_RT;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200539 if (row_regular_req_pending(rd))
540 rd->reg_prio_starvation.starvation_counter++;
541 if (row_low_req_pending(rd))
542 rd->low_prio_starvation.starvation_counter++;
543 } else if (rqueue->prio < ROWQ_LOW_PRIO_IDX) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200544 rd->last_served_ioprio_class = IOPRIO_CLASS_BE;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200545 rd->reg_prio_starvation.starvation_counter = 0;
546 if (row_low_req_pending(rd))
547 rd->low_prio_starvation.starvation_counter++;
548 } else {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200549 rd->last_served_ioprio_class = IOPRIO_CLASS_IDLE;
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200550 rd->low_prio_starvation.starvation_counter = 0;
551 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300552}
553
554/*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200555 * row_get_ioprio_class_to_serve() - Return the next I/O priority
556 * class to dispatch requests from
Tatyana Brokhman16349062012-09-20 10:46:10 +0300557 * @rd: pointer to struct row_data
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200558 * @force: flag indicating if forced dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300559 *
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200560 * This function returns the next I/O priority class to serve
561 * {IOPRIO_CLASS_NONE, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE}.
562 * If there are no more requests in scheduler or if we're idling on some queue
563 * IOPRIO_CLASS_NONE will be returned.
564 * If idling is scheduled on a lower priority queue than the one that needs
565 * to be served, it will be canceled.
Tatyana Brokhman16349062012-09-20 10:46:10 +0300566 *
567 */
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200568static int row_get_ioprio_class_to_serve(struct row_data *rd, int force)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300569{
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200570 int i;
571 int ret = IOPRIO_CLASS_NONE;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300572
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200573 if (!rd->nr_reqs[READ] && !rd->nr_reqs[WRITE]) {
Tatyana Brokhman16349062012-09-20 10:46:10 +0300574 row_log(rd->dispatch_queue, "No more requests in scheduler");
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200575 goto check_idling;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300576 }
577
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200578 /* First, go over the high priority queues */
579 for (i = 0; i < ROWQ_REG_PRIO_IDX; i++) {
580 if (!list_empty(&rd->row_queues[i].fifo)) {
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200581 if (hrtimer_active(&rd->rd_idle_data.hr_timer)) {
Tatyana Brokhmana1d6f9e2013-07-02 14:43:13 +0300582 if (hrtimer_try_to_cancel(
583 &rd->rd_idle_data.hr_timer) >= 0) {
584 row_log(rd->dispatch_queue,
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200585 "Canceling delayed work on %d. RT pending",
Tatyana Brokhmana1d6f9e2013-07-02 14:43:13 +0300586 rd->rd_idle_data.idling_queue_idx);
587 rd->rd_idle_data.idling_queue_idx =
588 ROWQ_MAX_PRIO;
589 }
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200590 }
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200591
592 if (row_regular_req_pending(rd) &&
593 (rd->reg_prio_starvation.starvation_counter >=
594 rd->reg_prio_starvation.starvation_limit))
595 ret = IOPRIO_CLASS_BE;
596 else if (row_low_req_pending(rd) &&
597 (rd->low_prio_starvation.starvation_counter >=
598 rd->low_prio_starvation.starvation_limit))
599 ret = IOPRIO_CLASS_IDLE;
600 else
601 ret = IOPRIO_CLASS_RT;
602
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200603 goto done;
604 }
605 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300606
607 /*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200608 * At the moment idling is implemented only for READ queues.
609 * If enabled on WRITE, this needs updating
Tatyana Brokhman16349062012-09-20 10:46:10 +0300610 */
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200611 if (hrtimer_active(&rd->rd_idle_data.hr_timer)) {
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200612 row_log(rd->dispatch_queue, "Delayed work pending. Exiting");
613 goto done;
614 }
615check_idling:
616 /* Check for (high priority) idling and enable if needed */
617 for (i = 0; i < ROWQ_REG_PRIO_IDX && !force; i++) {
618 if (rd->row_queues[i].idle_data.begin_idling &&
619 row_queues_def[i].idling_enabled)
620 goto initiate_idling;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300621 }
622
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200623 /* Regular priority queues */
624 for (i = ROWQ_REG_PRIO_IDX; i < ROWQ_LOW_PRIO_IDX; i++) {
625 if (list_empty(&rd->row_queues[i].fifo)) {
626 /* We can idle only if this is not a forced dispatch */
627 if (rd->row_queues[i].idle_data.begin_idling &&
628 !force && row_queues_def[i].idling_enabled)
629 goto initiate_idling;
630 } else {
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200631 if (row_low_req_pending(rd) &&
632 (rd->low_prio_starvation.starvation_counter >=
633 rd->low_prio_starvation.starvation_limit))
634 ret = IOPRIO_CLASS_IDLE;
635 else
636 ret = IOPRIO_CLASS_BE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200637 goto done;
638 }
639 }
640
641 if (rd->nr_reqs[READ] || rd->nr_reqs[WRITE])
642 ret = IOPRIO_CLASS_IDLE;
643 goto done;
644
645initiate_idling:
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200646 hrtimer_start(&rd->rd_idle_data.hr_timer,
647 ktime_set(0, rd->rd_idle_data.idle_time_ms * NSEC_PER_MSEC),
648 HRTIMER_MODE_REL);
649
650 rd->rd_idle_data.idling_queue_idx = i;
651 row_log_rowq(rd, i, "Scheduled delayed work on %d. exiting", i);
652
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200653done:
654 return ret;
655}
656
657static void row_restart_cycle(struct row_data *rd,
658 int start_idx, int end_idx)
659{
660 int i;
661
662 row_dump_queues_stat(rd);
663 for (i = start_idx; i < end_idx; i++) {
664 if (rd->row_queues[i].nr_dispatched <
665 rd->row_queues[i].disp_quantum)
666 row_mark_rowq_unserved(rd, i);
667 rd->row_queues[i].nr_dispatched = 0;
668 }
669 row_log(rd->dispatch_queue, "Restarting cycle for class @ %d-%d",
670 start_idx, end_idx);
671}
672
673/*
674 * row_get_next_queue() - selects the next queue to dispatch from
675 * @q: requests queue
676 * @rd: pointer to struct row_data
677 * @start_idx/end_idx: indexes in the row_queues array to select a queue
678 * from.
679 *
680 * Return index of the queues to dispatch from. Error code if fails.
681 *
682 */
683static int row_get_next_queue(struct request_queue *q, struct row_data *rd,
684 int start_idx, int end_idx)
685{
686 int i = start_idx;
687 bool restart = true;
688 int ret = -EIO;
689
690 do {
691 if (list_empty(&rd->row_queues[i].fifo) ||
692 rd->row_queues[i].nr_dispatched >=
693 rd->row_queues[i].disp_quantum) {
694 i++;
695 if (i == end_idx && restart) {
696 /* Restart cycle for this priority class */
697 row_restart_cycle(rd, start_idx, end_idx);
698 i = start_idx;
699 restart = false;
700 }
701 } else {
702 ret = i;
703 break;
704 }
705 } while (i < end_idx);
706
707 return ret;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300708}
709
710/*
711 * row_dispatch_requests() - selects the next request to dispatch
712 * @q: requests queue
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200713 * @force: flag indicating if forced dispatch
Tatyana Brokhman16349062012-09-20 10:46:10 +0300714 *
715 * Return 0 if no requests were moved to the dispatch queue.
716 * 1 otherwise
717 *
718 */
719static int row_dispatch_requests(struct request_queue *q, int force)
720{
721 struct row_data *rd = (struct row_data *)q->elevator->elevator_data;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200722 int ret = 0, currq, ioprio_class_to_serve, start_idx, end_idx;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300723
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200724 if (force && hrtimer_active(&rd->rd_idle_data.hr_timer)) {
Tatyana Brokhmana1d6f9e2013-07-02 14:43:13 +0300725 if (hrtimer_try_to_cancel(&rd->rd_idle_data.hr_timer) >= 0) {
726 row_log(rd->dispatch_queue,
727 "Canceled delayed work on %d - forced dispatch",
728 rd->rd_idle_data.idling_queue_idx);
729 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
730 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300731 }
732
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200733 if (rd->pending_urgent_rq) {
734 row_log(rd->dispatch_queue, "dispatching urgent request");
735 row_dispatch_insert(rd, rd->pending_urgent_rq);
736 ret = 1;
737 goto done;
738 }
739
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200740 ioprio_class_to_serve = row_get_ioprio_class_to_serve(rd, force);
741 row_log(rd->dispatch_queue, "Dispatching from %d priority class",
742 ioprio_class_to_serve);
743
744 switch (ioprio_class_to_serve) {
745 case IOPRIO_CLASS_NONE:
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200746 rd->last_served_ioprio_class = IOPRIO_CLASS_NONE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200747 goto done;
748 case IOPRIO_CLASS_RT:
749 start_idx = ROWQ_HIGH_PRIO_IDX;
750 end_idx = ROWQ_REG_PRIO_IDX;
751 break;
752 case IOPRIO_CLASS_BE:
753 start_idx = ROWQ_REG_PRIO_IDX;
754 end_idx = ROWQ_LOW_PRIO_IDX;
755 break;
756 case IOPRIO_CLASS_IDLE:
757 start_idx = ROWQ_LOW_PRIO_IDX;
758 end_idx = ROWQ_MAX_PRIO;
759 break;
760 default:
761 pr_err("%s(): Invalid I/O priority class", __func__);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300762 goto done;
763 }
764
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200765 currq = row_get_next_queue(q, rd, start_idx, end_idx);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300766
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200767 /* Dispatch */
768 if (currq >= 0) {
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200769 row_dispatch_insert(rd,
770 rq_entry_fifo(rd->row_queues[currq].fifo.next));
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200771 ret = 1;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300772 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300773done:
774 return ret;
775}
776
777/*
778 * row_init_queue() - Init scheduler data structures
779 * @q: requests queue
780 *
781 * Return pointer to struct row_data to be saved in elevator for
782 * this dispatch queue
783 *
784 */
785static void *row_init_queue(struct request_queue *q)
786{
787
788 struct row_data *rdata;
789 int i;
790
791 rdata = kmalloc_node(sizeof(*rdata),
792 GFP_KERNEL | __GFP_ZERO, q->node);
793 if (!rdata)
794 return NULL;
795
Tatyana Brokhman522778f2013-01-24 16:17:27 +0200796 memset(rdata, 0, sizeof(*rdata));
Tatyana Brokhman16349062012-09-20 10:46:10 +0300797 for (i = 0; i < ROWQ_MAX_PRIO; i++) {
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200798 INIT_LIST_HEAD(&rdata->row_queues[i].fifo);
Tatyana Brokhman9375bcc2013-01-12 16:23:18 +0200799 rdata->row_queues[i].disp_quantum = row_queues_def[i].quantum;
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200800 rdata->row_queues[i].rdata = rdata;
801 rdata->row_queues[i].prio = i;
802 rdata->row_queues[i].idle_data.begin_idling = false;
803 rdata->row_queues[i].idle_data.last_insert_time =
Tatyana Brokhmanbfb04f62012-12-06 13:17:19 +0200804 ktime_set(0, 0);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300805 }
806
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200807 rdata->reg_prio_starvation.starvation_limit =
808 ROW_REG_STARVATION_TOLLERANCE;
809 rdata->low_prio_starvation.starvation_limit =
810 ROW_LOW_STARVATION_TOLLERANCE;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300811 /*
812 * Currently idling is enabled only for READ queues. If we want to
813 * enable it for write queues also, note that idling frequency will
814 * be the same in both cases
815 */
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200816 rdata->rd_idle_data.idle_time_ms = ROW_IDLE_TIME_MSEC;
817 rdata->rd_idle_data.freq_ms = ROW_READ_FREQ_MSEC;
818 hrtimer_init(&rdata->rd_idle_data.hr_timer,
819 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
820 rdata->rd_idle_data.hr_timer.function = &row_idle_hrtimer_fn;
821
822 INIT_WORK(&rdata->rd_idle_data.idle_work, kick_queue);
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200823 rdata->last_served_ioprio_class = IOPRIO_CLASS_NONE;
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200824 rdata->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300825 rdata->dispatch_queue = q;
826
Tatyana Brokhman16349062012-09-20 10:46:10 +0300827 return rdata;
828}
829
830/*
831 * row_exit_queue() - called on unloading the RAW scheduler
832 * @e: poiner to struct elevator_queue
833 *
834 */
835static void row_exit_queue(struct elevator_queue *e)
836{
837 struct row_data *rd = (struct row_data *)e->elevator_data;
838 int i;
839
840 for (i = 0; i < ROWQ_MAX_PRIO; i++)
Tatyana Brokhman8a970bc2013-01-12 16:21:12 +0200841 BUG_ON(!list_empty(&rd->row_queues[i].fifo));
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +0200842 if (hrtimer_cancel(&rd->rd_idle_data.hr_timer))
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200843 pr_err("%s(): idle timer was active!", __func__);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200844 rd->rd_idle_data.idling_queue_idx = ROWQ_MAX_PRIO;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300845 kfree(rd);
846}
847
848/*
849 * row_merged_requests() - Called when 2 requests are merged
850 * @q: requests queue
851 * @rq: request the two requests were merged into
852 * @next: request that was merged
853 */
854static void row_merged_requests(struct request_queue *q, struct request *rq,
855 struct request *next)
856{
857 struct row_queue *rqueue = RQ_ROWQ(next);
858
859 list_del_init(&next->queuelist);
Tatyana Brokhmanbd56be32013-01-13 22:04:59 +0200860 rqueue->nr_req--;
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200861 if (rqueue->rdata->pending_urgent_rq == next) {
862 pr_err("\n\nROW_WARNING: merging pending urgent!");
863 rqueue->rdata->pending_urgent_rq = rq;
864 rq->cmd_flags |= REQ_URGENT;
865 WARN_ON(!(next->cmd_flags & REQ_URGENT));
866 next->cmd_flags &= ~REQ_URGENT;
867 }
Tatyana Brokhman16349062012-09-20 10:46:10 +0300868 rqueue->rdata->nr_reqs[rq_data_dir(rq)]--;
869}
870
871/*
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200872 * row_get_queue_prio() - Get queue priority for a given request
Tatyana Brokhman16349062012-09-20 10:46:10 +0300873 *
874 * This is a helping function which purpose is to determine what
875 * ROW queue the given request should be added to (and
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200876 * dispatched from later on)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300877 *
Tatyana Brokhman16349062012-09-20 10:46:10 +0300878 */
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200879static enum row_queue_prio row_get_queue_prio(struct request *rq,
880 struct row_data *rd)
Tatyana Brokhman16349062012-09-20 10:46:10 +0300881{
882 const int data_dir = rq_data_dir(rq);
883 const bool is_sync = rq_is_sync(rq);
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200884 enum row_queue_prio q_type = ROWQ_MAX_PRIO;
885 int ioprio_class = IOPRIO_PRIO_CLASS(rq->elv.icq->ioc->ioprio);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300886
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +0200887 switch (ioprio_class) {
888 case IOPRIO_CLASS_RT:
889 if (data_dir == READ)
890 q_type = ROWQ_PRIO_HIGH_READ;
891 else if (is_sync)
892 q_type = ROWQ_PRIO_HIGH_SWRITE;
893 else {
894 pr_err("%s:%s(): got a simple write from RT_CLASS. How???",
895 rq->rq_disk->disk_name, __func__);
896 q_type = ROWQ_PRIO_REG_WRITE;
897 }
898 break;
899 case IOPRIO_CLASS_IDLE:
900 if (data_dir == READ)
901 q_type = ROWQ_PRIO_LOW_READ;
902 else if (is_sync)
903 q_type = ROWQ_PRIO_LOW_SWRITE;
904 else {
905 pr_err("%s:%s(): got a simple write from IDLE_CLASS. How???",
906 rq->rq_disk->disk_name, __func__);
907 q_type = ROWQ_PRIO_REG_WRITE;
908 }
909 break;
910 case IOPRIO_CLASS_NONE:
911 case IOPRIO_CLASS_BE:
912 default:
913 if (data_dir == READ)
914 q_type = ROWQ_PRIO_REG_READ;
915 else if (is_sync)
916 q_type = ROWQ_PRIO_REG_SWRITE;
917 else
918 q_type = ROWQ_PRIO_REG_WRITE;
919 break;
920 }
921
922 return q_type;
Tatyana Brokhman16349062012-09-20 10:46:10 +0300923}
924
925/*
926 * row_set_request() - Set ROW data structures associated with this request.
927 * @q: requests queue
928 * @rq: pointer to the request
929 * @gfp_mask: ignored
930 *
931 */
932static int
933row_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
934{
935 struct row_data *rd = (struct row_data *)q->elevator->elevator_data;
936 unsigned long flags;
937
938 spin_lock_irqsave(q->queue_lock, flags);
939 rq->elv.priv[0] =
Tatyana Brokhmanfe6fd2f2013-03-12 21:17:18 +0200940 (void *)(&rd->row_queues[row_get_queue_prio(rq, rd)]);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300941 spin_unlock_irqrestore(q->queue_lock, flags);
942
943 return 0;
944}
945
946/********** Helping sysfs functions/defenitions for ROW attributes ******/
947static ssize_t row_var_show(int var, char *page)
948{
949 return snprintf(page, 100, "%d\n", var);
950}
951
952static ssize_t row_var_store(int *var, const char *page, size_t count)
953{
954 int err;
955 err = kstrtoul(page, 10, (unsigned long *)var);
956
957 return count;
958}
959
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200960#define SHOW_FUNCTION(__FUNC, __VAR) \
Tatyana Brokhman16349062012-09-20 10:46:10 +0300961static ssize_t __FUNC(struct elevator_queue *e, char *page) \
962{ \
963 struct row_data *rowd = e->elevator_data; \
964 int __data = __VAR; \
Tatyana Brokhman16349062012-09-20 10:46:10 +0300965 return row_var_show(__data, (page)); \
966}
967SHOW_FUNCTION(row_hp_read_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200968 rowd->row_queues[ROWQ_PRIO_HIGH_READ].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300969SHOW_FUNCTION(row_rp_read_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200970 rowd->row_queues[ROWQ_PRIO_REG_READ].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300971SHOW_FUNCTION(row_hp_swrite_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200972 rowd->row_queues[ROWQ_PRIO_HIGH_SWRITE].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300973SHOW_FUNCTION(row_rp_swrite_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200974 rowd->row_queues[ROWQ_PRIO_REG_SWRITE].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300975SHOW_FUNCTION(row_rp_write_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200976 rowd->row_queues[ROWQ_PRIO_REG_WRITE].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300977SHOW_FUNCTION(row_lp_read_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200978 rowd->row_queues[ROWQ_PRIO_LOW_READ].disp_quantum);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300979SHOW_FUNCTION(row_lp_swrite_quantum_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200980 rowd->row_queues[ROWQ_PRIO_LOW_SWRITE].disp_quantum);
981SHOW_FUNCTION(row_rd_idle_data_show, rowd->rd_idle_data.idle_time_ms);
982SHOW_FUNCTION(row_rd_idle_data_freq_show, rowd->rd_idle_data.freq_ms);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200983SHOW_FUNCTION(row_reg_starv_limit_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200984 rowd->reg_prio_starvation.starvation_limit);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +0200985SHOW_FUNCTION(row_low_starv_limit_show,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200986 rowd->low_prio_starvation.starvation_limit);
Tatyana Brokhman16349062012-09-20 10:46:10 +0300987#undef SHOW_FUNCTION
988
Tatyana Brokhmane9aab612013-03-21 11:04:02 +0200989#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
Tatyana Brokhman16349062012-09-20 10:46:10 +0300990static ssize_t __FUNC(struct elevator_queue *e, \
991 const char *page, size_t count) \
992{ \
993 struct row_data *rowd = e->elevator_data; \
994 int __data; \
995 int ret = row_var_store(&__data, (page), count); \
Tatyana Brokhman16349062012-09-20 10:46:10 +0300996 if (__data < (MIN)) \
997 __data = (MIN); \
998 else if (__data > (MAX)) \
999 __data = (MAX); \
1000 *(__PTR) = __data; \
1001 return ret; \
1002}
1003STORE_FUNCTION(row_hp_read_quantum_store,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001004&rowd->row_queues[ROWQ_PRIO_HIGH_READ].disp_quantum, 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001005STORE_FUNCTION(row_rp_read_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001006 &rowd->row_queues[ROWQ_PRIO_REG_READ].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001007 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001008STORE_FUNCTION(row_hp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001009 &rowd->row_queues[ROWQ_PRIO_HIGH_SWRITE].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001010 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001011STORE_FUNCTION(row_rp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001012 &rowd->row_queues[ROWQ_PRIO_REG_SWRITE].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001013 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001014STORE_FUNCTION(row_rp_write_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001015 &rowd->row_queues[ROWQ_PRIO_REG_WRITE].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001016 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001017STORE_FUNCTION(row_lp_read_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001018 &rowd->row_queues[ROWQ_PRIO_LOW_READ].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001019 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001020STORE_FUNCTION(row_lp_swrite_quantum_store,
Tatyana Brokhman0a0345a2012-10-15 20:50:54 +02001021 &rowd->row_queues[ROWQ_PRIO_LOW_SWRITE].disp_quantum,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001022 1, INT_MAX);
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +02001023STORE_FUNCTION(row_rd_idle_data_store, &rowd->rd_idle_data.idle_time_ms,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001024 1, INT_MAX);
Tatyana Brokhmance1a8ed2013-01-17 20:56:07 +02001025STORE_FUNCTION(row_rd_idle_data_freq_store, &rowd->rd_idle_data.freq_ms,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001026 1, INT_MAX);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +02001027STORE_FUNCTION(row_reg_starv_limit_store,
1028 &rowd->reg_prio_starvation.starvation_limit,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001029 1, INT_MAX);
Tatyana Brokhmaneec49472013-03-21 13:02:07 +02001030STORE_FUNCTION(row_low_starv_limit_store,
1031 &rowd->low_prio_starvation.starvation_limit,
Tatyana Brokhmane9aab612013-03-21 11:04:02 +02001032 1, INT_MAX);
Tatyana Brokhman16349062012-09-20 10:46:10 +03001033
1034#undef STORE_FUNCTION
1035
1036#define ROW_ATTR(name) \
1037 __ATTR(name, S_IRUGO|S_IWUSR, row_##name##_show, \
1038 row_##name##_store)
1039
1040static struct elv_fs_entry row_attrs[] = {
1041 ROW_ATTR(hp_read_quantum),
1042 ROW_ATTR(rp_read_quantum),
1043 ROW_ATTR(hp_swrite_quantum),
1044 ROW_ATTR(rp_swrite_quantum),
1045 ROW_ATTR(rp_write_quantum),
1046 ROW_ATTR(lp_read_quantum),
1047 ROW_ATTR(lp_swrite_quantum),
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001048 ROW_ATTR(rd_idle_data),
1049 ROW_ATTR(rd_idle_data_freq),
Tatyana Brokhmaneec49472013-03-21 13:02:07 +02001050 ROW_ATTR(reg_starv_limit),
1051 ROW_ATTR(low_starv_limit),
Tatyana Brokhman16349062012-09-20 10:46:10 +03001052 __ATTR_NULL
1053};
1054
1055static struct elevator_type iosched_row = {
1056 .ops = {
1057 .elevator_merge_req_fn = row_merged_requests,
1058 .elevator_dispatch_fn = row_dispatch_requests,
1059 .elevator_add_req_fn = row_add_request,
Tatyana Brokhmanb7bf9ac2012-10-30 08:33:06 +02001060 .elevator_reinsert_req_fn = row_reinsert_req,
Tatyana Brokhman0ef81432012-12-20 19:23:58 +02001061 .elevator_is_urgent_fn = row_urgent_pending,
Tatyana Brokhman4c3c3cc2013-01-24 15:08:40 +02001062 .elevator_completed_req_fn = row_completed_req,
Tatyana Brokhman16349062012-09-20 10:46:10 +03001063 .elevator_former_req_fn = elv_rb_former_request,
1064 .elevator_latter_req_fn = elv_rb_latter_request,
1065 .elevator_set_req_fn = row_set_request,
1066 .elevator_init_fn = row_init_queue,
1067 .elevator_exit_fn = row_exit_queue,
1068 },
Tatyana Brokhmandb7c1532013-01-23 17:15:49 +02001069 .icq_size = sizeof(struct io_cq),
1070 .icq_align = __alignof__(struct io_cq),
Tatyana Brokhman16349062012-09-20 10:46:10 +03001071 .elevator_attrs = row_attrs,
1072 .elevator_name = "row",
1073 .elevator_owner = THIS_MODULE,
1074};
1075
1076static int __init row_init(void)
1077{
1078 elv_register(&iosched_row);
1079 return 0;
1080}
1081
1082static void __exit row_exit(void)
1083{
1084 elv_unregister(&iosched_row);
1085}
1086
1087module_init(row_init);
1088module_exit(row_exit);
1089
1090MODULE_LICENSE("GPLv2");
1091MODULE_DESCRIPTION("Read Over Write IO scheduler");