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; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 81 | |
| 82 | static int null_param_store_val(const char *str, int *val, int min, int max) |
| 83 | { |
| 84 | int ret, new_val; |
| 85 | |
| 86 | ret = kstrtoint(str, 10, &new_val); |
| 87 | if (ret) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | if (new_val < min || new_val > max) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | *val = new_val; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int null_set_queue_mode(const char *str, const struct kernel_param *kp) |
| 98 | { |
| 99 | return null_param_store_val(str, &queue_mode, NULL_Q_BIO, NULL_Q_MQ); |
| 100 | } |
| 101 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 102 | static const struct kernel_param_ops null_queue_mode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 103 | .set = null_set_queue_mode, |
| 104 | .get = param_get_int, |
| 105 | }; |
| 106 | |
| 107 | device_param_cb(queue_mode, &null_queue_mode_param_ops, &queue_mode, S_IRUGO); |
Mike Snitzer | 54ae81c | 2014-06-11 17:13:50 -0400 | [diff] [blame] | 108 | MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 109 | |
| 110 | static int gb = 250; |
| 111 | module_param(gb, int, S_IRUGO); |
| 112 | MODULE_PARM_DESC(gb, "Size in GB"); |
| 113 | |
| 114 | static int bs = 512; |
| 115 | module_param(bs, int, S_IRUGO); |
| 116 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
| 117 | |
| 118 | static int nr_devices = 2; |
| 119 | module_param(nr_devices, int, S_IRUGO); |
| 120 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); |
| 121 | |
| 122 | static int irqmode = NULL_IRQ_SOFTIRQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 123 | |
| 124 | static int null_set_irqmode(const char *str, const struct kernel_param *kp) |
| 125 | { |
| 126 | return null_param_store_val(str, &irqmode, NULL_IRQ_NONE, |
| 127 | NULL_IRQ_TIMER); |
| 128 | } |
| 129 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 130 | static const struct kernel_param_ops null_irqmode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 131 | .set = null_set_irqmode, |
| 132 | .get = param_get_int, |
| 133 | }; |
| 134 | |
| 135 | device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 136 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
| 137 | |
| 138 | static int completion_nsec = 10000; |
| 139 | module_param(completion_nsec, int, S_IRUGO); |
| 140 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
| 141 | |
| 142 | static int hw_queue_depth = 64; |
| 143 | module_param(hw_queue_depth, int, S_IRUGO); |
| 144 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
| 145 | |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 146 | static bool use_per_node_hctx = false; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 147 | module_param(use_per_node_hctx, bool, S_IRUGO); |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 148 | 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] | 149 | |
| 150 | static void put_tag(struct nullb_queue *nq, unsigned int tag) |
| 151 | { |
| 152 | clear_bit_unlock(tag, nq->tag_map); |
| 153 | |
| 154 | if (waitqueue_active(&nq->wait)) |
| 155 | wake_up(&nq->wait); |
| 156 | } |
| 157 | |
| 158 | static unsigned int get_tag(struct nullb_queue *nq) |
| 159 | { |
| 160 | unsigned int tag; |
| 161 | |
| 162 | do { |
| 163 | tag = find_first_zero_bit(nq->tag_map, nq->queue_depth); |
| 164 | if (tag >= nq->queue_depth) |
| 165 | return -1U; |
| 166 | } while (test_and_set_bit_lock(tag, nq->tag_map)); |
| 167 | |
| 168 | return tag; |
| 169 | } |
| 170 | |
| 171 | static void free_cmd(struct nullb_cmd *cmd) |
| 172 | { |
| 173 | put_tag(cmd->nq, cmd->tag); |
| 174 | } |
| 175 | |
| 176 | static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq) |
| 177 | { |
| 178 | struct nullb_cmd *cmd; |
| 179 | unsigned int tag; |
| 180 | |
| 181 | tag = get_tag(nq); |
| 182 | if (tag != -1U) { |
| 183 | cmd = &nq->cmds[tag]; |
| 184 | cmd->tag = tag; |
| 185 | cmd->nq = nq; |
| 186 | return cmd; |
| 187 | } |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait) |
| 193 | { |
| 194 | struct nullb_cmd *cmd; |
| 195 | DEFINE_WAIT(wait); |
| 196 | |
| 197 | cmd = __alloc_cmd(nq); |
| 198 | if (cmd || !can_wait) |
| 199 | return cmd; |
| 200 | |
| 201 | do { |
| 202 | prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 203 | cmd = __alloc_cmd(nq); |
| 204 | if (cmd) |
| 205 | break; |
| 206 | |
| 207 | io_schedule(); |
| 208 | } while (1); |
| 209 | |
| 210 | finish_wait(&nq->wait, &wait); |
| 211 | return cmd; |
| 212 | } |
| 213 | |
| 214 | static void end_cmd(struct nullb_cmd *cmd) |
| 215 | { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 216 | switch (queue_mode) { |
| 217 | case NULL_Q_MQ: |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 218 | blk_mq_end_request(cmd->rq, 0); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 219 | return; |
| 220 | case NULL_Q_RQ: |
| 221 | INIT_LIST_HEAD(&cmd->rq->queuelist); |
| 222 | blk_end_request_all(cmd->rq, 0); |
| 223 | break; |
| 224 | case NULL_Q_BIO: |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 225 | bio_endio(cmd->bio); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 226 | break; |
| 227 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 228 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 229 | free_cmd(cmd); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer) |
| 233 | { |
| 234 | struct completion_queue *cq; |
| 235 | struct llist_node *entry; |
| 236 | struct nullb_cmd *cmd; |
| 237 | |
| 238 | cq = &per_cpu(completion_queues, smp_processor_id()); |
| 239 | |
| 240 | while ((entry = llist_del_all(&cq->list)) != NULL) { |
Shlomo Pongratz | d7790b9 | 2014-02-06 18:33:17 +0200 | [diff] [blame] | 241 | entry = llist_reverse_order(entry); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 242 | do { |
Mike Krinkin | 2197406 | 2015-07-19 09:53:17 +0300 | [diff] [blame] | 243 | struct request_queue *q = NULL; |
| 244 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 245 | cmd = container_of(entry, struct nullb_cmd, ll_list); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 246 | entry = entry->next; |
Mike Krinkin | 2197406 | 2015-07-19 09:53:17 +0300 | [diff] [blame] | 247 | if (cmd->rq) |
| 248 | q = cmd->rq->q; |
Ming Lei | fc27691 | 2014-05-01 15:12:36 +0800 | [diff] [blame] | 249 | end_cmd(cmd); |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 250 | |
Mike Krinkin | 2197406 | 2015-07-19 09:53:17 +0300 | [diff] [blame] | 251 | if (q && !q->mq_ops && blk_queue_stopped(q)) { |
| 252 | spin_lock(q->queue_lock); |
| 253 | if (blk_queue_stopped(q)) |
| 254 | blk_start_queue(q); |
| 255 | spin_unlock(q->queue_lock); |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 256 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 257 | } while (entry); |
| 258 | } |
| 259 | |
| 260 | return HRTIMER_NORESTART; |
| 261 | } |
| 262 | |
| 263 | static void null_cmd_end_timer(struct nullb_cmd *cmd) |
| 264 | { |
| 265 | struct completion_queue *cq = &per_cpu(completion_queues, get_cpu()); |
| 266 | |
| 267 | cmd->ll_list.next = NULL; |
| 268 | if (llist_add(&cmd->ll_list, &cq->list)) { |
| 269 | ktime_t kt = ktime_set(0, completion_nsec); |
| 270 | |
Akinobu Mita | 419c21a | 2015-06-02 08:35:09 +0900 | [diff] [blame] | 271 | hrtimer_start(&cq->timer, kt, HRTIMER_MODE_REL_PINNED); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | put_cpu(); |
| 275 | } |
| 276 | |
| 277 | static void null_softirq_done_fn(struct request *rq) |
| 278 | { |
Jens Axboe | d891fa7 | 2014-06-16 11:40:25 -0600 | [diff] [blame] | 279 | if (queue_mode == NULL_Q_MQ) |
| 280 | end_cmd(blk_mq_rq_to_pdu(rq)); |
| 281 | else |
| 282 | end_cmd(rq->special); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 285 | static inline void null_handle_cmd(struct nullb_cmd *cmd) |
| 286 | { |
| 287 | /* Complete IO by inline, softirq or timer */ |
| 288 | switch (irqmode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 289 | case NULL_IRQ_SOFTIRQ: |
| 290 | switch (queue_mode) { |
| 291 | case NULL_Q_MQ: |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 292 | blk_mq_complete_request(cmd->rq, cmd->rq->errors); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 293 | break; |
| 294 | case NULL_Q_RQ: |
| 295 | blk_complete_request(cmd->rq); |
| 296 | break; |
| 297 | case NULL_Q_BIO: |
| 298 | /* |
| 299 | * XXX: no proper submitting cpu information available. |
| 300 | */ |
| 301 | end_cmd(cmd); |
| 302 | break; |
| 303 | } |
| 304 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 305 | case NULL_IRQ_NONE: |
| 306 | end_cmd(cmd); |
| 307 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 308 | case NULL_IRQ_TIMER: |
| 309 | null_cmd_end_timer(cmd); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static struct nullb_queue *nullb_to_queue(struct nullb *nullb) |
| 315 | { |
| 316 | int index = 0; |
| 317 | |
| 318 | if (nullb->nr_queues != 1) |
| 319 | index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues); |
| 320 | |
| 321 | return &nullb->queues[index]; |
| 322 | } |
| 323 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 324 | static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 325 | { |
| 326 | struct nullb *nullb = q->queuedata; |
| 327 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 328 | struct nullb_cmd *cmd; |
| 329 | |
| 330 | cmd = alloc_cmd(nq, 1); |
| 331 | cmd->bio = bio; |
| 332 | |
| 333 | null_handle_cmd(cmd); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 334 | return BLK_QC_T_NONE; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int null_rq_prep_fn(struct request_queue *q, struct request *req) |
| 338 | { |
| 339 | struct nullb *nullb = q->queuedata; |
| 340 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 341 | struct nullb_cmd *cmd; |
| 342 | |
| 343 | cmd = alloc_cmd(nq, 0); |
| 344 | if (cmd) { |
| 345 | cmd->rq = req; |
| 346 | req->special = cmd; |
| 347 | return BLKPREP_OK; |
| 348 | } |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 349 | blk_stop_queue(q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 350 | |
| 351 | return BLKPREP_DEFER; |
| 352 | } |
| 353 | |
| 354 | static void null_request_fn(struct request_queue *q) |
| 355 | { |
| 356 | struct request *rq; |
| 357 | |
| 358 | while ((rq = blk_fetch_request(q)) != NULL) { |
| 359 | struct nullb_cmd *cmd = rq->special; |
| 360 | |
| 361 | spin_unlock_irq(q->queue_lock); |
| 362 | null_handle_cmd(cmd); |
| 363 | spin_lock_irq(q->queue_lock); |
| 364 | } |
| 365 | } |
| 366 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 367 | static int null_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 368 | const struct blk_mq_queue_data *bd) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 369 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 370 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 371 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 372 | cmd->rq = bd->rq; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 373 | cmd->nq = hctx->driver_data; |
| 374 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 375 | blk_mq_start_request(bd->rq); |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 376 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 377 | null_handle_cmd(cmd); |
| 378 | return BLK_MQ_RQ_QUEUE_OK; |
| 379 | } |
| 380 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 381 | static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq) |
| 382 | { |
| 383 | BUG_ON(!nullb); |
| 384 | BUG_ON(!nq); |
| 385 | |
| 386 | init_waitqueue_head(&nq->wait); |
| 387 | nq->queue_depth = nullb->queue_depth; |
| 388 | } |
| 389 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 390 | static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 391 | unsigned int index) |
| 392 | { |
| 393 | struct nullb *nullb = data; |
| 394 | struct nullb_queue *nq = &nullb->queues[index]; |
| 395 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 396 | hctx->driver_data = nq; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 397 | null_init_queue(nullb, nq); |
| 398 | nullb->nr_queues++; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static struct blk_mq_ops null_mq_ops = { |
| 404 | .queue_rq = null_queue_rq, |
| 405 | .map_queue = blk_mq_map_queue, |
| 406 | .init_hctx = null_init_hctx, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 407 | .complete = null_softirq_done_fn, |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 408 | }; |
| 409 | |
Matias Bjørling | de65d2d | 2015-08-31 14:17:18 +0200 | [diff] [blame] | 410 | static void cleanup_queue(struct nullb_queue *nq) |
| 411 | { |
| 412 | kfree(nq->tag_map); |
| 413 | kfree(nq->cmds); |
| 414 | } |
| 415 | |
| 416 | static void cleanup_queues(struct nullb *nullb) |
| 417 | { |
| 418 | int i; |
| 419 | |
| 420 | for (i = 0; i < nullb->nr_queues; i++) |
| 421 | cleanup_queue(&nullb->queues[i]); |
| 422 | |
| 423 | kfree(nullb->queues); |
| 424 | } |
| 425 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 426 | static void null_del_dev(struct nullb *nullb) |
| 427 | { |
| 428 | list_del_init(&nullb->list); |
| 429 | |
| 430 | del_gendisk(nullb->disk); |
Ming Lei | 518d00b | 2013-12-26 21:31:37 +0800 | [diff] [blame] | 431 | blk_cleanup_queue(nullb->q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 432 | if (queue_mode == NULL_Q_MQ) |
| 433 | blk_mq_free_tag_set(&nullb->tag_set); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 434 | put_disk(nullb->disk); |
Matias Bjørling | de65d2d | 2015-08-31 14:17:18 +0200 | [diff] [blame] | 435 | cleanup_queues(nullb); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 436 | kfree(nullb); |
| 437 | } |
| 438 | |
| 439 | static int null_open(struct block_device *bdev, fmode_t mode) |
| 440 | { |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static void null_release(struct gendisk *disk, fmode_t mode) |
| 445 | { |
| 446 | } |
| 447 | |
| 448 | static const struct block_device_operations null_fops = { |
| 449 | .owner = THIS_MODULE, |
| 450 | .open = null_open, |
| 451 | .release = null_release, |
| 452 | }; |
| 453 | |
| 454 | static int setup_commands(struct nullb_queue *nq) |
| 455 | { |
| 456 | struct nullb_cmd *cmd; |
| 457 | int i, tag_size; |
| 458 | |
| 459 | nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL); |
| 460 | if (!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 | tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG; |
| 464 | nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL); |
| 465 | if (!nq->tag_map) { |
| 466 | kfree(nq->cmds); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 467 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | for (i = 0; i < nq->queue_depth; i++) { |
| 471 | cmd = &nq->cmds[i]; |
| 472 | INIT_LIST_HEAD(&cmd->list); |
| 473 | cmd->ll_list.next = NULL; |
| 474 | cmd->tag = -1U; |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 480 | static int setup_queues(struct nullb *nullb) |
| 481 | { |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 482 | nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue), |
| 483 | GFP_KERNEL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 484 | if (!nullb->queues) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 485 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 486 | |
| 487 | nullb->nr_queues = 0; |
| 488 | nullb->queue_depth = hw_queue_depth; |
| 489 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int init_driver_queues(struct nullb *nullb) |
| 494 | { |
| 495 | struct nullb_queue *nq; |
| 496 | int i, ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 497 | |
| 498 | for (i = 0; i < submit_queues; i++) { |
| 499 | nq = &nullb->queues[i]; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 500 | |
| 501 | null_init_queue(nullb, nq); |
| 502 | |
| 503 | ret = setup_commands(nq); |
| 504 | if (ret) |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 505 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 506 | nullb->nr_queues++; |
| 507 | } |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 508 | return 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static int null_add_dev(void) |
| 512 | { |
| 513 | struct gendisk *disk; |
| 514 | struct nullb *nullb; |
| 515 | sector_t size; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 516 | int rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 517 | |
| 518 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 519 | if (!nullb) { |
| 520 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 521 | goto out; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 522 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 523 | |
| 524 | spin_lock_init(&nullb->lock); |
| 525 | |
Matias Bjorling | 57053d8 | 2013-12-10 16:50:38 +0100 | [diff] [blame] | 526 | if (queue_mode == NULL_Q_MQ && use_per_node_hctx) |
| 527 | submit_queues = nr_online_nodes; |
| 528 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 529 | rv = setup_queues(nullb); |
| 530 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 531 | goto out_free_nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 532 | |
| 533 | if (queue_mode == NULL_Q_MQ) { |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 534 | nullb->tag_set.ops = &null_mq_ops; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 535 | nullb->tag_set.nr_hw_queues = submit_queues; |
| 536 | nullb->tag_set.queue_depth = hw_queue_depth; |
| 537 | nullb->tag_set.numa_node = home_node; |
| 538 | nullb->tag_set.cmd_size = sizeof(struct nullb_cmd); |
| 539 | nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 540 | nullb->tag_set.driver_data = nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 541 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 542 | rv = blk_mq_alloc_tag_set(&nullb->tag_set); |
| 543 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 544 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 545 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 546 | nullb->q = blk_mq_init_queue(&nullb->tag_set); |
Ming Lei | 35b489d | 2015-01-02 14:25:27 +0000 | [diff] [blame] | 547 | if (IS_ERR(nullb->q)) { |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 548 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 549 | goto out_cleanup_tags; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 550 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 551 | } else if (queue_mode == NULL_Q_BIO) { |
| 552 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 553 | if (!nullb->q) { |
| 554 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 555 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 556 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 557 | blk_queue_make_request(nullb->q, null_queue_bio); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 558 | rv = init_driver_queues(nullb); |
| 559 | if (rv) |
| 560 | goto out_cleanup_blk_queue; |
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); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 563 | if (!nullb->q) { |
| 564 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 565 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 566 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 567 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 568 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 569 | rv = init_driver_queues(nullb); |
| 570 | if (rv) |
| 571 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 574 | nullb->q->queuedata = nullb; |
| 575 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 576 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 577 | |
| 578 | disk = nullb->disk = alloc_disk_node(1, home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 579 | if (!disk) { |
| 580 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 581 | goto out_cleanup_blk_queue; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 582 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 583 | |
| 584 | mutex_lock(&lock); |
| 585 | list_add_tail(&nullb->list, &nullb_list); |
| 586 | nullb->index = nullb_indexes++; |
| 587 | mutex_unlock(&lock); |
| 588 | |
| 589 | blk_queue_logical_block_size(nullb->q, bs); |
| 590 | blk_queue_physical_block_size(nullb->q, bs); |
| 591 | |
| 592 | size = gb * 1024 * 1024 * 1024ULL; |
Matias Bjørling | 5fdb7e1 | 2015-08-31 14:17:31 +0200 | [diff] [blame] | 593 | set_capacity(disk, size >> 9); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 594 | |
Jens Axboe | 227290b | 2015-01-16 16:02:24 -0700 | [diff] [blame] | 595 | disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 596 | disk->major = null_major; |
| 597 | disk->first_minor = nullb->index; |
| 598 | disk->fops = &null_fops; |
| 599 | disk->private_data = nullb; |
| 600 | disk->queue = nullb->q; |
| 601 | sprintf(disk->disk_name, "nullb%d", nullb->index); |
| 602 | add_disk(disk); |
| 603 | return 0; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 604 | |
| 605 | out_cleanup_blk_queue: |
| 606 | blk_cleanup_queue(nullb->q); |
| 607 | out_cleanup_tags: |
| 608 | if (queue_mode == NULL_Q_MQ) |
| 609 | blk_mq_free_tag_set(&nullb->tag_set); |
| 610 | out_cleanup_queues: |
| 611 | cleanup_queues(nullb); |
| 612 | out_free_nullb: |
| 613 | kfree(nullb); |
| 614 | out: |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 615 | return rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int __init null_init(void) |
| 619 | { |
| 620 | unsigned int i; |
| 621 | |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 622 | if (bs > PAGE_SIZE) { |
| 623 | pr_warn("null_blk: invalid block size\n"); |
| 624 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); |
| 625 | bs = PAGE_SIZE; |
| 626 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 627 | |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 628 | if (queue_mode == NULL_Q_MQ && use_per_node_hctx) { |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 629 | if (submit_queues < nr_online_nodes) { |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 630 | pr_warn("null_blk: submit_queues param is set to %u.", |
| 631 | nr_online_nodes); |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 632 | submit_queues = nr_online_nodes; |
| 633 | } |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 634 | } else if (submit_queues > nr_cpu_ids) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 635 | submit_queues = nr_cpu_ids; |
| 636 | else if (!submit_queues) |
| 637 | submit_queues = 1; |
| 638 | |
| 639 | mutex_init(&lock); |
| 640 | |
| 641 | /* Initialize a separate list for each CPU for issuing softirqs */ |
| 642 | for_each_possible_cpu(i) { |
| 643 | struct completion_queue *cq = &per_cpu(completion_queues, i); |
| 644 | |
| 645 | init_llist_head(&cq->list); |
| 646 | |
| 647 | if (irqmode != NULL_IRQ_TIMER) |
| 648 | continue; |
| 649 | |
| 650 | hrtimer_init(&cq->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 651 | cq->timer.function = null_cmd_timer_expired; |
| 652 | } |
| 653 | |
| 654 | null_major = register_blkdev(0, "nullb"); |
| 655 | if (null_major < 0) |
| 656 | return null_major; |
| 657 | |
| 658 | for (i = 0; i < nr_devices; i++) { |
| 659 | if (null_add_dev()) { |
| 660 | unregister_blkdev(null_major, "nullb"); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | pr_info("null: module loaded\n"); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static void __exit null_exit(void) |
| 670 | { |
| 671 | struct nullb *nullb; |
| 672 | |
| 673 | unregister_blkdev(null_major, "nullb"); |
| 674 | |
| 675 | mutex_lock(&lock); |
| 676 | while (!list_empty(&nullb_list)) { |
| 677 | nullb = list_entry(nullb_list.next, struct nullb, list); |
| 678 | null_del_dev(nullb); |
| 679 | } |
| 680 | mutex_unlock(&lock); |
| 681 | } |
| 682 | |
| 683 | module_init(null_init); |
| 684 | module_exit(null_exit); |
| 685 | |
| 686 | MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>"); |
| 687 | MODULE_LICENSE("GPL"); |