forked from lordarcadius/electrablue_mido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzen-iosched.c
292 lines (245 loc) · 7.54 KB
/
zen-iosched.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Zen IO scheduler
* Primarily based on Noop, deadline, and SIO IO schedulers.
*
* Copyright (C) 2012 Brandon Berhent <bbedward@gmail.com>
* (C) 2014 LoungeKatt <twistedumbrella@gmail.com>
* (c) 2015 Fixes to stop crashing on 3.10 by Matthew Alex <matthewalex@outlook.com>
* (c) 2016 POrt and fixes for Linux 3.18 by engstk <eng.stk@sapo.pt>
*
* FCFS, dispatches are back-inserted, deadlines ensure fairness.
* Should work best with devices where there is no travel delay.
*/
#include <linux/blkdev.h>
#include <linux/elevator.h>
#include <linux/bio.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
enum zen_data_dir { ASYNC, SYNC };
static const int sync_expire = HZ / 2; /* max time before a sync is submitted. */
static const int async_expire = 5 * HZ; /* ditto for async, these limits are SOFT! */
static const int fifo_batch = 16;
struct zen_data {
/* Runtime Data */
/* Requests are only present on fifo_list */
struct list_head fifo_list[2];
unsigned int batching; /* number of sequential requests made */
/* tunables */
int fifo_expire[2];
int fifo_batch;
};
static inline struct zen_data *
zen_get_data(struct request_queue *q) {
return q->elevator->elevator_data;
}
static void zen_dispatch(struct zen_data *, struct request *);
static void
zen_merged_requests(struct request_queue *q, struct request *req,
struct request *next)
{
/*
* if next expires before rq, assign its expire time to arq
* and move into next position (next will be deleted) in fifo
*/
if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
if (time_before(next->fifo_time, req->fifo_time)) {
list_move(&req->queuelist, &next->queuelist);
req->fifo_time = next->fifo_time;
}
}
/* next request is gone */
rq_fifo_clear(next);
}
static void zen_add_request(struct request_queue *q, struct request *rq)
{
struct zen_data *zdata = zen_get_data(q);
const int sync = rq_is_sync(rq);
if (zdata->fifo_expire[sync]) {
rq->fifo_time = jiffies + zdata->fifo_expire[sync];
list_add_tail(&rq->queuelist, &zdata->fifo_list[sync]);
}
}
static void zen_dispatch(struct zen_data *zdata, struct request *rq)
{
/* Remove request from list and dispatch it */
rq_fifo_clear(rq);
elv_dispatch_add_tail(rq->q, rq);
/* Increment # of sequential requests */
zdata->batching++;
}
/*
* get the first expired request in direction ddir
*/
static struct request *
zen_expired_request(struct zen_data *zdata, int ddir)
{
struct request *rq;
if (list_empty(&zdata->fifo_list[ddir]))
return NULL;
rq = rq_entry_fifo(zdata->fifo_list[ddir].next);
if (time_after_eq(jiffies, rq->fifo_time))
return rq;
return NULL;
}
/*
* zen_check_fifo returns 0 if there are no expired requests on the fifo,
* otherwise it returns the next expired request
*/
static struct request *
zen_check_fifo(struct zen_data *zdata)
{
struct request *rq_sync = zen_expired_request(zdata, SYNC);
struct request *rq_async = zen_expired_request(zdata, ASYNC);
if (rq_async && rq_sync) {
if (time_after(rq_async->fifo_time, rq_sync->fifo_time))
return rq_sync;
} else if (rq_sync) {
return rq_sync;
} else if (rq_async) {
return rq_async;
}
return 0;
}
static struct request *
zen_choose_request(struct zen_data *zdata)
{
/*
* Retrieve request from available fifo list.
* Synchronous requests have priority over asynchronous.
*/
if (!list_empty(&zdata->fifo_list[SYNC]))
return rq_entry_fifo(zdata->fifo_list[SYNC].next);
if (!list_empty(&zdata->fifo_list[ASYNC]))
return rq_entry_fifo(zdata->fifo_list[ASYNC].next);
return NULL;
}
static int zen_dispatch_requests(struct request_queue *q, int force)
{
struct zen_data *zdata = zen_get_data(q);
struct request *rq = NULL;
/* Check for and issue expired requests */
if (zdata->batching > zdata->fifo_batch) {
zdata->batching = 0;
rq = zen_check_fifo(zdata);
}
if (!rq) {
rq = zen_choose_request(zdata);
if (!rq)
return 0;
}
zen_dispatch(zdata, rq);
return 1;
}
static int zen_init_queue(struct request_queue *q, struct elevator_type *e)
{
struct zen_data *zdata;
struct elevator_queue *eq;
eq = elevator_alloc(q, e);
if (!eq)
return -ENOMEM;
zdata = kmalloc_node(sizeof(*zdata), GFP_KERNEL, q->node);
if (!zdata) {
kobject_put(&eq->kobj);
return -ENOMEM;
}
eq->elevator_data = zdata;
spin_lock_irq(q->queue_lock);
q->elevator = eq;
spin_unlock_irq(q->queue_lock);
INIT_LIST_HEAD(&zdata->fifo_list[SYNC]);
INIT_LIST_HEAD(&zdata->fifo_list[ASYNC]);
zdata->fifo_expire[SYNC] = sync_expire;
zdata->fifo_expire[ASYNC] = async_expire;
zdata->fifo_batch = fifo_batch;
return 0;
}
static void zen_exit_queue(struct elevator_queue *e)
{
struct zen_data *zdata = e->elevator_data;
BUG_ON(!list_empty(&zdata->fifo_list[SYNC]));
BUG_ON(!list_empty(&zdata->fifo_list[ASYNC]));
kfree(zdata);
}
/* Sysfs */
static ssize_t
zen_var_show(int var, char *page)
{
return sprintf(page, "%d\n", var);
}
static ssize_t
zen_var_store(int *var, const char *page, size_t count)
{
*var = simple_strtol(page, NULL, 10);
return count;
}
#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
static ssize_t __FUNC(struct elevator_queue *e, char *page) \
{ \
struct zen_data *zdata = e->elevator_data; \
int __data = __VAR; \
if (__CONV) \
__data = jiffies_to_msecs(__data); \
return zen_var_show(__data, (page)); \
}
SHOW_FUNCTION(zen_sync_expire_show, zdata->fifo_expire[SYNC], 1);
SHOW_FUNCTION(zen_async_expire_show, zdata->fifo_expire[ASYNC], 1);
SHOW_FUNCTION(zen_fifo_batch_show, zdata->fifo_batch, 0);
#undef SHOW_FUNCTION
#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
{ \
struct zen_data *zdata = e->elevator_data; \
int __data; \
int ret = zen_var_store(&__data, (page), count); \
if (__data < (MIN)) \
__data = (MIN); \
else if (__data > (MAX)) \
__data = (MAX); \
if (__CONV) \
*(__PTR) = msecs_to_jiffies(__data); \
else \
*(__PTR) = __data; \
return ret; \
}
STORE_FUNCTION(zen_sync_expire_store, &zdata->fifo_expire[SYNC], 0, INT_MAX, 1);
STORE_FUNCTION(zen_async_expire_store, &zdata->fifo_expire[ASYNC], 0, INT_MAX, 1);
STORE_FUNCTION(zen_fifo_batch_store, &zdata->fifo_batch, 0, INT_MAX, 0);
#undef STORE_FUNCTION
#define DD_ATTR(name) \
__ATTR(name, S_IRUGO|S_IWUSR, zen_##name##_show, \
zen_##name##_store)
static struct elv_fs_entry zen_attrs[] = {
DD_ATTR(sync_expire),
DD_ATTR(async_expire),
DD_ATTR(fifo_batch),
__ATTR_NULL
};
static struct elevator_type iosched_zen = {
.ops = {
.elevator_merge_req_fn = zen_merged_requests,
.elevator_dispatch_fn = zen_dispatch_requests,
.elevator_add_req_fn = zen_add_request,
.elevator_former_req_fn = elv_rb_former_request,
.elevator_latter_req_fn = elv_rb_latter_request,
.elevator_init_fn = zen_init_queue,
.elevator_exit_fn = zen_exit_queue,
},
.elevator_attrs = zen_attrs,
.elevator_name = "zen",
.elevator_owner = THIS_MODULE,
};
static int __init zen_init(void)
{
return elv_register(&iosched_zen);
}
static void __exit zen_exit(void)
{
elv_unregister(&iosched_zen);
}
module_init(zen_init);
module_exit(zen_exit);
MODULE_AUTHOR("Brandon Berhent");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Zen IO scheduler");
MODULE_VERSION("1.1");