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