Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1 | #include <linux/module.h> |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 2 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
| 12 | struct 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 | |
| 22 | struct 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 | |
| 30 | struct nullb { |
| 31 | struct list_head list; |
| 32 | unsigned int index; |
| 33 | struct request_queue *q; |
| 34 | struct gendisk *disk; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 35 | struct blk_mq_tag_set tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
| 44 | static LIST_HEAD(nullb_list); |
| 45 | static struct mutex lock; |
| 46 | static int null_major; |
| 47 | static int nullb_indexes; |
| 48 | |
| 49 | struct 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 | */ |
| 58 | static DEFINE_PER_CPU(struct completion_queue, completion_queues); |
| 59 | |
| 60 | enum { |
| 61 | NULL_IRQ_NONE = 0, |
| 62 | NULL_IRQ_SOFTIRQ = 1, |
| 63 | NULL_IRQ_TIMER = 2, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 64 | }; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 65 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 66 | enum { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 67 | NULL_Q_BIO = 0, |
| 68 | NULL_Q_RQ = 1, |
| 69 | NULL_Q_MQ = 2, |
| 70 | }; |
| 71 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 72 | static int submit_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 73 | module_param(submit_queues, int, S_IRUGO); |
| 74 | MODULE_PARM_DESC(submit_queues, "Number of submission queues"); |
| 75 | |
| 76 | static int home_node = NUMA_NO_NODE; |
| 77 | module_param(home_node, int, S_IRUGO); |
| 78 | MODULE_PARM_DESC(home_node, "Home node for the device"); |
| 79 | |
| 80 | static int queue_mode = NULL_Q_MQ; |
| 81 | module_param(queue_mode, int, S_IRUGO); |
| 82 | MODULE_PARM_DESC(use_mq, "Use blk-mq interface (0=bio,1=rq,2=multiqueue)"); |
| 83 | |
| 84 | static int gb = 250; |
| 85 | module_param(gb, int, S_IRUGO); |
| 86 | MODULE_PARM_DESC(gb, "Size in GB"); |
| 87 | |
| 88 | static int bs = 512; |
| 89 | module_param(bs, int, S_IRUGO); |
| 90 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
| 91 | |
| 92 | static int nr_devices = 2; |
| 93 | module_param(nr_devices, int, S_IRUGO); |
| 94 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); |
| 95 | |
| 96 | static int irqmode = NULL_IRQ_SOFTIRQ; |
| 97 | module_param(irqmode, int, S_IRUGO); |
| 98 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
| 99 | |
| 100 | static int completion_nsec = 10000; |
| 101 | module_param(completion_nsec, int, S_IRUGO); |
| 102 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
| 103 | |
| 104 | static int hw_queue_depth = 64; |
| 105 | module_param(hw_queue_depth, int, S_IRUGO); |
| 106 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
| 107 | |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 108 | static bool use_per_node_hctx = false; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 109 | module_param(use_per_node_hctx, bool, S_IRUGO); |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 110 | MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 111 | |
| 112 | static 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 | |
| 120 | static 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 | |
| 133 | static void free_cmd(struct nullb_cmd *cmd) |
| 134 | { |
| 135 | put_tag(cmd->nq, cmd->tag); |
| 136 | } |
| 137 | |
| 138 | static 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 | |
| 154 | static 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 | |
| 176 | static void end_cmd(struct nullb_cmd *cmd) |
| 177 | { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 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: |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 187 | bio_endio(cmd->bio, 0); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 188 | break; |
| 189 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 190 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 191 | free_cmd(cmd); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static 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) { |
Shlomo Pongratz | d7790b9 | 2014-02-06 18:33:17 +0200 | [diff] [blame] | 203 | entry = llist_reverse_order(entry); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
| 214 | static 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 | |
| 228 | static void null_softirq_done_fn(struct request *rq) |
| 229 | { |
Christoph Hellwig | 9d74e25 | 2014-04-14 10:30:07 +0200 | [diff] [blame] | 230 | end_cmd(blk_mq_rq_to_pdu(rq)); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 233 | static inline void null_handle_cmd(struct nullb_cmd *cmd) |
| 234 | { |
| 235 | /* Complete IO by inline, softirq or timer */ |
| 236 | switch (irqmode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 237 | case NULL_IRQ_SOFTIRQ: |
| 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; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 253 | case NULL_IRQ_NONE: |
| 254 | end_cmd(cmd); |
| 255 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 256 | case NULL_IRQ_TIMER: |
| 257 | null_cmd_end_timer(cmd); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static 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 | |
| 272 | static 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 | |
| 284 | static 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 | |
| 300 | static 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 | |
| 313 | static int null_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq) |
| 314 | { |
Christoph Hellwig | 9d74e25 | 2014-04-14 10:30:07 +0200 | [diff] [blame] | 315 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 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 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 324 | static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq) |
| 325 | { |
| 326 | BUG_ON(!nullb); |
| 327 | BUG_ON(!nq); |
| 328 | |
| 329 | init_waitqueue_head(&nq->wait); |
| 330 | nq->queue_depth = nullb->queue_depth; |
| 331 | } |
| 332 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 333 | static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 334 | unsigned int index) |
| 335 | { |
| 336 | struct nullb *nullb = data; |
| 337 | struct nullb_queue *nq = &nullb->queues[index]; |
| 338 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 339 | hctx->driver_data = nq; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 340 | null_init_queue(nullb, nq); |
| 341 | nullb->nr_queues++; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static struct blk_mq_ops null_mq_ops = { |
| 347 | .queue_rq = null_queue_rq, |
| 348 | .map_queue = blk_mq_map_queue, |
| 349 | .init_hctx = null_init_hctx, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 350 | .complete = null_softirq_done_fn, |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | static void null_del_dev(struct nullb *nullb) |
| 354 | { |
| 355 | list_del_init(&nullb->list); |
| 356 | |
| 357 | del_gendisk(nullb->disk); |
Ming Lei | 518d00b | 2013-12-26 21:31:37 +0800 | [diff] [blame] | 358 | blk_cleanup_queue(nullb->q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 359 | if (queue_mode == NULL_Q_MQ) |
| 360 | blk_mq_free_tag_set(&nullb->tag_set); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 361 | put_disk(nullb->disk); |
| 362 | kfree(nullb); |
| 363 | } |
| 364 | |
| 365 | static int null_open(struct block_device *bdev, fmode_t mode) |
| 366 | { |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static void null_release(struct gendisk *disk, fmode_t mode) |
| 371 | { |
| 372 | } |
| 373 | |
| 374 | static const struct block_device_operations null_fops = { |
| 375 | .owner = THIS_MODULE, |
| 376 | .open = null_open, |
| 377 | .release = null_release, |
| 378 | }; |
| 379 | |
| 380 | static int setup_commands(struct nullb_queue *nq) |
| 381 | { |
| 382 | struct nullb_cmd *cmd; |
| 383 | int i, tag_size; |
| 384 | |
| 385 | nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL); |
| 386 | if (!nq->cmds) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 387 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 388 | |
| 389 | tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG; |
| 390 | nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL); |
| 391 | if (!nq->tag_map) { |
| 392 | kfree(nq->cmds); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 393 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | for (i = 0; i < nq->queue_depth; i++) { |
| 397 | cmd = &nq->cmds[i]; |
| 398 | INIT_LIST_HEAD(&cmd->list); |
| 399 | cmd->ll_list.next = NULL; |
| 400 | cmd->tag = -1U; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static void cleanup_queue(struct nullb_queue *nq) |
| 407 | { |
| 408 | kfree(nq->tag_map); |
| 409 | kfree(nq->cmds); |
| 410 | } |
| 411 | |
| 412 | static void cleanup_queues(struct nullb *nullb) |
| 413 | { |
| 414 | int i; |
| 415 | |
| 416 | for (i = 0; i < nullb->nr_queues; i++) |
| 417 | cleanup_queue(&nullb->queues[i]); |
| 418 | |
| 419 | kfree(nullb->queues); |
| 420 | } |
| 421 | |
| 422 | static int setup_queues(struct nullb *nullb) |
| 423 | { |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 424 | nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue), |
| 425 | GFP_KERNEL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 426 | if (!nullb->queues) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 427 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 428 | |
| 429 | nullb->nr_queues = 0; |
| 430 | nullb->queue_depth = hw_queue_depth; |
| 431 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int init_driver_queues(struct nullb *nullb) |
| 436 | { |
| 437 | struct nullb_queue *nq; |
| 438 | int i, ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 439 | |
| 440 | for (i = 0; i < submit_queues; i++) { |
| 441 | nq = &nullb->queues[i]; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 442 | |
| 443 | null_init_queue(nullb, nq); |
| 444 | |
| 445 | ret = setup_commands(nq); |
| 446 | if (ret) |
| 447 | goto err_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 448 | nullb->nr_queues++; |
| 449 | } |
| 450 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 451 | return 0; |
| 452 | err_queue: |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 453 | cleanup_queues(nullb); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 454 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int null_add_dev(void) |
| 458 | { |
| 459 | struct gendisk *disk; |
| 460 | struct nullb *nullb; |
| 461 | sector_t size; |
| 462 | |
| 463 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node); |
| 464 | if (!nullb) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 465 | goto out; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 466 | |
| 467 | spin_lock_init(&nullb->lock); |
| 468 | |
Matias Bjorling | 57053d8 | 2013-12-10 16:50:38 +0100 | [diff] [blame] | 469 | if (queue_mode == NULL_Q_MQ && use_per_node_hctx) |
| 470 | submit_queues = nr_online_nodes; |
| 471 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 472 | if (setup_queues(nullb)) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 473 | goto out_free_nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 474 | |
| 475 | if (queue_mode == NULL_Q_MQ) { |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame^] | 476 | nullb->tag_set.ops = &null_mq_ops; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 477 | nullb->tag_set.nr_hw_queues = submit_queues; |
| 478 | nullb->tag_set.queue_depth = hw_queue_depth; |
| 479 | nullb->tag_set.numa_node = home_node; |
| 480 | nullb->tag_set.cmd_size = sizeof(struct nullb_cmd); |
| 481 | nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 482 | nullb->tag_set.driver_data = nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 483 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 484 | if (blk_mq_alloc_tag_set(&nullb->tag_set)) |
| 485 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 486 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 487 | nullb->q = blk_mq_init_queue(&nullb->tag_set); |
| 488 | if (!nullb->q) |
| 489 | goto out_cleanup_tags; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 490 | } else if (queue_mode == NULL_Q_BIO) { |
| 491 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 492 | if (!nullb->q) |
| 493 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 494 | blk_queue_make_request(nullb->q, null_queue_bio); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 495 | init_driver_queues(nullb); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 496 | } else { |
| 497 | nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 498 | if (!nullb->q) |
| 499 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 500 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 501 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 502 | init_driver_queues(nullb); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 505 | nullb->q->queuedata = nullb; |
| 506 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); |
| 507 | |
| 508 | disk = nullb->disk = alloc_disk_node(1, home_node); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 509 | if (!disk) |
| 510 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 511 | |
| 512 | mutex_lock(&lock); |
| 513 | list_add_tail(&nullb->list, &nullb_list); |
| 514 | nullb->index = nullb_indexes++; |
| 515 | mutex_unlock(&lock); |
| 516 | |
| 517 | blk_queue_logical_block_size(nullb->q, bs); |
| 518 | blk_queue_physical_block_size(nullb->q, bs); |
| 519 | |
| 520 | size = gb * 1024 * 1024 * 1024ULL; |
| 521 | sector_div(size, bs); |
| 522 | set_capacity(disk, size); |
| 523 | |
| 524 | disk->flags |= GENHD_FL_EXT_DEVT; |
| 525 | disk->major = null_major; |
| 526 | disk->first_minor = nullb->index; |
| 527 | disk->fops = &null_fops; |
| 528 | disk->private_data = nullb; |
| 529 | disk->queue = nullb->q; |
| 530 | sprintf(disk->disk_name, "nullb%d", nullb->index); |
| 531 | add_disk(disk); |
| 532 | return 0; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 533 | |
| 534 | out_cleanup_blk_queue: |
| 535 | blk_cleanup_queue(nullb->q); |
| 536 | out_cleanup_tags: |
| 537 | if (queue_mode == NULL_Q_MQ) |
| 538 | blk_mq_free_tag_set(&nullb->tag_set); |
| 539 | out_cleanup_queues: |
| 540 | cleanup_queues(nullb); |
| 541 | out_free_nullb: |
| 542 | kfree(nullb); |
| 543 | out: |
| 544 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int __init null_init(void) |
| 548 | { |
| 549 | unsigned int i; |
| 550 | |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 551 | if (bs > PAGE_SIZE) { |
| 552 | pr_warn("null_blk: invalid block size\n"); |
| 553 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); |
| 554 | bs = PAGE_SIZE; |
| 555 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 556 | |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 557 | if (queue_mode == NULL_Q_MQ && use_per_node_hctx) { |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 558 | if (submit_queues < nr_online_nodes) { |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 559 | pr_warn("null_blk: submit_queues param is set to %u.", |
| 560 | nr_online_nodes); |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 561 | submit_queues = nr_online_nodes; |
| 562 | } |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 563 | } else if (submit_queues > nr_cpu_ids) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 564 | submit_queues = nr_cpu_ids; |
| 565 | else if (!submit_queues) |
| 566 | submit_queues = 1; |
| 567 | |
| 568 | mutex_init(&lock); |
| 569 | |
| 570 | /* Initialize a separate list for each CPU for issuing softirqs */ |
| 571 | for_each_possible_cpu(i) { |
| 572 | struct completion_queue *cq = &per_cpu(completion_queues, i); |
| 573 | |
| 574 | init_llist_head(&cq->list); |
| 575 | |
| 576 | if (irqmode != NULL_IRQ_TIMER) |
| 577 | continue; |
| 578 | |
| 579 | hrtimer_init(&cq->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 580 | cq->timer.function = null_cmd_timer_expired; |
| 581 | } |
| 582 | |
| 583 | null_major = register_blkdev(0, "nullb"); |
| 584 | if (null_major < 0) |
| 585 | return null_major; |
| 586 | |
| 587 | for (i = 0; i < nr_devices; i++) { |
| 588 | if (null_add_dev()) { |
| 589 | unregister_blkdev(null_major, "nullb"); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | pr_info("null: module loaded\n"); |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static void __exit null_exit(void) |
| 599 | { |
| 600 | struct nullb *nullb; |
| 601 | |
| 602 | unregister_blkdev(null_major, "nullb"); |
| 603 | |
| 604 | mutex_lock(&lock); |
| 605 | while (!list_empty(&nullb_list)) { |
| 606 | nullb = list_entry(nullb_list.next, struct nullb, list); |
| 607 | null_del_dev(nullb); |
| 608 | } |
| 609 | mutex_unlock(&lock); |
| 610 | } |
| 611 | |
| 612 | module_init(null_init); |
| 613 | module_exit(null_exit); |
| 614 | |
| 615 | MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>"); |
| 616 | MODULE_LICENSE("GPL"); |