1 #include <linux/module.h>
3 #include <linux/moduleparam.h>
4 #include <linux/sched.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>
13 struct list_head list;
14 struct llist_node ll_list;
15 struct call_single_data csd;
19 struct nullb_queue *nq;
23 unsigned long *tag_map;
24 wait_queue_head_t wait;
25 unsigned int queue_depth;
27 struct nullb_cmd *cmds;
31 struct list_head list;
33 struct request_queue *q;
36 unsigned int queue_depth;
39 struct nullb_queue *queues;
40 unsigned int nr_queues;
43 static LIST_HEAD(nullb_list);
44 static struct mutex lock;
45 static int null_major;
46 static int nullb_indexes;
48 struct completion_queue {
49 struct llist_head list;
54 * These are per-cpu for now, they will need to be configured by the
55 * complete_queues parameter and appropriately mapped.
57 static DEFINE_PER_CPU(struct completion_queue, completion_queues);
71 static int submit_queues;
72 module_param(submit_queues, int, S_IRUGO);
73 MODULE_PARM_DESC(submit_queues, "Number of submission queues");
75 static int home_node = NUMA_NO_NODE;
76 module_param(home_node, int, S_IRUGO);
77 MODULE_PARM_DESC(home_node, "Home node for the device");
79 static int queue_mode = NULL_Q_MQ;
80 module_param(queue_mode, int, S_IRUGO);
81 MODULE_PARM_DESC(use_mq, "Use blk-mq interface (0=bio,1=rq,2=multiqueue)");
84 module_param(gb, int, S_IRUGO);
85 MODULE_PARM_DESC(gb, "Size in GB");
88 module_param(bs, int, S_IRUGO);
89 MODULE_PARM_DESC(bs, "Block size (in bytes)");
91 static int nr_devices = 2;
92 module_param(nr_devices, int, S_IRUGO);
93 MODULE_PARM_DESC(nr_devices, "Number of devices to register");
95 static int irqmode = NULL_IRQ_SOFTIRQ;
96 module_param(irqmode, int, S_IRUGO);
97 MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
99 static int completion_nsec = 10000;
100 module_param(completion_nsec, int, S_IRUGO);
101 MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
103 static int hw_queue_depth = 64;
104 module_param(hw_queue_depth, int, S_IRUGO);
105 MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
107 static bool use_per_node_hctx = false;
108 module_param(use_per_node_hctx, bool, S_IRUGO);
109 MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
111 static void put_tag(struct nullb_queue *nq, unsigned int tag)
113 clear_bit_unlock(tag, nq->tag_map);
115 if (waitqueue_active(&nq->wait))
119 static unsigned int get_tag(struct nullb_queue *nq)
124 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
125 if (tag >= nq->queue_depth)
127 } while (test_and_set_bit_lock(tag, nq->tag_map));
132 static void free_cmd(struct nullb_cmd *cmd)
134 put_tag(cmd->nq, cmd->tag);
137 static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
139 struct nullb_cmd *cmd;
144 cmd = &nq->cmds[tag];
153 static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
155 struct nullb_cmd *cmd;
158 cmd = __alloc_cmd(nq);
159 if (cmd || !can_wait)
163 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
164 cmd = __alloc_cmd(nq);
171 finish_wait(&nq->wait, &wait);
175 static void end_cmd(struct nullb_cmd *cmd)
177 switch (queue_mode) {
179 blk_mq_end_io(cmd->rq, 0);
182 INIT_LIST_HEAD(&cmd->rq->queuelist);
183 blk_end_request_all(cmd->rq, 0);
186 bio_endio(cmd->bio, 0);
193 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
195 struct completion_queue *cq;
196 struct llist_node *entry;
197 struct nullb_cmd *cmd;
199 cq = &per_cpu(completion_queues, smp_processor_id());
201 while ((entry = llist_del_all(&cq->list)) != NULL) {
202 entry = llist_reverse_order(entry);
204 cmd = container_of(entry, struct nullb_cmd, ll_list);
210 return HRTIMER_NORESTART;
213 static void null_cmd_end_timer(struct nullb_cmd *cmd)
215 struct completion_queue *cq = &per_cpu(completion_queues, get_cpu());
217 cmd->ll_list.next = NULL;
218 if (llist_add(&cmd->ll_list, &cq->list)) {
219 ktime_t kt = ktime_set(0, completion_nsec);
221 hrtimer_start(&cq->timer, kt, HRTIMER_MODE_REL);
227 static void null_softirq_done_fn(struct request *rq)
229 end_cmd(rq->special);
232 static inline void null_handle_cmd(struct nullb_cmd *cmd)
234 /* Complete IO by inline, softirq or timer */
236 case NULL_IRQ_SOFTIRQ:
237 switch (queue_mode) {
239 blk_mq_complete_request(cmd->rq);
242 blk_complete_request(cmd->rq);
246 * XXX: no proper submitting cpu information available.
256 null_cmd_end_timer(cmd);
261 static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
265 if (nullb->nr_queues != 1)
266 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
268 return &nullb->queues[index];
271 static void null_queue_bio(struct request_queue *q, struct bio *bio)
273 struct nullb *nullb = q->queuedata;
274 struct nullb_queue *nq = nullb_to_queue(nullb);
275 struct nullb_cmd *cmd;
277 cmd = alloc_cmd(nq, 1);
280 null_handle_cmd(cmd);
283 static int null_rq_prep_fn(struct request_queue *q, struct request *req)
285 struct nullb *nullb = q->queuedata;
286 struct nullb_queue *nq = nullb_to_queue(nullb);
287 struct nullb_cmd *cmd;
289 cmd = alloc_cmd(nq, 0);
296 return BLKPREP_DEFER;
299 static void null_request_fn(struct request_queue *q)
303 while ((rq = blk_fetch_request(q)) != NULL) {
304 struct nullb_cmd *cmd = rq->special;
306 spin_unlock_irq(q->queue_lock);
307 null_handle_cmd(cmd);
308 spin_lock_irq(q->queue_lock);
312 static int null_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
314 struct nullb_cmd *cmd = rq->special;
317 cmd->nq = hctx->driver_data;
319 null_handle_cmd(cmd);
320 return BLK_MQ_RQ_QUEUE_OK;
323 static struct blk_mq_hw_ctx *null_alloc_hctx(struct blk_mq_reg *reg, unsigned int hctx_index)
325 int b_size = DIV_ROUND_UP(reg->nr_hw_queues, nr_online_nodes);
326 int tip = (reg->nr_hw_queues % nr_online_nodes);
330 * Split submit queues evenly wrt to the number of nodes. If uneven,
331 * fill the first buckets with one extra, until the rest is filled with
334 for (i = 0, n = 1; i < hctx_index; i++, n++) {
335 if (n % b_size == 0) {
341 b_size = reg->nr_hw_queues / nr_online_nodes;
346 * A node might not be online, therefore map the relative node id to the
349 for_each_online_node(n) {
355 return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL, n);
358 static void null_free_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_index)
363 static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
368 init_waitqueue_head(&nq->wait);
369 nq->queue_depth = nullb->queue_depth;
372 static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
375 struct nullb *nullb = data;
376 struct nullb_queue *nq = &nullb->queues[index];
378 hctx->driver_data = nq;
379 null_init_queue(nullb, nq);
385 static struct blk_mq_ops null_mq_ops = {
386 .queue_rq = null_queue_rq,
387 .map_queue = blk_mq_map_queue,
388 .init_hctx = null_init_hctx,
389 .complete = null_softirq_done_fn,
392 static struct blk_mq_reg null_mq_reg = {
395 .cmd_size = sizeof(struct nullb_cmd),
396 .flags = BLK_MQ_F_SHOULD_MERGE,
399 static void null_del_dev(struct nullb *nullb)
401 list_del_init(&nullb->list);
403 del_gendisk(nullb->disk);
404 blk_cleanup_queue(nullb->q);
405 put_disk(nullb->disk);
409 static int null_open(struct block_device *bdev, fmode_t mode)
414 static void null_release(struct gendisk *disk, fmode_t mode)
418 static const struct block_device_operations null_fops = {
419 .owner = THIS_MODULE,
421 .release = null_release,
424 static int setup_commands(struct nullb_queue *nq)
426 struct nullb_cmd *cmd;
429 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
433 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
434 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
440 for (i = 0; i < nq->queue_depth; i++) {
442 INIT_LIST_HEAD(&cmd->list);
443 cmd->ll_list.next = NULL;
450 static void cleanup_queue(struct nullb_queue *nq)
456 static void cleanup_queues(struct nullb *nullb)
460 for (i = 0; i < nullb->nr_queues; i++)
461 cleanup_queue(&nullb->queues[i]);
463 kfree(nullb->queues);
466 static int setup_queues(struct nullb *nullb)
468 nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
473 nullb->nr_queues = 0;
474 nullb->queue_depth = hw_queue_depth;
479 static int init_driver_queues(struct nullb *nullb)
481 struct nullb_queue *nq;
484 for (i = 0; i < submit_queues; i++) {
485 nq = &nullb->queues[i];
487 null_init_queue(nullb, nq);
489 ret = setup_commands(nq);
497 cleanup_queues(nullb);
501 static int null_add_dev(void)
503 struct gendisk *disk;
507 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
511 spin_lock_init(&nullb->lock);
513 if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
514 submit_queues = nr_online_nodes;
516 if (setup_queues(nullb))
519 if (queue_mode == NULL_Q_MQ) {
520 null_mq_reg.numa_node = home_node;
521 null_mq_reg.queue_depth = hw_queue_depth;
522 null_mq_reg.nr_hw_queues = submit_queues;
524 if (use_per_node_hctx) {
525 null_mq_reg.ops->alloc_hctx = null_alloc_hctx;
526 null_mq_reg.ops->free_hctx = null_free_hctx;
528 null_mq_reg.ops->alloc_hctx = blk_mq_alloc_single_hw_queue;
529 null_mq_reg.ops->free_hctx = blk_mq_free_single_hw_queue;
532 nullb->q = blk_mq_init_queue(&null_mq_reg, nullb);
533 } else if (queue_mode == NULL_Q_BIO) {
534 nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
535 blk_queue_make_request(nullb->q, null_queue_bio);
536 init_driver_queues(nullb);
538 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
539 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
541 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
542 init_driver_queues(nullb);
548 nullb->q->queuedata = nullb;
549 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
551 disk = nullb->disk = alloc_disk_node(1, home_node);
554 blk_cleanup_queue(nullb->q);
555 cleanup_queues(nullb);
562 list_add_tail(&nullb->list, &nullb_list);
563 nullb->index = nullb_indexes++;
566 blk_queue_logical_block_size(nullb->q, bs);
567 blk_queue_physical_block_size(nullb->q, bs);
569 size = gb * 1024 * 1024 * 1024ULL;
570 sector_div(size, bs);
571 set_capacity(disk, size);
573 disk->flags |= GENHD_FL_EXT_DEVT;
574 disk->major = null_major;
575 disk->first_minor = nullb->index;
576 disk->fops = &null_fops;
577 disk->private_data = nullb;
578 disk->queue = nullb->q;
579 sprintf(disk->disk_name, "nullb%d", nullb->index);
584 static int __init null_init(void)
588 if (bs > PAGE_SIZE) {
589 pr_warn("null_blk: invalid block size\n");
590 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
594 if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
595 if (submit_queues < nr_online_nodes) {
596 pr_warn("null_blk: submit_queues param is set to %u.",
598 submit_queues = nr_online_nodes;
600 } else if (submit_queues > nr_cpu_ids)
601 submit_queues = nr_cpu_ids;
602 else if (!submit_queues)
607 /* Initialize a separate list for each CPU for issuing softirqs */
608 for_each_possible_cpu(i) {
609 struct completion_queue *cq = &per_cpu(completion_queues, i);
611 init_llist_head(&cq->list);
613 if (irqmode != NULL_IRQ_TIMER)
616 hrtimer_init(&cq->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
617 cq->timer.function = null_cmd_timer_expired;
620 null_major = register_blkdev(0, "nullb");
624 for (i = 0; i < nr_devices; i++) {
625 if (null_add_dev()) {
626 unregister_blkdev(null_major, "nullb");
631 pr_info("null: module loaded\n");
635 static void __exit null_exit(void)
639 unregister_blkdev(null_major, "nullb");
642 while (!list_empty(&nullb_list)) {
643 nullb = list_entry(nullb_list.next, struct nullb, list);
649 module_init(null_init);
650 module_exit(null_exit);
652 MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
653 MODULE_LICENSE("GPL");