blk-mq: add file comments and update copyright notices
[deliverable/linux.git] / drivers / block / null_blk.c
CommitLineData
f2298c04 1#include <linux/module.h>
fc1bc354 2
f2298c04
JA
3#include <linux/moduleparam.h>
4#include <linux/sched.h>
5#include <linux/fs.h>
6#include <linux/blkdev.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/blk-mq.h>
10#include <linux/hrtimer.h>
11
12struct nullb_cmd {
13 struct list_head list;
14 struct llist_node ll_list;
15 struct call_single_data csd;
16 struct request *rq;
17 struct bio *bio;
18 unsigned int tag;
19 struct nullb_queue *nq;
20};
21
22struct nullb_queue {
23 unsigned long *tag_map;
24 wait_queue_head_t wait;
25 unsigned int queue_depth;
26
27 struct nullb_cmd *cmds;
28};
29
30struct nullb {
31 struct list_head list;
32 unsigned int index;
33 struct request_queue *q;
34 struct gendisk *disk;
24d2f903 35 struct blk_mq_tag_set tag_set;
f2298c04
JA
36 struct hrtimer timer;
37 unsigned int queue_depth;
38 spinlock_t lock;
39
40 struct nullb_queue *queues;
41 unsigned int nr_queues;
42};
43
44static LIST_HEAD(nullb_list);
45static struct mutex lock;
46static int null_major;
47static int nullb_indexes;
48
49struct completion_queue {
50 struct llist_head list;
51 struct hrtimer timer;
52};
53
54/*
55 * These are per-cpu for now, they will need to be configured by the
56 * complete_queues parameter and appropriately mapped.
57 */
58static DEFINE_PER_CPU(struct completion_queue, completion_queues);
59
60enum {
61 NULL_IRQ_NONE = 0,
62 NULL_IRQ_SOFTIRQ = 1,
63 NULL_IRQ_TIMER = 2,
ce2c350b 64};
f2298c04 65
ce2c350b 66enum {
f2298c04
JA
67 NULL_Q_BIO = 0,
68 NULL_Q_RQ = 1,
69 NULL_Q_MQ = 2,
70};
71
2d263a78 72static int submit_queues;
f2298c04
JA
73module_param(submit_queues, int, S_IRUGO);
74MODULE_PARM_DESC(submit_queues, "Number of submission queues");
75
76static int home_node = NUMA_NO_NODE;
77module_param(home_node, int, S_IRUGO);
78MODULE_PARM_DESC(home_node, "Home node for the device");
79
80static int queue_mode = NULL_Q_MQ;
81module_param(queue_mode, int, S_IRUGO);
82MODULE_PARM_DESC(use_mq, "Use blk-mq interface (0=bio,1=rq,2=multiqueue)");
83
84static int gb = 250;
85module_param(gb, int, S_IRUGO);
86MODULE_PARM_DESC(gb, "Size in GB");
87
88static int bs = 512;
89module_param(bs, int, S_IRUGO);
90MODULE_PARM_DESC(bs, "Block size (in bytes)");
91
92static int nr_devices = 2;
93module_param(nr_devices, int, S_IRUGO);
94MODULE_PARM_DESC(nr_devices, "Number of devices to register");
95
96static int irqmode = NULL_IRQ_SOFTIRQ;
97module_param(irqmode, int, S_IRUGO);
98MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
99
100static int completion_nsec = 10000;
101module_param(completion_nsec, int, S_IRUGO);
102MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
103
104static int hw_queue_depth = 64;
105module_param(hw_queue_depth, int, S_IRUGO);
106MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
107
20005244 108static bool use_per_node_hctx = false;
f2298c04 109module_param(use_per_node_hctx, bool, S_IRUGO);
20005244 110MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
f2298c04
JA
111
112static void put_tag(struct nullb_queue *nq, unsigned int tag)
113{
114 clear_bit_unlock(tag, nq->tag_map);
115
116 if (waitqueue_active(&nq->wait))
117 wake_up(&nq->wait);
118}
119
120static unsigned int get_tag(struct nullb_queue *nq)
121{
122 unsigned int tag;
123
124 do {
125 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
126 if (tag >= nq->queue_depth)
127 return -1U;
128 } while (test_and_set_bit_lock(tag, nq->tag_map));
129
130 return tag;
131}
132
133static void free_cmd(struct nullb_cmd *cmd)
134{
135 put_tag(cmd->nq, cmd->tag);
136}
137
138static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
139{
140 struct nullb_cmd *cmd;
141 unsigned int tag;
142
143 tag = get_tag(nq);
144 if (tag != -1U) {
145 cmd = &nq->cmds[tag];
146 cmd->tag = tag;
147 cmd->nq = nq;
148 return cmd;
149 }
150
151 return NULL;
152}
153
154static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
155{
156 struct nullb_cmd *cmd;
157 DEFINE_WAIT(wait);
158
159 cmd = __alloc_cmd(nq);
160 if (cmd || !can_wait)
161 return cmd;
162
163 do {
164 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
165 cmd = __alloc_cmd(nq);
166 if (cmd)
167 break;
168
169 io_schedule();
170 } while (1);
171
172 finish_wait(&nq->wait, &wait);
173 return cmd;
174}
175
176static void end_cmd(struct nullb_cmd *cmd)
177{
ce2c350b
CH
178 switch (queue_mode) {
179 case NULL_Q_MQ:
180 blk_mq_end_io(cmd->rq, 0);
181 return;
182 case NULL_Q_RQ:
183 INIT_LIST_HEAD(&cmd->rq->queuelist);
184 blk_end_request_all(cmd->rq, 0);
185 break;
186 case NULL_Q_BIO:
f2298c04 187 bio_endio(cmd->bio, 0);
ce2c350b
CH
188 break;
189 }
f2298c04 190
ce2c350b 191 free_cmd(cmd);
f2298c04
JA
192}
193
194static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
195{
196 struct completion_queue *cq;
197 struct llist_node *entry;
198 struct nullb_cmd *cmd;
199
200 cq = &per_cpu(completion_queues, smp_processor_id());
201
202 while ((entry = llist_del_all(&cq->list)) != NULL) {
d7790b92 203 entry = llist_reverse_order(entry);
f2298c04
JA
204 do {
205 cmd = container_of(entry, struct nullb_cmd, ll_list);
206 end_cmd(cmd);
207 entry = entry->next;
208 } while (entry);
209 }
210
211 return HRTIMER_NORESTART;
212}
213
214static void null_cmd_end_timer(struct nullb_cmd *cmd)
215{
216 struct completion_queue *cq = &per_cpu(completion_queues, get_cpu());
217
218 cmd->ll_list.next = NULL;
219 if (llist_add(&cmd->ll_list, &cq->list)) {
220 ktime_t kt = ktime_set(0, completion_nsec);
221
222 hrtimer_start(&cq->timer, kt, HRTIMER_MODE_REL);
223 }
224
225 put_cpu();
226}
227
228static void null_softirq_done_fn(struct request *rq)
229{
9d74e257 230 end_cmd(blk_mq_rq_to_pdu(rq));
f2298c04
JA
231}
232
f2298c04
JA
233static inline void null_handle_cmd(struct nullb_cmd *cmd)
234{
235 /* Complete IO by inline, softirq or timer */
236 switch (irqmode) {
f2298c04 237 case NULL_IRQ_SOFTIRQ:
ce2c350b
CH
238 switch (queue_mode) {
239 case NULL_Q_MQ:
240 blk_mq_complete_request(cmd->rq);
241 break;
242 case NULL_Q_RQ:
243 blk_complete_request(cmd->rq);
244 break;
245 case NULL_Q_BIO:
246 /*
247 * XXX: no proper submitting cpu information available.
248 */
249 end_cmd(cmd);
250 break;
251 }
252 break;
253 case NULL_IRQ_NONE:
f2298c04 254 end_cmd(cmd);
f2298c04
JA
255 break;
256 case NULL_IRQ_TIMER:
257 null_cmd_end_timer(cmd);
258 break;
259 }
260}
261
262static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
263{
264 int index = 0;
265
266 if (nullb->nr_queues != 1)
267 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
268
269 return &nullb->queues[index];
270}
271
272static void null_queue_bio(struct request_queue *q, struct bio *bio)
273{
274 struct nullb *nullb = q->queuedata;
275 struct nullb_queue *nq = nullb_to_queue(nullb);
276 struct nullb_cmd *cmd;
277
278 cmd = alloc_cmd(nq, 1);
279 cmd->bio = bio;
280
281 null_handle_cmd(cmd);
282}
283
284static int null_rq_prep_fn(struct request_queue *q, struct request *req)
285{
286 struct nullb *nullb = q->queuedata;
287 struct nullb_queue *nq = nullb_to_queue(nullb);
288 struct nullb_cmd *cmd;
289
290 cmd = alloc_cmd(nq, 0);
291 if (cmd) {
292 cmd->rq = req;
293 req->special = cmd;
294 return BLKPREP_OK;
295 }
296
297 return BLKPREP_DEFER;
298}
299
300static void null_request_fn(struct request_queue *q)
301{
302 struct request *rq;
303
304 while ((rq = blk_fetch_request(q)) != NULL) {
305 struct nullb_cmd *cmd = rq->special;
306
307 spin_unlock_irq(q->queue_lock);
308 null_handle_cmd(cmd);
309 spin_lock_irq(q->queue_lock);
310 }
311}
312
313static int null_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
314{
9d74e257 315 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
f2298c04
JA
316
317 cmd->rq = rq;
318 cmd->nq = hctx->driver_data;
319
320 null_handle_cmd(cmd);
321 return BLK_MQ_RQ_QUEUE_OK;
322}
323
24d2f903 324static struct blk_mq_hw_ctx *null_alloc_hctx(struct blk_mq_tag_set *set,
f14bbe77
JA
325 unsigned int hctx_index,
326 int node)
f2298c04 327{
f14bbe77 328 return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL, node);
f2298c04
JA
329}
330
331static void null_free_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_index)
332{
333 kfree(hctx);
334}
335
2d263a78
MB
336static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
337{
338 BUG_ON(!nullb);
339 BUG_ON(!nq);
340
341 init_waitqueue_head(&nq->wait);
342 nq->queue_depth = nullb->queue_depth;
343}
344
f2298c04
JA
345static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
346 unsigned int index)
347{
348 struct nullb *nullb = data;
349 struct nullb_queue *nq = &nullb->queues[index];
350
f2298c04 351 hctx->driver_data = nq;
2d263a78
MB
352 null_init_queue(nullb, nq);
353 nullb->nr_queues++;
f2298c04
JA
354
355 return 0;
356}
357
358static struct blk_mq_ops null_mq_ops = {
359 .queue_rq = null_queue_rq,
360 .map_queue = blk_mq_map_queue,
361 .init_hctx = null_init_hctx,
ce2c350b 362 .complete = null_softirq_done_fn,
24d2f903
CH
363 .alloc_hctx = blk_mq_alloc_single_hw_queue,
364 .free_hctx = blk_mq_free_single_hw_queue,
f2298c04
JA
365};
366
24d2f903
CH
367static struct blk_mq_ops null_mq_ops_pernode = {
368 .queue_rq = null_queue_rq,
369 .map_queue = blk_mq_map_queue,
370 .init_hctx = null_init_hctx,
371 .complete = null_softirq_done_fn,
372 .alloc_hctx = null_alloc_hctx,
373 .free_hctx = null_free_hctx,
f2298c04
JA
374};
375
376static void null_del_dev(struct nullb *nullb)
377{
378 list_del_init(&nullb->list);
379
380 del_gendisk(nullb->disk);
518d00b7 381 blk_cleanup_queue(nullb->q);
24d2f903
CH
382 if (queue_mode == NULL_Q_MQ)
383 blk_mq_free_tag_set(&nullb->tag_set);
f2298c04
JA
384 put_disk(nullb->disk);
385 kfree(nullb);
386}
387
388static int null_open(struct block_device *bdev, fmode_t mode)
389{
390 return 0;
391}
392
393static void null_release(struct gendisk *disk, fmode_t mode)
394{
395}
396
397static const struct block_device_operations null_fops = {
398 .owner = THIS_MODULE,
399 .open = null_open,
400 .release = null_release,
401};
402
403static int setup_commands(struct nullb_queue *nq)
404{
405 struct nullb_cmd *cmd;
406 int i, tag_size;
407
408 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
409 if (!nq->cmds)
2d263a78 410 return -ENOMEM;
f2298c04
JA
411
412 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
413 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
414 if (!nq->tag_map) {
415 kfree(nq->cmds);
2d263a78 416 return -ENOMEM;
f2298c04
JA
417 }
418
419 for (i = 0; i < nq->queue_depth; i++) {
420 cmd = &nq->cmds[i];
421 INIT_LIST_HEAD(&cmd->list);
422 cmd->ll_list.next = NULL;
423 cmd->tag = -1U;
424 }
425
426 return 0;
427}
428
429static void cleanup_queue(struct nullb_queue *nq)
430{
431 kfree(nq->tag_map);
432 kfree(nq->cmds);
433}
434
435static void cleanup_queues(struct nullb *nullb)
436{
437 int i;
438
439 for (i = 0; i < nullb->nr_queues; i++)
440 cleanup_queue(&nullb->queues[i]);
441
442 kfree(nullb->queues);
443}
444
445static int setup_queues(struct nullb *nullb)
446{
2d263a78
MB
447 nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
448 GFP_KERNEL);
f2298c04 449 if (!nullb->queues)
2d263a78 450 return -ENOMEM;
f2298c04
JA
451
452 nullb->nr_queues = 0;
453 nullb->queue_depth = hw_queue_depth;
454
2d263a78
MB
455 return 0;
456}
457
458static int init_driver_queues(struct nullb *nullb)
459{
460 struct nullb_queue *nq;
461 int i, ret = 0;
f2298c04
JA
462
463 for (i = 0; i < submit_queues; i++) {
464 nq = &nullb->queues[i];
2d263a78
MB
465
466 null_init_queue(nullb, nq);
467
468 ret = setup_commands(nq);
469 if (ret)
470 goto err_queue;
f2298c04
JA
471 nullb->nr_queues++;
472 }
473
2d263a78
MB
474 return 0;
475err_queue:
f2298c04 476 cleanup_queues(nullb);
2d263a78 477 return ret;
f2298c04
JA
478}
479
480static int null_add_dev(void)
481{
482 struct gendisk *disk;
483 struct nullb *nullb;
484 sector_t size;
485
486 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
487 if (!nullb)
24d2f903 488 goto out;
f2298c04
JA
489
490 spin_lock_init(&nullb->lock);
491
57053d8c
MB
492 if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
493 submit_queues = nr_online_nodes;
494
f2298c04 495 if (setup_queues(nullb))
24d2f903 496 goto out_free_nullb;
f2298c04
JA
497
498 if (queue_mode == NULL_Q_MQ) {
24d2f903
CH
499 if (use_per_node_hctx)
500 nullb->tag_set.ops = &null_mq_ops_pernode;
501 else
502 nullb->tag_set.ops = &null_mq_ops;
503 nullb->tag_set.nr_hw_queues = submit_queues;
504 nullb->tag_set.queue_depth = hw_queue_depth;
505 nullb->tag_set.numa_node = home_node;
506 nullb->tag_set.cmd_size = sizeof(struct nullb_cmd);
507 nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
508 nullb->tag_set.driver_data = nullb;
509
510 if (blk_mq_alloc_tag_set(&nullb->tag_set))
511 goto out_cleanup_queues;
512
513 nullb->q = blk_mq_init_queue(&nullb->tag_set);
514 if (!nullb->q)
515 goto out_cleanup_tags;
f2298c04
JA
516 } else if (queue_mode == NULL_Q_BIO) {
517 nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
24d2f903
CH
518 if (!nullb->q)
519 goto out_cleanup_queues;
f2298c04 520 blk_queue_make_request(nullb->q, null_queue_bio);
2d263a78 521 init_driver_queues(nullb);
f2298c04
JA
522 } else {
523 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
24d2f903
CH
524 if (!nullb->q)
525 goto out_cleanup_queues;
f2298c04 526 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
24d2f903 527 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
2d263a78 528 init_driver_queues(nullb);
f2298c04
JA
529 }
530
f2298c04
JA
531 nullb->q->queuedata = nullb;
532 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
533
534 disk = nullb->disk = alloc_disk_node(1, home_node);
24d2f903
CH
535 if (!disk)
536 goto out_cleanup_blk_queue;
f2298c04
JA
537
538 mutex_lock(&lock);
539 list_add_tail(&nullb->list, &nullb_list);
540 nullb->index = nullb_indexes++;
541 mutex_unlock(&lock);
542
543 blk_queue_logical_block_size(nullb->q, bs);
544 blk_queue_physical_block_size(nullb->q, bs);
545
546 size = gb * 1024 * 1024 * 1024ULL;
547 sector_div(size, bs);
548 set_capacity(disk, size);
549
550 disk->flags |= GENHD_FL_EXT_DEVT;
551 disk->major = null_major;
552 disk->first_minor = nullb->index;
553 disk->fops = &null_fops;
554 disk->private_data = nullb;
555 disk->queue = nullb->q;
556 sprintf(disk->disk_name, "nullb%d", nullb->index);
557 add_disk(disk);
558 return 0;
24d2f903
CH
559
560out_cleanup_blk_queue:
561 blk_cleanup_queue(nullb->q);
562out_cleanup_tags:
563 if (queue_mode == NULL_Q_MQ)
564 blk_mq_free_tag_set(&nullb->tag_set);
565out_cleanup_queues:
566 cleanup_queues(nullb);
567out_free_nullb:
568 kfree(nullb);
569out:
570 return -ENOMEM;
f2298c04
JA
571}
572
573static int __init null_init(void)
574{
575 unsigned int i;
576
9967d8ac
R
577 if (bs > PAGE_SIZE) {
578 pr_warn("null_blk: invalid block size\n");
579 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
580 bs = PAGE_SIZE;
581 }
f2298c04 582
d15ee6b1 583 if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
fc1bc354 584 if (submit_queues < nr_online_nodes) {
d15ee6b1
MB
585 pr_warn("null_blk: submit_queues param is set to %u.",
586 nr_online_nodes);
fc1bc354
MB
587 submit_queues = nr_online_nodes;
588 }
d15ee6b1 589 } else if (submit_queues > nr_cpu_ids)
f2298c04
JA
590 submit_queues = nr_cpu_ids;
591 else if (!submit_queues)
592 submit_queues = 1;
593
594 mutex_init(&lock);
595
596 /* Initialize a separate list for each CPU for issuing softirqs */
597 for_each_possible_cpu(i) {
598 struct completion_queue *cq = &per_cpu(completion_queues, i);
599
600 init_llist_head(&cq->list);
601
602 if (irqmode != NULL_IRQ_TIMER)
603 continue;
604
605 hrtimer_init(&cq->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
606 cq->timer.function = null_cmd_timer_expired;
607 }
608
609 null_major = register_blkdev(0, "nullb");
610 if (null_major < 0)
611 return null_major;
612
613 for (i = 0; i < nr_devices; i++) {
614 if (null_add_dev()) {
615 unregister_blkdev(null_major, "nullb");
616 return -EINVAL;
617 }
618 }
619
620 pr_info("null: module loaded\n");
621 return 0;
622}
623
624static void __exit null_exit(void)
625{
626 struct nullb *nullb;
627
628 unregister_blkdev(null_major, "nullb");
629
630 mutex_lock(&lock);
631 while (!list_empty(&nullb_list)) {
632 nullb = list_entry(nullb_list.next, struct nullb, list);
633 null_del_dev(nullb);
634 }
635 mutex_unlock(&lock);
636}
637
638module_init(null_init);
639module_exit(null_exit);
640
641MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
642MODULE_LICENSE("GPL");
This page took 0.076096 seconds and 5 git commands to generate.