Linux 4.19.133
[linux/fpc-iii.git] / drivers / block / null_blk_main.c
blobd2d7dc9cd58d21fcfb897af6f97a50843fcffc63
1 /*
2 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
3 * Shaohua Li <shli@fb.com>
4 */
5 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/sched.h>
9 #include <linux/fs.h>
10 #include <linux/init.h>
11 #include "null_blk.h"
13 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
14 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
15 #define SECTOR_MASK (PAGE_SECTORS - 1)
17 #define FREE_BATCH 16
19 #define TICKS_PER_SEC 50ULL
20 #define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
22 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
23 static DECLARE_FAULT_ATTR(null_timeout_attr);
24 static DECLARE_FAULT_ATTR(null_requeue_attr);
25 #endif
27 static inline u64 mb_per_tick(int mbps)
29 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
33 * Status flags for nullb_device.
35 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
36 * UP: Device is currently on and visible in userspace.
37 * THROTTLED: Device is being throttled.
38 * CACHE: Device is using a write-back cache.
40 enum nullb_device_flags {
41 NULLB_DEV_FL_CONFIGURED = 0,
42 NULLB_DEV_FL_UP = 1,
43 NULLB_DEV_FL_THROTTLED = 2,
44 NULLB_DEV_FL_CACHE = 3,
47 #define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
49 * nullb_page is a page in memory for nullb devices.
51 * @page: The page holding the data.
52 * @bitmap: The bitmap represents which sector in the page has data.
53 * Each bit represents one block size. For example, sector 8
54 * will use the 7th bit
55 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
56 * page is being flushing to storage. FREE means the cache page is freed and
57 * should be skipped from flushing to storage. Please see
58 * null_make_cache_space
60 struct nullb_page {
61 struct page *page;
62 DECLARE_BITMAP(bitmap, MAP_SZ);
64 #define NULLB_PAGE_LOCK (MAP_SZ - 1)
65 #define NULLB_PAGE_FREE (MAP_SZ - 2)
67 static LIST_HEAD(nullb_list);
68 static struct mutex lock;
69 static int null_major;
70 static DEFINE_IDA(nullb_indexes);
71 static struct blk_mq_tag_set tag_set;
73 enum {
74 NULL_IRQ_NONE = 0,
75 NULL_IRQ_SOFTIRQ = 1,
76 NULL_IRQ_TIMER = 2,
79 enum {
80 NULL_Q_BIO = 0,
81 NULL_Q_RQ = 1,
82 NULL_Q_MQ = 2,
85 static int g_no_sched;
86 module_param_named(no_sched, g_no_sched, int, 0444);
87 MODULE_PARM_DESC(no_sched, "No io scheduler");
89 static int g_submit_queues = 1;
90 module_param_named(submit_queues, g_submit_queues, int, 0444);
91 MODULE_PARM_DESC(submit_queues, "Number of submission queues");
93 static int g_home_node = NUMA_NO_NODE;
94 module_param_named(home_node, g_home_node, int, 0444);
95 MODULE_PARM_DESC(home_node, "Home node for the device");
97 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
98 static char g_timeout_str[80];
99 module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
101 static char g_requeue_str[80];
102 module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
103 #endif
105 static int g_queue_mode = NULL_Q_MQ;
107 static int null_param_store_val(const char *str, int *val, int min, int max)
109 int ret, new_val;
111 ret = kstrtoint(str, 10, &new_val);
112 if (ret)
113 return -EINVAL;
115 if (new_val < min || new_val > max)
116 return -EINVAL;
118 *val = new_val;
119 return 0;
122 static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
124 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
127 static const struct kernel_param_ops null_queue_mode_param_ops = {
128 .set = null_set_queue_mode,
129 .get = param_get_int,
132 device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
133 MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
135 static int g_gb = 250;
136 module_param_named(gb, g_gb, int, 0444);
137 MODULE_PARM_DESC(gb, "Size in GB");
139 static int g_bs = 512;
140 module_param_named(bs, g_bs, int, 0444);
141 MODULE_PARM_DESC(bs, "Block size (in bytes)");
143 static int nr_devices = 1;
144 module_param(nr_devices, int, 0444);
145 MODULE_PARM_DESC(nr_devices, "Number of devices to register");
147 static bool g_blocking;
148 module_param_named(blocking, g_blocking, bool, 0444);
149 MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
151 static bool shared_tags;
152 module_param(shared_tags, bool, 0444);
153 MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
155 static int g_irqmode = NULL_IRQ_SOFTIRQ;
157 static int null_set_irqmode(const char *str, const struct kernel_param *kp)
159 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
160 NULL_IRQ_TIMER);
163 static const struct kernel_param_ops null_irqmode_param_ops = {
164 .set = null_set_irqmode,
165 .get = param_get_int,
168 device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
169 MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
171 static unsigned long g_completion_nsec = 10000;
172 module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
173 MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
175 static int g_hw_queue_depth = 64;
176 module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
177 MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
179 static bool g_use_per_node_hctx;
180 module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
181 MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
183 static bool g_zoned;
184 module_param_named(zoned, g_zoned, bool, S_IRUGO);
185 MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
187 static unsigned long g_zone_size = 256;
188 module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
189 MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
191 static struct nullb_device *null_alloc_dev(void);
192 static void null_free_dev(struct nullb_device *dev);
193 static void null_del_dev(struct nullb *nullb);
194 static int null_add_dev(struct nullb_device *dev);
195 static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
197 static inline struct nullb_device *to_nullb_device(struct config_item *item)
199 return item ? container_of(item, struct nullb_device, item) : NULL;
202 static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
204 return snprintf(page, PAGE_SIZE, "%u\n", val);
207 static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
208 char *page)
210 return snprintf(page, PAGE_SIZE, "%lu\n", val);
213 static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
215 return snprintf(page, PAGE_SIZE, "%u\n", val);
218 static ssize_t nullb_device_uint_attr_store(unsigned int *val,
219 const char *page, size_t count)
221 unsigned int tmp;
222 int result;
224 result = kstrtouint(page, 0, &tmp);
225 if (result)
226 return result;
228 *val = tmp;
229 return count;
232 static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
233 const char *page, size_t count)
235 int result;
236 unsigned long tmp;
238 result = kstrtoul(page, 0, &tmp);
239 if (result)
240 return result;
242 *val = tmp;
243 return count;
246 static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
247 size_t count)
249 bool tmp;
250 int result;
252 result = kstrtobool(page, &tmp);
253 if (result)
254 return result;
256 *val = tmp;
257 return count;
260 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
261 #define NULLB_DEVICE_ATTR(NAME, TYPE) \
262 static ssize_t \
263 nullb_device_##NAME##_show(struct config_item *item, char *page) \
265 return nullb_device_##TYPE##_attr_show( \
266 to_nullb_device(item)->NAME, page); \
268 static ssize_t \
269 nullb_device_##NAME##_store(struct config_item *item, const char *page, \
270 size_t count) \
272 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
273 return -EBUSY; \
274 return nullb_device_##TYPE##_attr_store( \
275 &to_nullb_device(item)->NAME, page, count); \
277 CONFIGFS_ATTR(nullb_device_, NAME);
279 NULLB_DEVICE_ATTR(size, ulong);
280 NULLB_DEVICE_ATTR(completion_nsec, ulong);
281 NULLB_DEVICE_ATTR(submit_queues, uint);
282 NULLB_DEVICE_ATTR(home_node, uint);
283 NULLB_DEVICE_ATTR(queue_mode, uint);
284 NULLB_DEVICE_ATTR(blocksize, uint);
285 NULLB_DEVICE_ATTR(irqmode, uint);
286 NULLB_DEVICE_ATTR(hw_queue_depth, uint);
287 NULLB_DEVICE_ATTR(index, uint);
288 NULLB_DEVICE_ATTR(blocking, bool);
289 NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
290 NULLB_DEVICE_ATTR(memory_backed, bool);
291 NULLB_DEVICE_ATTR(discard, bool);
292 NULLB_DEVICE_ATTR(mbps, uint);
293 NULLB_DEVICE_ATTR(cache_size, ulong);
294 NULLB_DEVICE_ATTR(zoned, bool);
295 NULLB_DEVICE_ATTR(zone_size, ulong);
297 static ssize_t nullb_device_power_show(struct config_item *item, char *page)
299 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
302 static ssize_t nullb_device_power_store(struct config_item *item,
303 const char *page, size_t count)
305 struct nullb_device *dev = to_nullb_device(item);
306 bool newp = false;
307 ssize_t ret;
309 ret = nullb_device_bool_attr_store(&newp, page, count);
310 if (ret < 0)
311 return ret;
313 if (!dev->power && newp) {
314 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
315 return count;
316 if (null_add_dev(dev)) {
317 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
318 return -ENOMEM;
321 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
322 dev->power = newp;
323 } else if (dev->power && !newp) {
324 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
325 mutex_lock(&lock);
326 dev->power = newp;
327 null_del_dev(dev->nullb);
328 mutex_unlock(&lock);
330 clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
333 return count;
336 CONFIGFS_ATTR(nullb_device_, power);
338 static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
340 struct nullb_device *t_dev = to_nullb_device(item);
342 return badblocks_show(&t_dev->badblocks, page, 0);
345 static ssize_t nullb_device_badblocks_store(struct config_item *item,
346 const char *page, size_t count)
348 struct nullb_device *t_dev = to_nullb_device(item);
349 char *orig, *buf, *tmp;
350 u64 start, end;
351 int ret;
353 orig = kstrndup(page, count, GFP_KERNEL);
354 if (!orig)
355 return -ENOMEM;
357 buf = strstrip(orig);
359 ret = -EINVAL;
360 if (buf[0] != '+' && buf[0] != '-')
361 goto out;
362 tmp = strchr(&buf[1], '-');
363 if (!tmp)
364 goto out;
365 *tmp = '\0';
366 ret = kstrtoull(buf + 1, 0, &start);
367 if (ret)
368 goto out;
369 ret = kstrtoull(tmp + 1, 0, &end);
370 if (ret)
371 goto out;
372 ret = -EINVAL;
373 if (start > end)
374 goto out;
375 /* enable badblocks */
376 cmpxchg(&t_dev->badblocks.shift, -1, 0);
377 if (buf[0] == '+')
378 ret = badblocks_set(&t_dev->badblocks, start,
379 end - start + 1, 1);
380 else
381 ret = badblocks_clear(&t_dev->badblocks, start,
382 end - start + 1);
383 if (ret == 0)
384 ret = count;
385 out:
386 kfree(orig);
387 return ret;
389 CONFIGFS_ATTR(nullb_device_, badblocks);
391 static struct configfs_attribute *nullb_device_attrs[] = {
392 &nullb_device_attr_size,
393 &nullb_device_attr_completion_nsec,
394 &nullb_device_attr_submit_queues,
395 &nullb_device_attr_home_node,
396 &nullb_device_attr_queue_mode,
397 &nullb_device_attr_blocksize,
398 &nullb_device_attr_irqmode,
399 &nullb_device_attr_hw_queue_depth,
400 &nullb_device_attr_index,
401 &nullb_device_attr_blocking,
402 &nullb_device_attr_use_per_node_hctx,
403 &nullb_device_attr_power,
404 &nullb_device_attr_memory_backed,
405 &nullb_device_attr_discard,
406 &nullb_device_attr_mbps,
407 &nullb_device_attr_cache_size,
408 &nullb_device_attr_badblocks,
409 &nullb_device_attr_zoned,
410 &nullb_device_attr_zone_size,
411 NULL,
414 static void nullb_device_release(struct config_item *item)
416 struct nullb_device *dev = to_nullb_device(item);
418 null_free_device_storage(dev, false);
419 null_free_dev(dev);
422 static struct configfs_item_operations nullb_device_ops = {
423 .release = nullb_device_release,
426 static const struct config_item_type nullb_device_type = {
427 .ct_item_ops = &nullb_device_ops,
428 .ct_attrs = nullb_device_attrs,
429 .ct_owner = THIS_MODULE,
432 static struct
433 config_item *nullb_group_make_item(struct config_group *group, const char *name)
435 struct nullb_device *dev;
437 dev = null_alloc_dev();
438 if (!dev)
439 return ERR_PTR(-ENOMEM);
441 config_item_init_type_name(&dev->item, name, &nullb_device_type);
443 return &dev->item;
446 static void
447 nullb_group_drop_item(struct config_group *group, struct config_item *item)
449 struct nullb_device *dev = to_nullb_device(item);
451 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
452 mutex_lock(&lock);
453 dev->power = false;
454 null_del_dev(dev->nullb);
455 mutex_unlock(&lock);
458 config_item_put(item);
461 static ssize_t memb_group_features_show(struct config_item *item, char *page)
463 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size\n");
466 CONFIGFS_ATTR_RO(memb_group_, features);
468 static struct configfs_attribute *nullb_group_attrs[] = {
469 &memb_group_attr_features,
470 NULL,
473 static struct configfs_group_operations nullb_group_ops = {
474 .make_item = nullb_group_make_item,
475 .drop_item = nullb_group_drop_item,
478 static const struct config_item_type nullb_group_type = {
479 .ct_group_ops = &nullb_group_ops,
480 .ct_attrs = nullb_group_attrs,
481 .ct_owner = THIS_MODULE,
484 static struct configfs_subsystem nullb_subsys = {
485 .su_group = {
486 .cg_item = {
487 .ci_namebuf = "nullb",
488 .ci_type = &nullb_group_type,
493 static inline int null_cache_active(struct nullb *nullb)
495 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
498 static struct nullb_device *null_alloc_dev(void)
500 struct nullb_device *dev;
502 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
503 if (!dev)
504 return NULL;
505 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
506 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
507 if (badblocks_init(&dev->badblocks, 0)) {
508 kfree(dev);
509 return NULL;
512 dev->size = g_gb * 1024;
513 dev->completion_nsec = g_completion_nsec;
514 dev->submit_queues = g_submit_queues;
515 dev->home_node = g_home_node;
516 dev->queue_mode = g_queue_mode;
517 dev->blocksize = g_bs;
518 dev->irqmode = g_irqmode;
519 dev->hw_queue_depth = g_hw_queue_depth;
520 dev->blocking = g_blocking;
521 dev->use_per_node_hctx = g_use_per_node_hctx;
522 dev->zoned = g_zoned;
523 dev->zone_size = g_zone_size;
524 return dev;
527 static void null_free_dev(struct nullb_device *dev)
529 if (!dev)
530 return;
532 null_zone_exit(dev);
533 badblocks_exit(&dev->badblocks);
534 kfree(dev);
537 static void put_tag(struct nullb_queue *nq, unsigned int tag)
539 clear_bit_unlock(tag, nq->tag_map);
541 if (waitqueue_active(&nq->wait))
542 wake_up(&nq->wait);
545 static unsigned int get_tag(struct nullb_queue *nq)
547 unsigned int tag;
549 do {
550 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
551 if (tag >= nq->queue_depth)
552 return -1U;
553 } while (test_and_set_bit_lock(tag, nq->tag_map));
555 return tag;
558 static void free_cmd(struct nullb_cmd *cmd)
560 put_tag(cmd->nq, cmd->tag);
563 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
565 static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
567 struct nullb_cmd *cmd;
568 unsigned int tag;
570 tag = get_tag(nq);
571 if (tag != -1U) {
572 cmd = &nq->cmds[tag];
573 cmd->tag = tag;
574 cmd->error = BLK_STS_OK;
575 cmd->nq = nq;
576 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
577 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
578 HRTIMER_MODE_REL);
579 cmd->timer.function = null_cmd_timer_expired;
581 return cmd;
584 return NULL;
587 static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
589 struct nullb_cmd *cmd;
590 DEFINE_WAIT(wait);
592 cmd = __alloc_cmd(nq);
593 if (cmd || !can_wait)
594 return cmd;
596 do {
597 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
598 cmd = __alloc_cmd(nq);
599 if (cmd)
600 break;
602 io_schedule();
603 } while (1);
605 finish_wait(&nq->wait, &wait);
606 return cmd;
609 static void end_cmd(struct nullb_cmd *cmd)
611 struct request_queue *q = NULL;
612 int queue_mode = cmd->nq->dev->queue_mode;
614 if (cmd->rq)
615 q = cmd->rq->q;
617 switch (queue_mode) {
618 case NULL_Q_MQ:
619 blk_mq_end_request(cmd->rq, cmd->error);
620 return;
621 case NULL_Q_RQ:
622 INIT_LIST_HEAD(&cmd->rq->queuelist);
623 blk_end_request_all(cmd->rq, cmd->error);
624 break;
625 case NULL_Q_BIO:
626 cmd->bio->bi_status = cmd->error;
627 bio_endio(cmd->bio);
628 break;
631 free_cmd(cmd);
633 /* Restart queue if needed, as we are freeing a tag */
634 if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
635 unsigned long flags;
637 spin_lock_irqsave(q->queue_lock, flags);
638 blk_start_queue_async(q);
639 spin_unlock_irqrestore(q->queue_lock, flags);
643 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
645 end_cmd(container_of(timer, struct nullb_cmd, timer));
647 return HRTIMER_NORESTART;
650 static void null_cmd_end_timer(struct nullb_cmd *cmd)
652 ktime_t kt = cmd->nq->dev->completion_nsec;
654 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
657 static void null_softirq_done_fn(struct request *rq)
659 struct nullb *nullb = rq->q->queuedata;
661 if (nullb->dev->queue_mode == NULL_Q_MQ)
662 end_cmd(blk_mq_rq_to_pdu(rq));
663 else
664 end_cmd(rq->special);
667 static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
669 struct nullb_page *t_page;
671 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
672 if (!t_page)
673 goto out;
675 t_page->page = alloc_pages(gfp_flags, 0);
676 if (!t_page->page)
677 goto out_freepage;
679 memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
680 return t_page;
681 out_freepage:
682 kfree(t_page);
683 out:
684 return NULL;
687 static void null_free_page(struct nullb_page *t_page)
689 __set_bit(NULLB_PAGE_FREE, t_page->bitmap);
690 if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
691 return;
692 __free_page(t_page->page);
693 kfree(t_page);
696 static bool null_page_empty(struct nullb_page *page)
698 int size = MAP_SZ - 2;
700 return find_first_bit(page->bitmap, size) == size;
703 static void null_free_sector(struct nullb *nullb, sector_t sector,
704 bool is_cache)
706 unsigned int sector_bit;
707 u64 idx;
708 struct nullb_page *t_page, *ret;
709 struct radix_tree_root *root;
711 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
712 idx = sector >> PAGE_SECTORS_SHIFT;
713 sector_bit = (sector & SECTOR_MASK);
715 t_page = radix_tree_lookup(root, idx);
716 if (t_page) {
717 __clear_bit(sector_bit, t_page->bitmap);
719 if (null_page_empty(t_page)) {
720 ret = radix_tree_delete_item(root, idx, t_page);
721 WARN_ON(ret != t_page);
722 null_free_page(ret);
723 if (is_cache)
724 nullb->dev->curr_cache -= PAGE_SIZE;
729 static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
730 struct nullb_page *t_page, bool is_cache)
732 struct radix_tree_root *root;
734 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
736 if (radix_tree_insert(root, idx, t_page)) {
737 null_free_page(t_page);
738 t_page = radix_tree_lookup(root, idx);
739 WARN_ON(!t_page || t_page->page->index != idx);
740 } else if (is_cache)
741 nullb->dev->curr_cache += PAGE_SIZE;
743 return t_page;
746 static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
748 unsigned long pos = 0;
749 int nr_pages;
750 struct nullb_page *ret, *t_pages[FREE_BATCH];
751 struct radix_tree_root *root;
753 root = is_cache ? &dev->cache : &dev->data;
755 do {
756 int i;
758 nr_pages = radix_tree_gang_lookup(root,
759 (void **)t_pages, pos, FREE_BATCH);
761 for (i = 0; i < nr_pages; i++) {
762 pos = t_pages[i]->page->index;
763 ret = radix_tree_delete_item(root, pos, t_pages[i]);
764 WARN_ON(ret != t_pages[i]);
765 null_free_page(ret);
768 pos++;
769 } while (nr_pages == FREE_BATCH);
771 if (is_cache)
772 dev->curr_cache = 0;
775 static struct nullb_page *__null_lookup_page(struct nullb *nullb,
776 sector_t sector, bool for_write, bool is_cache)
778 unsigned int sector_bit;
779 u64 idx;
780 struct nullb_page *t_page;
781 struct radix_tree_root *root;
783 idx = sector >> PAGE_SECTORS_SHIFT;
784 sector_bit = (sector & SECTOR_MASK);
786 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
787 t_page = radix_tree_lookup(root, idx);
788 WARN_ON(t_page && t_page->page->index != idx);
790 if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
791 return t_page;
793 return NULL;
796 static struct nullb_page *null_lookup_page(struct nullb *nullb,
797 sector_t sector, bool for_write, bool ignore_cache)
799 struct nullb_page *page = NULL;
801 if (!ignore_cache)
802 page = __null_lookup_page(nullb, sector, for_write, true);
803 if (page)
804 return page;
805 return __null_lookup_page(nullb, sector, for_write, false);
808 static struct nullb_page *null_insert_page(struct nullb *nullb,
809 sector_t sector, bool ignore_cache)
810 __releases(&nullb->lock)
811 __acquires(&nullb->lock)
813 u64 idx;
814 struct nullb_page *t_page;
816 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
817 if (t_page)
818 return t_page;
820 spin_unlock_irq(&nullb->lock);
822 t_page = null_alloc_page(GFP_NOIO);
823 if (!t_page)
824 goto out_lock;
826 if (radix_tree_preload(GFP_NOIO))
827 goto out_freepage;
829 spin_lock_irq(&nullb->lock);
830 idx = sector >> PAGE_SECTORS_SHIFT;
831 t_page->page->index = idx;
832 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
833 radix_tree_preload_end();
835 return t_page;
836 out_freepage:
837 null_free_page(t_page);
838 out_lock:
839 spin_lock_irq(&nullb->lock);
840 return null_lookup_page(nullb, sector, true, ignore_cache);
843 static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
845 int i;
846 unsigned int offset;
847 u64 idx;
848 struct nullb_page *t_page, *ret;
849 void *dst, *src;
851 idx = c_page->page->index;
853 t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
855 __clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
856 if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
857 null_free_page(c_page);
858 if (t_page && null_page_empty(t_page)) {
859 ret = radix_tree_delete_item(&nullb->dev->data,
860 idx, t_page);
861 null_free_page(t_page);
863 return 0;
866 if (!t_page)
867 return -ENOMEM;
869 src = kmap_atomic(c_page->page);
870 dst = kmap_atomic(t_page->page);
872 for (i = 0; i < PAGE_SECTORS;
873 i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
874 if (test_bit(i, c_page->bitmap)) {
875 offset = (i << SECTOR_SHIFT);
876 memcpy(dst + offset, src + offset,
877 nullb->dev->blocksize);
878 __set_bit(i, t_page->bitmap);
882 kunmap_atomic(dst);
883 kunmap_atomic(src);
885 ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
886 null_free_page(ret);
887 nullb->dev->curr_cache -= PAGE_SIZE;
889 return 0;
892 static int null_make_cache_space(struct nullb *nullb, unsigned long n)
894 int i, err, nr_pages;
895 struct nullb_page *c_pages[FREE_BATCH];
896 unsigned long flushed = 0, one_round;
898 again:
899 if ((nullb->dev->cache_size * 1024 * 1024) >
900 nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
901 return 0;
903 nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
904 (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
906 * nullb_flush_cache_page could unlock before using the c_pages. To
907 * avoid race, we don't allow page free
909 for (i = 0; i < nr_pages; i++) {
910 nullb->cache_flush_pos = c_pages[i]->page->index;
912 * We found the page which is being flushed to disk by other
913 * threads
915 if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
916 c_pages[i] = NULL;
917 else
918 __set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
921 one_round = 0;
922 for (i = 0; i < nr_pages; i++) {
923 if (c_pages[i] == NULL)
924 continue;
925 err = null_flush_cache_page(nullb, c_pages[i]);
926 if (err)
927 return err;
928 one_round++;
930 flushed += one_round << PAGE_SHIFT;
932 if (n > flushed) {
933 if (nr_pages == 0)
934 nullb->cache_flush_pos = 0;
935 if (one_round == 0) {
936 /* give other threads a chance */
937 spin_unlock_irq(&nullb->lock);
938 spin_lock_irq(&nullb->lock);
940 goto again;
942 return 0;
945 static int copy_to_nullb(struct nullb *nullb, struct page *source,
946 unsigned int off, sector_t sector, size_t n, bool is_fua)
948 size_t temp, count = 0;
949 unsigned int offset;
950 struct nullb_page *t_page;
951 void *dst, *src;
953 while (count < n) {
954 temp = min_t(size_t, nullb->dev->blocksize, n - count);
956 if (null_cache_active(nullb) && !is_fua)
957 null_make_cache_space(nullb, PAGE_SIZE);
959 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
960 t_page = null_insert_page(nullb, sector,
961 !null_cache_active(nullb) || is_fua);
962 if (!t_page)
963 return -ENOSPC;
965 src = kmap_atomic(source);
966 dst = kmap_atomic(t_page->page);
967 memcpy(dst + offset, src + off + count, temp);
968 kunmap_atomic(dst);
969 kunmap_atomic(src);
971 __set_bit(sector & SECTOR_MASK, t_page->bitmap);
973 if (is_fua)
974 null_free_sector(nullb, sector, true);
976 count += temp;
977 sector += temp >> SECTOR_SHIFT;
979 return 0;
982 static int copy_from_nullb(struct nullb *nullb, struct page *dest,
983 unsigned int off, sector_t sector, size_t n)
985 size_t temp, count = 0;
986 unsigned int offset;
987 struct nullb_page *t_page;
988 void *dst, *src;
990 while (count < n) {
991 temp = min_t(size_t, nullb->dev->blocksize, n - count);
993 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
994 t_page = null_lookup_page(nullb, sector, false,
995 !null_cache_active(nullb));
997 dst = kmap_atomic(dest);
998 if (!t_page) {
999 memset(dst + off + count, 0, temp);
1000 goto next;
1002 src = kmap_atomic(t_page->page);
1003 memcpy(dst + off + count, src + offset, temp);
1004 kunmap_atomic(src);
1005 next:
1006 kunmap_atomic(dst);
1008 count += temp;
1009 sector += temp >> SECTOR_SHIFT;
1011 return 0;
1014 static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
1016 size_t temp;
1018 spin_lock_irq(&nullb->lock);
1019 while (n > 0) {
1020 temp = min_t(size_t, n, nullb->dev->blocksize);
1021 null_free_sector(nullb, sector, false);
1022 if (null_cache_active(nullb))
1023 null_free_sector(nullb, sector, true);
1024 sector += temp >> SECTOR_SHIFT;
1025 n -= temp;
1027 spin_unlock_irq(&nullb->lock);
1030 static int null_handle_flush(struct nullb *nullb)
1032 int err;
1034 if (!null_cache_active(nullb))
1035 return 0;
1037 spin_lock_irq(&nullb->lock);
1038 while (true) {
1039 err = null_make_cache_space(nullb,
1040 nullb->dev->cache_size * 1024 * 1024);
1041 if (err || nullb->dev->curr_cache == 0)
1042 break;
1045 WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1046 spin_unlock_irq(&nullb->lock);
1047 return err;
1050 static int null_transfer(struct nullb *nullb, struct page *page,
1051 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1052 bool is_fua)
1054 int err = 0;
1056 if (!is_write) {
1057 err = copy_from_nullb(nullb, page, off, sector, len);
1058 flush_dcache_page(page);
1059 } else {
1060 flush_dcache_page(page);
1061 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
1064 return err;
1067 static int null_handle_rq(struct nullb_cmd *cmd)
1069 struct request *rq = cmd->rq;
1070 struct nullb *nullb = cmd->nq->dev->nullb;
1071 int err;
1072 unsigned int len;
1073 sector_t sector;
1074 struct req_iterator iter;
1075 struct bio_vec bvec;
1077 sector = blk_rq_pos(rq);
1079 if (req_op(rq) == REQ_OP_DISCARD) {
1080 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1081 return 0;
1084 spin_lock_irq(&nullb->lock);
1085 rq_for_each_segment(bvec, rq, iter) {
1086 len = bvec.bv_len;
1087 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
1088 op_is_write(req_op(rq)), sector,
1089 req_op(rq) & REQ_FUA);
1090 if (err) {
1091 spin_unlock_irq(&nullb->lock);
1092 return err;
1094 sector += len >> SECTOR_SHIFT;
1096 spin_unlock_irq(&nullb->lock);
1098 return 0;
1101 static int null_handle_bio(struct nullb_cmd *cmd)
1103 struct bio *bio = cmd->bio;
1104 struct nullb *nullb = cmd->nq->dev->nullb;
1105 int err;
1106 unsigned int len;
1107 sector_t sector;
1108 struct bio_vec bvec;
1109 struct bvec_iter iter;
1111 sector = bio->bi_iter.bi_sector;
1113 if (bio_op(bio) == REQ_OP_DISCARD) {
1114 null_handle_discard(nullb, sector,
1115 bio_sectors(bio) << SECTOR_SHIFT);
1116 return 0;
1119 spin_lock_irq(&nullb->lock);
1120 bio_for_each_segment(bvec, bio, iter) {
1121 len = bvec.bv_len;
1122 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
1123 op_is_write(bio_op(bio)), sector,
1124 bio_op(bio) & REQ_FUA);
1125 if (err) {
1126 spin_unlock_irq(&nullb->lock);
1127 return err;
1129 sector += len >> SECTOR_SHIFT;
1131 spin_unlock_irq(&nullb->lock);
1132 return 0;
1135 static void null_stop_queue(struct nullb *nullb)
1137 struct request_queue *q = nullb->q;
1139 if (nullb->dev->queue_mode == NULL_Q_MQ)
1140 blk_mq_stop_hw_queues(q);
1141 else {
1142 spin_lock_irq(q->queue_lock);
1143 blk_stop_queue(q);
1144 spin_unlock_irq(q->queue_lock);
1148 static void null_restart_queue_async(struct nullb *nullb)
1150 struct request_queue *q = nullb->q;
1151 unsigned long flags;
1153 if (nullb->dev->queue_mode == NULL_Q_MQ)
1154 blk_mq_start_stopped_hw_queues(q, true);
1155 else {
1156 spin_lock_irqsave(q->queue_lock, flags);
1157 blk_start_queue_async(q);
1158 spin_unlock_irqrestore(q->queue_lock, flags);
1162 static bool cmd_report_zone(struct nullb *nullb, struct nullb_cmd *cmd)
1164 struct nullb_device *dev = cmd->nq->dev;
1166 if (dev->queue_mode == NULL_Q_BIO) {
1167 if (bio_op(cmd->bio) == REQ_OP_ZONE_REPORT) {
1168 cmd->error = null_zone_report(nullb, cmd->bio);
1169 return true;
1171 } else {
1172 if (req_op(cmd->rq) == REQ_OP_ZONE_REPORT) {
1173 cmd->error = null_zone_report(nullb, cmd->rq->bio);
1174 return true;
1178 return false;
1181 static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
1183 struct nullb_device *dev = cmd->nq->dev;
1184 struct nullb *nullb = dev->nullb;
1185 int err = 0;
1187 if (cmd_report_zone(nullb, cmd))
1188 goto out;
1190 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1191 struct request *rq = cmd->rq;
1193 if (!hrtimer_active(&nullb->bw_timer))
1194 hrtimer_restart(&nullb->bw_timer);
1196 if (atomic_long_sub_return(blk_rq_bytes(rq),
1197 &nullb->cur_bytes) < 0) {
1198 null_stop_queue(nullb);
1199 /* race with timer */
1200 if (atomic_long_read(&nullb->cur_bytes) > 0)
1201 null_restart_queue_async(nullb);
1202 if (dev->queue_mode == NULL_Q_RQ) {
1203 struct request_queue *q = nullb->q;
1205 spin_lock_irq(q->queue_lock);
1206 rq->rq_flags |= RQF_DONTPREP;
1207 blk_requeue_request(q, rq);
1208 spin_unlock_irq(q->queue_lock);
1209 return BLK_STS_OK;
1210 } else
1211 /* requeue request */
1212 return BLK_STS_DEV_RESOURCE;
1216 if (nullb->dev->badblocks.shift != -1) {
1217 int bad_sectors;
1218 sector_t sector, size, first_bad;
1219 bool is_flush = true;
1221 if (dev->queue_mode == NULL_Q_BIO &&
1222 bio_op(cmd->bio) != REQ_OP_FLUSH) {
1223 is_flush = false;
1224 sector = cmd->bio->bi_iter.bi_sector;
1225 size = bio_sectors(cmd->bio);
1227 if (dev->queue_mode != NULL_Q_BIO &&
1228 req_op(cmd->rq) != REQ_OP_FLUSH) {
1229 is_flush = false;
1230 sector = blk_rq_pos(cmd->rq);
1231 size = blk_rq_sectors(cmd->rq);
1233 if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector,
1234 size, &first_bad, &bad_sectors)) {
1235 cmd->error = BLK_STS_IOERR;
1236 goto out;
1240 if (dev->memory_backed) {
1241 if (dev->queue_mode == NULL_Q_BIO) {
1242 if (bio_op(cmd->bio) == REQ_OP_FLUSH)
1243 err = null_handle_flush(nullb);
1244 else
1245 err = null_handle_bio(cmd);
1246 } else {
1247 if (req_op(cmd->rq) == REQ_OP_FLUSH)
1248 err = null_handle_flush(nullb);
1249 else
1250 err = null_handle_rq(cmd);
1253 cmd->error = errno_to_blk_status(err);
1255 if (!cmd->error && dev->zoned) {
1256 sector_t sector;
1257 unsigned int nr_sectors;
1258 int op;
1260 if (dev->queue_mode == NULL_Q_BIO) {
1261 op = bio_op(cmd->bio);
1262 sector = cmd->bio->bi_iter.bi_sector;
1263 nr_sectors = cmd->bio->bi_iter.bi_size >> 9;
1264 } else {
1265 op = req_op(cmd->rq);
1266 sector = blk_rq_pos(cmd->rq);
1267 nr_sectors = blk_rq_sectors(cmd->rq);
1270 if (op == REQ_OP_WRITE)
1271 null_zone_write(cmd, sector, nr_sectors);
1272 else if (op == REQ_OP_ZONE_RESET)
1273 null_zone_reset(cmd, sector);
1275 out:
1276 /* Complete IO by inline, softirq or timer */
1277 switch (dev->irqmode) {
1278 case NULL_IRQ_SOFTIRQ:
1279 switch (dev->queue_mode) {
1280 case NULL_Q_MQ:
1281 blk_mq_complete_request(cmd->rq);
1282 break;
1283 case NULL_Q_RQ:
1284 blk_complete_request(cmd->rq);
1285 break;
1286 case NULL_Q_BIO:
1288 * XXX: no proper submitting cpu information available.
1290 end_cmd(cmd);
1291 break;
1293 break;
1294 case NULL_IRQ_NONE:
1295 end_cmd(cmd);
1296 break;
1297 case NULL_IRQ_TIMER:
1298 null_cmd_end_timer(cmd);
1299 break;
1301 return BLK_STS_OK;
1304 static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1306 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1307 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1308 unsigned int mbps = nullb->dev->mbps;
1310 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1311 return HRTIMER_NORESTART;
1313 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1314 null_restart_queue_async(nullb);
1316 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1318 return HRTIMER_RESTART;
1321 static void nullb_setup_bwtimer(struct nullb *nullb)
1323 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1325 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1326 nullb->bw_timer.function = nullb_bwtimer_fn;
1327 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1328 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
1331 static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1333 int index = 0;
1335 if (nullb->nr_queues != 1)
1336 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1338 return &nullb->queues[index];
1341 static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
1343 struct nullb *nullb = q->queuedata;
1344 struct nullb_queue *nq = nullb_to_queue(nullb);
1345 struct nullb_cmd *cmd;
1347 cmd = alloc_cmd(nq, 1);
1348 cmd->bio = bio;
1350 null_handle_cmd(cmd);
1351 return BLK_QC_T_NONE;
1354 static enum blk_eh_timer_return null_rq_timed_out_fn(struct request *rq)
1356 pr_info("null: rq %p timed out\n", rq);
1357 __blk_complete_request(rq);
1358 return BLK_EH_DONE;
1361 static int null_rq_prep_fn(struct request_queue *q, struct request *req)
1363 struct nullb *nullb = q->queuedata;
1364 struct nullb_queue *nq = nullb_to_queue(nullb);
1365 struct nullb_cmd *cmd;
1367 cmd = alloc_cmd(nq, 0);
1368 if (cmd) {
1369 cmd->rq = req;
1370 req->special = cmd;
1371 return BLKPREP_OK;
1373 blk_stop_queue(q);
1375 return BLKPREP_DEFER;
1378 static bool should_timeout_request(struct request *rq)
1380 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1381 if (g_timeout_str[0])
1382 return should_fail(&null_timeout_attr, 1);
1383 #endif
1384 return false;
1387 static bool should_requeue_request(struct request *rq)
1389 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1390 if (g_requeue_str[0])
1391 return should_fail(&null_requeue_attr, 1);
1392 #endif
1393 return false;
1396 static void null_request_fn(struct request_queue *q)
1398 struct request *rq;
1400 while ((rq = blk_fetch_request(q)) != NULL) {
1401 struct nullb_cmd *cmd = rq->special;
1403 /* just ignore the request */
1404 if (should_timeout_request(rq))
1405 continue;
1406 if (should_requeue_request(rq)) {
1407 blk_requeue_request(q, rq);
1408 continue;
1411 spin_unlock_irq(q->queue_lock);
1412 null_handle_cmd(cmd);
1413 spin_lock_irq(q->queue_lock);
1417 static enum blk_eh_timer_return null_timeout_rq(struct request *rq, bool res)
1419 pr_info("null: rq %p timed out\n", rq);
1420 blk_mq_complete_request(rq);
1421 return BLK_EH_DONE;
1424 static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
1425 const struct blk_mq_queue_data *bd)
1427 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1428 struct nullb_queue *nq = hctx->driver_data;
1430 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1432 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
1433 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1434 cmd->timer.function = null_cmd_timer_expired;
1436 cmd->rq = bd->rq;
1437 cmd->error = BLK_STS_OK;
1438 cmd->nq = nq;
1440 blk_mq_start_request(bd->rq);
1442 if (should_requeue_request(bd->rq)) {
1444 * Alternate between hitting the core BUSY path, and the
1445 * driver driven requeue path
1447 nq->requeue_selection++;
1448 if (nq->requeue_selection & 1)
1449 return BLK_STS_RESOURCE;
1450 else {
1451 blk_mq_requeue_request(bd->rq, true);
1452 return BLK_STS_OK;
1455 if (should_timeout_request(bd->rq))
1456 return BLK_STS_OK;
1458 return null_handle_cmd(cmd);
1461 static const struct blk_mq_ops null_mq_ops = {
1462 .queue_rq = null_queue_rq,
1463 .complete = null_softirq_done_fn,
1464 .timeout = null_timeout_rq,
1467 static void cleanup_queue(struct nullb_queue *nq)
1469 kfree(nq->tag_map);
1470 kfree(nq->cmds);
1473 static void cleanup_queues(struct nullb *nullb)
1475 int i;
1477 for (i = 0; i < nullb->nr_queues; i++)
1478 cleanup_queue(&nullb->queues[i]);
1480 kfree(nullb->queues);
1483 static void null_del_dev(struct nullb *nullb)
1485 struct nullb_device *dev;
1487 if (!nullb)
1488 return;
1490 dev = nullb->dev;
1492 ida_simple_remove(&nullb_indexes, nullb->index);
1494 list_del_init(&nullb->list);
1496 del_gendisk(nullb->disk);
1498 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1499 hrtimer_cancel(&nullb->bw_timer);
1500 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1501 null_restart_queue_async(nullb);
1504 blk_cleanup_queue(nullb->q);
1505 if (dev->queue_mode == NULL_Q_MQ &&
1506 nullb->tag_set == &nullb->__tag_set)
1507 blk_mq_free_tag_set(nullb->tag_set);
1508 put_disk(nullb->disk);
1509 cleanup_queues(nullb);
1510 if (null_cache_active(nullb))
1511 null_free_device_storage(nullb->dev, true);
1512 kfree(nullb);
1513 dev->nullb = NULL;
1516 static void null_config_discard(struct nullb *nullb)
1518 if (nullb->dev->discard == false)
1519 return;
1520 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1521 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1522 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
1523 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nullb->q);
1526 static int null_open(struct block_device *bdev, fmode_t mode)
1528 return 0;
1531 static void null_release(struct gendisk *disk, fmode_t mode)
1535 static const struct block_device_operations null_fops = {
1536 .owner = THIS_MODULE,
1537 .open = null_open,
1538 .release = null_release,
1541 static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1543 BUG_ON(!nullb);
1544 BUG_ON(!nq);
1546 init_waitqueue_head(&nq->wait);
1547 nq->queue_depth = nullb->queue_depth;
1548 nq->dev = nullb->dev;
1551 static void null_init_queues(struct nullb *nullb)
1553 struct request_queue *q = nullb->q;
1554 struct blk_mq_hw_ctx *hctx;
1555 struct nullb_queue *nq;
1556 int i;
1558 queue_for_each_hw_ctx(q, hctx, i) {
1559 if (!hctx->nr_ctx || !hctx->tags)
1560 continue;
1561 nq = &nullb->queues[i];
1562 hctx->driver_data = nq;
1563 null_init_queue(nullb, nq);
1564 nullb->nr_queues++;
1568 static int setup_commands(struct nullb_queue *nq)
1570 struct nullb_cmd *cmd;
1571 int i, tag_size;
1573 nq->cmds = kcalloc(nq->queue_depth, sizeof(*cmd), GFP_KERNEL);
1574 if (!nq->cmds)
1575 return -ENOMEM;
1577 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
1578 nq->tag_map = kcalloc(tag_size, sizeof(unsigned long), GFP_KERNEL);
1579 if (!nq->tag_map) {
1580 kfree(nq->cmds);
1581 return -ENOMEM;
1584 for (i = 0; i < nq->queue_depth; i++) {
1585 cmd = &nq->cmds[i];
1586 INIT_LIST_HEAD(&cmd->list);
1587 cmd->ll_list.next = NULL;
1588 cmd->tag = -1U;
1591 return 0;
1594 static int setup_queues(struct nullb *nullb)
1596 nullb->queues = kcalloc(nullb->dev->submit_queues,
1597 sizeof(struct nullb_queue),
1598 GFP_KERNEL);
1599 if (!nullb->queues)
1600 return -ENOMEM;
1602 nullb->nr_queues = 0;
1603 nullb->queue_depth = nullb->dev->hw_queue_depth;
1605 return 0;
1608 static int init_driver_queues(struct nullb *nullb)
1610 struct nullb_queue *nq;
1611 int i, ret = 0;
1613 for (i = 0; i < nullb->dev->submit_queues; i++) {
1614 nq = &nullb->queues[i];
1616 null_init_queue(nullb, nq);
1618 ret = setup_commands(nq);
1619 if (ret)
1620 return ret;
1621 nullb->nr_queues++;
1623 return 0;
1626 static int null_gendisk_register(struct nullb *nullb)
1628 struct gendisk *disk;
1629 sector_t size;
1631 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
1632 if (!disk)
1633 return -ENOMEM;
1634 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
1635 set_capacity(disk, size >> 9);
1637 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1638 disk->major = null_major;
1639 disk->first_minor = nullb->index;
1640 disk->fops = &null_fops;
1641 disk->private_data = nullb;
1642 disk->queue = nullb->q;
1643 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1645 add_disk(disk);
1646 return 0;
1649 static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
1651 set->ops = &null_mq_ops;
1652 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1653 g_submit_queues;
1654 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1655 g_hw_queue_depth;
1656 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
1657 set->cmd_size = sizeof(struct nullb_cmd);
1658 set->flags = BLK_MQ_F_SHOULD_MERGE;
1659 if (g_no_sched)
1660 set->flags |= BLK_MQ_F_NO_SCHED;
1661 set->driver_data = NULL;
1663 if ((nullb && nullb->dev->blocking) || g_blocking)
1664 set->flags |= BLK_MQ_F_BLOCKING;
1666 return blk_mq_alloc_tag_set(set);
1669 static void null_validate_conf(struct nullb_device *dev)
1671 dev->blocksize = round_down(dev->blocksize, 512);
1672 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
1674 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1675 if (dev->submit_queues != nr_online_nodes)
1676 dev->submit_queues = nr_online_nodes;
1677 } else if (dev->submit_queues > nr_cpu_ids)
1678 dev->submit_queues = nr_cpu_ids;
1679 else if (dev->submit_queues == 0)
1680 dev->submit_queues = 1;
1682 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1683 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
1685 /* Do memory allocation, so set blocking */
1686 if (dev->memory_backed)
1687 dev->blocking = true;
1688 else /* cache is meaningless */
1689 dev->cache_size = 0;
1690 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1691 dev->cache_size);
1692 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1693 /* can not stop a queue */
1694 if (dev->queue_mode == NULL_Q_BIO)
1695 dev->mbps = 0;
1698 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1699 static bool __null_setup_fault(struct fault_attr *attr, char *str)
1701 if (!str[0])
1702 return true;
1704 if (!setup_fault_attr(attr, str))
1705 return false;
1707 attr->verbose = 0;
1708 return true;
1710 #endif
1712 static bool null_setup_fault(void)
1714 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1715 if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
1716 return false;
1717 if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
1718 return false;
1719 #endif
1720 return true;
1723 static int null_add_dev(struct nullb_device *dev)
1725 struct nullb *nullb;
1726 int rv;
1728 null_validate_conf(dev);
1730 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
1731 if (!nullb) {
1732 rv = -ENOMEM;
1733 goto out;
1735 nullb->dev = dev;
1736 dev->nullb = nullb;
1738 spin_lock_init(&nullb->lock);
1740 rv = setup_queues(nullb);
1741 if (rv)
1742 goto out_free_nullb;
1744 if (dev->queue_mode == NULL_Q_MQ) {
1745 if (shared_tags) {
1746 nullb->tag_set = &tag_set;
1747 rv = 0;
1748 } else {
1749 nullb->tag_set = &nullb->__tag_set;
1750 rv = null_init_tag_set(nullb, nullb->tag_set);
1753 if (rv)
1754 goto out_cleanup_queues;
1756 if (!null_setup_fault())
1757 goto out_cleanup_queues;
1759 nullb->tag_set->timeout = 5 * HZ;
1760 nullb->q = blk_mq_init_queue(nullb->tag_set);
1761 if (IS_ERR(nullb->q)) {
1762 rv = -ENOMEM;
1763 goto out_cleanup_tags;
1765 null_init_queues(nullb);
1766 } else if (dev->queue_mode == NULL_Q_BIO) {
1767 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node,
1768 NULL);
1769 if (!nullb->q) {
1770 rv = -ENOMEM;
1771 goto out_cleanup_queues;
1773 blk_queue_make_request(nullb->q, null_queue_bio);
1774 rv = init_driver_queues(nullb);
1775 if (rv)
1776 goto out_cleanup_blk_queue;
1777 } else {
1778 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock,
1779 dev->home_node);
1780 if (!nullb->q) {
1781 rv = -ENOMEM;
1782 goto out_cleanup_queues;
1785 if (!null_setup_fault())
1786 goto out_cleanup_blk_queue;
1788 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
1789 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
1790 blk_queue_rq_timed_out(nullb->q, null_rq_timed_out_fn);
1791 nullb->q->rq_timeout = 5 * HZ;
1792 rv = init_driver_queues(nullb);
1793 if (rv)
1794 goto out_cleanup_blk_queue;
1797 if (dev->mbps) {
1798 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1799 nullb_setup_bwtimer(nullb);
1802 if (dev->cache_size > 0) {
1803 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1804 blk_queue_write_cache(nullb->q, true, true);
1805 blk_queue_flush_queueable(nullb->q, true);
1808 if (dev->zoned) {
1809 rv = null_zone_init(dev);
1810 if (rv)
1811 goto out_cleanup_blk_queue;
1813 blk_queue_chunk_sectors(nullb->q, dev->zone_size_sects);
1814 nullb->q->limits.zoned = BLK_ZONED_HM;
1817 nullb->q->queuedata = nullb;
1818 blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
1819 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
1821 mutex_lock(&lock);
1822 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
1823 dev->index = nullb->index;
1824 mutex_unlock(&lock);
1826 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1827 blk_queue_physical_block_size(nullb->q, dev->blocksize);
1829 null_config_discard(nullb);
1831 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1833 rv = null_gendisk_register(nullb);
1834 if (rv)
1835 goto out_cleanup_zone;
1837 mutex_lock(&lock);
1838 list_add_tail(&nullb->list, &nullb_list);
1839 mutex_unlock(&lock);
1841 return 0;
1842 out_cleanup_zone:
1843 if (dev->zoned)
1844 null_zone_exit(dev);
1845 out_cleanup_blk_queue:
1846 blk_cleanup_queue(nullb->q);
1847 out_cleanup_tags:
1848 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
1849 blk_mq_free_tag_set(nullb->tag_set);
1850 out_cleanup_queues:
1851 cleanup_queues(nullb);
1852 out_free_nullb:
1853 kfree(nullb);
1854 dev->nullb = NULL;
1855 out:
1856 return rv;
1859 static int __init null_init(void)
1861 int ret = 0;
1862 unsigned int i;
1863 struct nullb *nullb;
1864 struct nullb_device *dev;
1866 if (g_bs > PAGE_SIZE) {
1867 pr_warn("null_blk: invalid block size\n");
1868 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
1869 g_bs = PAGE_SIZE;
1872 if (!is_power_of_2(g_zone_size)) {
1873 pr_err("null_blk: zone_size must be power-of-two\n");
1874 return -EINVAL;
1877 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1878 if (g_submit_queues != nr_online_nodes) {
1879 pr_warn("null_blk: submit_queues param is set to %u.\n",
1880 nr_online_nodes);
1881 g_submit_queues = nr_online_nodes;
1883 } else if (g_submit_queues > nr_cpu_ids)
1884 g_submit_queues = nr_cpu_ids;
1885 else if (g_submit_queues <= 0)
1886 g_submit_queues = 1;
1888 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1889 ret = null_init_tag_set(NULL, &tag_set);
1890 if (ret)
1891 return ret;
1894 config_group_init(&nullb_subsys.su_group);
1895 mutex_init(&nullb_subsys.su_mutex);
1897 ret = configfs_register_subsystem(&nullb_subsys);
1898 if (ret)
1899 goto err_tagset;
1901 mutex_init(&lock);
1903 null_major = register_blkdev(0, "nullb");
1904 if (null_major < 0) {
1905 ret = null_major;
1906 goto err_conf;
1909 for (i = 0; i < nr_devices; i++) {
1910 dev = null_alloc_dev();
1911 if (!dev) {
1912 ret = -ENOMEM;
1913 goto err_dev;
1915 ret = null_add_dev(dev);
1916 if (ret) {
1917 null_free_dev(dev);
1918 goto err_dev;
1922 pr_info("null: module loaded\n");
1923 return 0;
1925 err_dev:
1926 while (!list_empty(&nullb_list)) {
1927 nullb = list_entry(nullb_list.next, struct nullb, list);
1928 dev = nullb->dev;
1929 null_del_dev(nullb);
1930 null_free_dev(dev);
1932 unregister_blkdev(null_major, "nullb");
1933 err_conf:
1934 configfs_unregister_subsystem(&nullb_subsys);
1935 err_tagset:
1936 if (g_queue_mode == NULL_Q_MQ && shared_tags)
1937 blk_mq_free_tag_set(&tag_set);
1938 return ret;
1941 static void __exit null_exit(void)
1943 struct nullb *nullb;
1945 configfs_unregister_subsystem(&nullb_subsys);
1947 unregister_blkdev(null_major, "nullb");
1949 mutex_lock(&lock);
1950 while (!list_empty(&nullb_list)) {
1951 struct nullb_device *dev;
1953 nullb = list_entry(nullb_list.next, struct nullb, list);
1954 dev = nullb->dev;
1955 null_del_dev(nullb);
1956 null_free_dev(dev);
1958 mutex_unlock(&lock);
1960 if (g_queue_mode == NULL_Q_MQ && shared_tags)
1961 blk_mq_free_tag_set(&tag_set);
1964 module_init(null_init);
1965 module_exit(null_exit);
1967 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
1968 MODULE_LICENSE("GPL");