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