treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / block / null_blk_main.c
blob16510795e37720de6b4c233bbd9c0f4bcf798e05
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
4 * Shaohua Li <shli@fb.com>
5 */
6 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/sched.h>
10 #include <linux/fs.h>
11 #include <linux/init.h>
12 #include "null_blk.h"
14 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
15 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
16 #define SECTOR_MASK (PAGE_SECTORS - 1)
18 #define FREE_BATCH 16
20 #define TICKS_PER_SEC 50ULL
21 #define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
23 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
24 static DECLARE_FAULT_ATTR(null_timeout_attr);
25 static DECLARE_FAULT_ATTR(null_requeue_attr);
26 #endif
28 static inline u64 mb_per_tick(int mbps)
30 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
34 * Status flags for nullb_device.
36 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
37 * UP: Device is currently on and visible in userspace.
38 * THROTTLED: Device is being throttled.
39 * CACHE: Device is using a write-back cache.
41 enum nullb_device_flags {
42 NULLB_DEV_FL_CONFIGURED = 0,
43 NULLB_DEV_FL_UP = 1,
44 NULLB_DEV_FL_THROTTLED = 2,
45 NULLB_DEV_FL_CACHE = 3,
48 #define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
50 * nullb_page is a page in memory for nullb devices.
52 * @page: The page holding the data.
53 * @bitmap: The bitmap represents which sector in the page has data.
54 * Each bit represents one block size. For example, sector 8
55 * will use the 7th bit
56 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
57 * page is being flushing to storage. FREE means the cache page is freed and
58 * should be skipped from flushing to storage. Please see
59 * null_make_cache_space
61 struct nullb_page {
62 struct page *page;
63 DECLARE_BITMAP(bitmap, MAP_SZ);
65 #define NULLB_PAGE_LOCK (MAP_SZ - 1)
66 #define NULLB_PAGE_FREE (MAP_SZ - 2)
68 static LIST_HEAD(nullb_list);
69 static struct mutex lock;
70 static int null_major;
71 static DEFINE_IDA(nullb_indexes);
72 static struct blk_mq_tag_set tag_set;
74 enum {
75 NULL_IRQ_NONE = 0,
76 NULL_IRQ_SOFTIRQ = 1,
77 NULL_IRQ_TIMER = 2,
80 enum {
81 NULL_Q_BIO = 0,
82 NULL_Q_RQ = 1,
83 NULL_Q_MQ = 2,
86 static int g_no_sched;
87 module_param_named(no_sched, g_no_sched, int, 0444);
88 MODULE_PARM_DESC(no_sched, "No io scheduler");
90 static int g_submit_queues = 1;
91 module_param_named(submit_queues, g_submit_queues, int, 0444);
92 MODULE_PARM_DESC(submit_queues, "Number of submission queues");
94 static int g_home_node = NUMA_NO_NODE;
95 module_param_named(home_node, g_home_node, int, 0444);
96 MODULE_PARM_DESC(home_node, "Home node for the device");
98 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
99 static char g_timeout_str[80];
100 module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
102 static char g_requeue_str[80];
103 module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
104 #endif
106 static int g_queue_mode = NULL_Q_MQ;
108 static int null_param_store_val(const char *str, int *val, int min, int max)
110 int ret, new_val;
112 ret = kstrtoint(str, 10, &new_val);
113 if (ret)
114 return -EINVAL;
116 if (new_val < min || new_val > max)
117 return -EINVAL;
119 *val = new_val;
120 return 0;
123 static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
125 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
128 static const struct kernel_param_ops null_queue_mode_param_ops = {
129 .set = null_set_queue_mode,
130 .get = param_get_int,
133 device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
134 MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
136 static int g_gb = 250;
137 module_param_named(gb, g_gb, int, 0444);
138 MODULE_PARM_DESC(gb, "Size in GB");
140 static int g_bs = 512;
141 module_param_named(bs, g_bs, int, 0444);
142 MODULE_PARM_DESC(bs, "Block size (in bytes)");
144 static unsigned int nr_devices = 1;
145 module_param(nr_devices, uint, 0444);
146 MODULE_PARM_DESC(nr_devices, "Number of devices to register");
148 static bool g_blocking;
149 module_param_named(blocking, g_blocking, bool, 0444);
150 MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
152 static bool shared_tags;
153 module_param(shared_tags, bool, 0444);
154 MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
156 static int g_irqmode = NULL_IRQ_SOFTIRQ;
158 static int null_set_irqmode(const char *str, const struct kernel_param *kp)
160 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
161 NULL_IRQ_TIMER);
164 static const struct kernel_param_ops null_irqmode_param_ops = {
165 .set = null_set_irqmode,
166 .get = param_get_int,
169 device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
170 MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
172 static unsigned long g_completion_nsec = 10000;
173 module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
174 MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
176 static int g_hw_queue_depth = 64;
177 module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
178 MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
180 static bool g_use_per_node_hctx;
181 module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
182 MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
184 static bool g_zoned;
185 module_param_named(zoned, g_zoned, bool, S_IRUGO);
186 MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
188 static unsigned long g_zone_size = 256;
189 module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
190 MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
192 static unsigned int g_zone_nr_conv;
193 module_param_named(zone_nr_conv, g_zone_nr_conv, uint, 0444);
194 MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones when block device is zoned. Default: 0");
196 static struct nullb_device *null_alloc_dev(void);
197 static void null_free_dev(struct nullb_device *dev);
198 static void null_del_dev(struct nullb *nullb);
199 static int null_add_dev(struct nullb_device *dev);
200 static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
202 static inline struct nullb_device *to_nullb_device(struct config_item *item)
204 return item ? container_of(item, struct nullb_device, item) : NULL;
207 static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
209 return snprintf(page, PAGE_SIZE, "%u\n", val);
212 static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
213 char *page)
215 return snprintf(page, PAGE_SIZE, "%lu\n", val);
218 static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
220 return snprintf(page, PAGE_SIZE, "%u\n", val);
223 static ssize_t nullb_device_uint_attr_store(unsigned int *val,
224 const char *page, size_t count)
226 unsigned int tmp;
227 int result;
229 result = kstrtouint(page, 0, &tmp);
230 if (result < 0)
231 return result;
233 *val = tmp;
234 return count;
237 static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
238 const char *page, size_t count)
240 int result;
241 unsigned long tmp;
243 result = kstrtoul(page, 0, &tmp);
244 if (result < 0)
245 return result;
247 *val = tmp;
248 return count;
251 static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
252 size_t count)
254 bool tmp;
255 int result;
257 result = kstrtobool(page, &tmp);
258 if (result < 0)
259 return result;
261 *val = tmp;
262 return count;
265 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
266 #define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY) \
267 static ssize_t \
268 nullb_device_##NAME##_show(struct config_item *item, char *page) \
270 return nullb_device_##TYPE##_attr_show( \
271 to_nullb_device(item)->NAME, page); \
273 static ssize_t \
274 nullb_device_##NAME##_store(struct config_item *item, const char *page, \
275 size_t count) \
277 int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
278 struct nullb_device *dev = to_nullb_device(item); \
279 TYPE uninitialized_var(new_value); \
280 int ret; \
282 ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
283 if (ret < 0) \
284 return ret; \
285 if (apply_fn) \
286 ret = apply_fn(dev, new_value); \
287 else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) \
288 ret = -EBUSY; \
289 if (ret < 0) \
290 return ret; \
291 dev->NAME = new_value; \
292 return count; \
294 CONFIGFS_ATTR(nullb_device_, NAME);
296 static int nullb_apply_submit_queues(struct nullb_device *dev,
297 unsigned int submit_queues)
299 struct nullb *nullb = dev->nullb;
300 struct blk_mq_tag_set *set;
302 if (!nullb)
303 return 0;
305 set = nullb->tag_set;
306 blk_mq_update_nr_hw_queues(set, submit_queues);
307 return set->nr_hw_queues == submit_queues ? 0 : -ENOMEM;
310 NULLB_DEVICE_ATTR(size, ulong, NULL);
311 NULLB_DEVICE_ATTR(completion_nsec, ulong, NULL);
312 NULLB_DEVICE_ATTR(submit_queues, uint, nullb_apply_submit_queues);
313 NULLB_DEVICE_ATTR(home_node, uint, NULL);
314 NULLB_DEVICE_ATTR(queue_mode, uint, NULL);
315 NULLB_DEVICE_ATTR(blocksize, uint, NULL);
316 NULLB_DEVICE_ATTR(irqmode, uint, NULL);
317 NULLB_DEVICE_ATTR(hw_queue_depth, uint, NULL);
318 NULLB_DEVICE_ATTR(index, uint, NULL);
319 NULLB_DEVICE_ATTR(blocking, bool, NULL);
320 NULLB_DEVICE_ATTR(use_per_node_hctx, bool, NULL);
321 NULLB_DEVICE_ATTR(memory_backed, bool, NULL);
322 NULLB_DEVICE_ATTR(discard, bool, NULL);
323 NULLB_DEVICE_ATTR(mbps, uint, NULL);
324 NULLB_DEVICE_ATTR(cache_size, ulong, NULL);
325 NULLB_DEVICE_ATTR(zoned, bool, NULL);
326 NULLB_DEVICE_ATTR(zone_size, ulong, NULL);
327 NULLB_DEVICE_ATTR(zone_nr_conv, uint, NULL);
329 static ssize_t nullb_device_power_show(struct config_item *item, char *page)
331 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
334 static ssize_t nullb_device_power_store(struct config_item *item,
335 const char *page, size_t count)
337 struct nullb_device *dev = to_nullb_device(item);
338 bool newp = false;
339 ssize_t ret;
341 ret = nullb_device_bool_attr_store(&newp, page, count);
342 if (ret < 0)
343 return ret;
345 if (!dev->power && newp) {
346 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
347 return count;
348 if (null_add_dev(dev)) {
349 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
350 return -ENOMEM;
353 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
354 dev->power = newp;
355 } else if (dev->power && !newp) {
356 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
357 mutex_lock(&lock);
358 dev->power = newp;
359 null_del_dev(dev->nullb);
360 mutex_unlock(&lock);
362 clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
365 return count;
368 CONFIGFS_ATTR(nullb_device_, power);
370 static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
372 struct nullb_device *t_dev = to_nullb_device(item);
374 return badblocks_show(&t_dev->badblocks, page, 0);
377 static ssize_t nullb_device_badblocks_store(struct config_item *item,
378 const char *page, size_t count)
380 struct nullb_device *t_dev = to_nullb_device(item);
381 char *orig, *buf, *tmp;
382 u64 start, end;
383 int ret;
385 orig = kstrndup(page, count, GFP_KERNEL);
386 if (!orig)
387 return -ENOMEM;
389 buf = strstrip(orig);
391 ret = -EINVAL;
392 if (buf[0] != '+' && buf[0] != '-')
393 goto out;
394 tmp = strchr(&buf[1], '-');
395 if (!tmp)
396 goto out;
397 *tmp = '\0';
398 ret = kstrtoull(buf + 1, 0, &start);
399 if (ret)
400 goto out;
401 ret = kstrtoull(tmp + 1, 0, &end);
402 if (ret)
403 goto out;
404 ret = -EINVAL;
405 if (start > end)
406 goto out;
407 /* enable badblocks */
408 cmpxchg(&t_dev->badblocks.shift, -1, 0);
409 if (buf[0] == '+')
410 ret = badblocks_set(&t_dev->badblocks, start,
411 end - start + 1, 1);
412 else
413 ret = badblocks_clear(&t_dev->badblocks, start,
414 end - start + 1);
415 if (ret == 0)
416 ret = count;
417 out:
418 kfree(orig);
419 return ret;
421 CONFIGFS_ATTR(nullb_device_, badblocks);
423 static struct configfs_attribute *nullb_device_attrs[] = {
424 &nullb_device_attr_size,
425 &nullb_device_attr_completion_nsec,
426 &nullb_device_attr_submit_queues,
427 &nullb_device_attr_home_node,
428 &nullb_device_attr_queue_mode,
429 &nullb_device_attr_blocksize,
430 &nullb_device_attr_irqmode,
431 &nullb_device_attr_hw_queue_depth,
432 &nullb_device_attr_index,
433 &nullb_device_attr_blocking,
434 &nullb_device_attr_use_per_node_hctx,
435 &nullb_device_attr_power,
436 &nullb_device_attr_memory_backed,
437 &nullb_device_attr_discard,
438 &nullb_device_attr_mbps,
439 &nullb_device_attr_cache_size,
440 &nullb_device_attr_badblocks,
441 &nullb_device_attr_zoned,
442 &nullb_device_attr_zone_size,
443 &nullb_device_attr_zone_nr_conv,
444 NULL,
447 static void nullb_device_release(struct config_item *item)
449 struct nullb_device *dev = to_nullb_device(item);
451 null_free_device_storage(dev, false);
452 null_free_dev(dev);
455 static struct configfs_item_operations nullb_device_ops = {
456 .release = nullb_device_release,
459 static const struct config_item_type nullb_device_type = {
460 .ct_item_ops = &nullb_device_ops,
461 .ct_attrs = nullb_device_attrs,
462 .ct_owner = THIS_MODULE,
465 static struct
466 config_item *nullb_group_make_item(struct config_group *group, const char *name)
468 struct nullb_device *dev;
470 dev = null_alloc_dev();
471 if (!dev)
472 return ERR_PTR(-ENOMEM);
474 config_item_init_type_name(&dev->item, name, &nullb_device_type);
476 return &dev->item;
479 static void
480 nullb_group_drop_item(struct config_group *group, struct config_item *item)
482 struct nullb_device *dev = to_nullb_device(item);
484 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
485 mutex_lock(&lock);
486 dev->power = false;
487 null_del_dev(dev->nullb);
488 mutex_unlock(&lock);
491 config_item_put(item);
494 static ssize_t memb_group_features_show(struct config_item *item, char *page)
496 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size,zone_nr_conv\n");
499 CONFIGFS_ATTR_RO(memb_group_, features);
501 static struct configfs_attribute *nullb_group_attrs[] = {
502 &memb_group_attr_features,
503 NULL,
506 static struct configfs_group_operations nullb_group_ops = {
507 .make_item = nullb_group_make_item,
508 .drop_item = nullb_group_drop_item,
511 static const struct config_item_type nullb_group_type = {
512 .ct_group_ops = &nullb_group_ops,
513 .ct_attrs = nullb_group_attrs,
514 .ct_owner = THIS_MODULE,
517 static struct configfs_subsystem nullb_subsys = {
518 .su_group = {
519 .cg_item = {
520 .ci_namebuf = "nullb",
521 .ci_type = &nullb_group_type,
526 static inline int null_cache_active(struct nullb *nullb)
528 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
531 static struct nullb_device *null_alloc_dev(void)
533 struct nullb_device *dev;
535 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
536 if (!dev)
537 return NULL;
538 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
539 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
540 if (badblocks_init(&dev->badblocks, 0)) {
541 kfree(dev);
542 return NULL;
545 dev->size = g_gb * 1024;
546 dev->completion_nsec = g_completion_nsec;
547 dev->submit_queues = g_submit_queues;
548 dev->home_node = g_home_node;
549 dev->queue_mode = g_queue_mode;
550 dev->blocksize = g_bs;
551 dev->irqmode = g_irqmode;
552 dev->hw_queue_depth = g_hw_queue_depth;
553 dev->blocking = g_blocking;
554 dev->use_per_node_hctx = g_use_per_node_hctx;
555 dev->zoned = g_zoned;
556 dev->zone_size = g_zone_size;
557 dev->zone_nr_conv = g_zone_nr_conv;
558 return dev;
561 static void null_free_dev(struct nullb_device *dev)
563 if (!dev)
564 return;
566 null_zone_exit(dev);
567 badblocks_exit(&dev->badblocks);
568 kfree(dev);
571 static void put_tag(struct nullb_queue *nq, unsigned int tag)
573 clear_bit_unlock(tag, nq->tag_map);
575 if (waitqueue_active(&nq->wait))
576 wake_up(&nq->wait);
579 static unsigned int get_tag(struct nullb_queue *nq)
581 unsigned int tag;
583 do {
584 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
585 if (tag >= nq->queue_depth)
586 return -1U;
587 } while (test_and_set_bit_lock(tag, nq->tag_map));
589 return tag;
592 static void free_cmd(struct nullb_cmd *cmd)
594 put_tag(cmd->nq, cmd->tag);
597 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
599 static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
601 struct nullb_cmd *cmd;
602 unsigned int tag;
604 tag = get_tag(nq);
605 if (tag != -1U) {
606 cmd = &nq->cmds[tag];
607 cmd->tag = tag;
608 cmd->nq = nq;
609 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
610 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
611 HRTIMER_MODE_REL);
612 cmd->timer.function = null_cmd_timer_expired;
614 return cmd;
617 return NULL;
620 static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
622 struct nullb_cmd *cmd;
623 DEFINE_WAIT(wait);
625 cmd = __alloc_cmd(nq);
626 if (cmd || !can_wait)
627 return cmd;
629 do {
630 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
631 cmd = __alloc_cmd(nq);
632 if (cmd)
633 break;
635 io_schedule();
636 } while (1);
638 finish_wait(&nq->wait, &wait);
639 return cmd;
642 static void end_cmd(struct nullb_cmd *cmd)
644 int queue_mode = cmd->nq->dev->queue_mode;
646 switch (queue_mode) {
647 case NULL_Q_MQ:
648 blk_mq_end_request(cmd->rq, cmd->error);
649 return;
650 case NULL_Q_BIO:
651 cmd->bio->bi_status = cmd->error;
652 bio_endio(cmd->bio);
653 break;
656 free_cmd(cmd);
659 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
661 end_cmd(container_of(timer, struct nullb_cmd, timer));
663 return HRTIMER_NORESTART;
666 static void null_cmd_end_timer(struct nullb_cmd *cmd)
668 ktime_t kt = cmd->nq->dev->completion_nsec;
670 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
673 static void null_complete_rq(struct request *rq)
675 end_cmd(blk_mq_rq_to_pdu(rq));
678 static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
680 struct nullb_page *t_page;
682 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
683 if (!t_page)
684 goto out;
686 t_page->page = alloc_pages(gfp_flags, 0);
687 if (!t_page->page)
688 goto out_freepage;
690 memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
691 return t_page;
692 out_freepage:
693 kfree(t_page);
694 out:
695 return NULL;
698 static void null_free_page(struct nullb_page *t_page)
700 __set_bit(NULLB_PAGE_FREE, t_page->bitmap);
701 if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
702 return;
703 __free_page(t_page->page);
704 kfree(t_page);
707 static bool null_page_empty(struct nullb_page *page)
709 int size = MAP_SZ - 2;
711 return find_first_bit(page->bitmap, size) == size;
714 static void null_free_sector(struct nullb *nullb, sector_t sector,
715 bool is_cache)
717 unsigned int sector_bit;
718 u64 idx;
719 struct nullb_page *t_page, *ret;
720 struct radix_tree_root *root;
722 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
723 idx = sector >> PAGE_SECTORS_SHIFT;
724 sector_bit = (sector & SECTOR_MASK);
726 t_page = radix_tree_lookup(root, idx);
727 if (t_page) {
728 __clear_bit(sector_bit, t_page->bitmap);
730 if (null_page_empty(t_page)) {
731 ret = radix_tree_delete_item(root, idx, t_page);
732 WARN_ON(ret != t_page);
733 null_free_page(ret);
734 if (is_cache)
735 nullb->dev->curr_cache -= PAGE_SIZE;
740 static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
741 struct nullb_page *t_page, bool is_cache)
743 struct radix_tree_root *root;
745 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
747 if (radix_tree_insert(root, idx, t_page)) {
748 null_free_page(t_page);
749 t_page = radix_tree_lookup(root, idx);
750 WARN_ON(!t_page || t_page->page->index != idx);
751 } else if (is_cache)
752 nullb->dev->curr_cache += PAGE_SIZE;
754 return t_page;
757 static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
759 unsigned long pos = 0;
760 int nr_pages;
761 struct nullb_page *ret, *t_pages[FREE_BATCH];
762 struct radix_tree_root *root;
764 root = is_cache ? &dev->cache : &dev->data;
766 do {
767 int i;
769 nr_pages = radix_tree_gang_lookup(root,
770 (void **)t_pages, pos, FREE_BATCH);
772 for (i = 0; i < nr_pages; i++) {
773 pos = t_pages[i]->page->index;
774 ret = radix_tree_delete_item(root, pos, t_pages[i]);
775 WARN_ON(ret != t_pages[i]);
776 null_free_page(ret);
779 pos++;
780 } while (nr_pages == FREE_BATCH);
782 if (is_cache)
783 dev->curr_cache = 0;
786 static struct nullb_page *__null_lookup_page(struct nullb *nullb,
787 sector_t sector, bool for_write, bool is_cache)
789 unsigned int sector_bit;
790 u64 idx;
791 struct nullb_page *t_page;
792 struct radix_tree_root *root;
794 idx = sector >> PAGE_SECTORS_SHIFT;
795 sector_bit = (sector & SECTOR_MASK);
797 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
798 t_page = radix_tree_lookup(root, idx);
799 WARN_ON(t_page && t_page->page->index != idx);
801 if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
802 return t_page;
804 return NULL;
807 static struct nullb_page *null_lookup_page(struct nullb *nullb,
808 sector_t sector, bool for_write, bool ignore_cache)
810 struct nullb_page *page = NULL;
812 if (!ignore_cache)
813 page = __null_lookup_page(nullb, sector, for_write, true);
814 if (page)
815 return page;
816 return __null_lookup_page(nullb, sector, for_write, false);
819 static struct nullb_page *null_insert_page(struct nullb *nullb,
820 sector_t sector, bool ignore_cache)
821 __releases(&nullb->lock)
822 __acquires(&nullb->lock)
824 u64 idx;
825 struct nullb_page *t_page;
827 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
828 if (t_page)
829 return t_page;
831 spin_unlock_irq(&nullb->lock);
833 t_page = null_alloc_page(GFP_NOIO);
834 if (!t_page)
835 goto out_lock;
837 if (radix_tree_preload(GFP_NOIO))
838 goto out_freepage;
840 spin_lock_irq(&nullb->lock);
841 idx = sector >> PAGE_SECTORS_SHIFT;
842 t_page->page->index = idx;
843 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
844 radix_tree_preload_end();
846 return t_page;
847 out_freepage:
848 null_free_page(t_page);
849 out_lock:
850 spin_lock_irq(&nullb->lock);
851 return null_lookup_page(nullb, sector, true, ignore_cache);
854 static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
856 int i;
857 unsigned int offset;
858 u64 idx;
859 struct nullb_page *t_page, *ret;
860 void *dst, *src;
862 idx = c_page->page->index;
864 t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
866 __clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
867 if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
868 null_free_page(c_page);
869 if (t_page && null_page_empty(t_page)) {
870 ret = radix_tree_delete_item(&nullb->dev->data,
871 idx, t_page);
872 null_free_page(t_page);
874 return 0;
877 if (!t_page)
878 return -ENOMEM;
880 src = kmap_atomic(c_page->page);
881 dst = kmap_atomic(t_page->page);
883 for (i = 0; i < PAGE_SECTORS;
884 i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
885 if (test_bit(i, c_page->bitmap)) {
886 offset = (i << SECTOR_SHIFT);
887 memcpy(dst + offset, src + offset,
888 nullb->dev->blocksize);
889 __set_bit(i, t_page->bitmap);
893 kunmap_atomic(dst);
894 kunmap_atomic(src);
896 ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
897 null_free_page(ret);
898 nullb->dev->curr_cache -= PAGE_SIZE;
900 return 0;
903 static int null_make_cache_space(struct nullb *nullb, unsigned long n)
905 int i, err, nr_pages;
906 struct nullb_page *c_pages[FREE_BATCH];
907 unsigned long flushed = 0, one_round;
909 again:
910 if ((nullb->dev->cache_size * 1024 * 1024) >
911 nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
912 return 0;
914 nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
915 (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
917 * nullb_flush_cache_page could unlock before using the c_pages. To
918 * avoid race, we don't allow page free
920 for (i = 0; i < nr_pages; i++) {
921 nullb->cache_flush_pos = c_pages[i]->page->index;
923 * We found the page which is being flushed to disk by other
924 * threads
926 if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
927 c_pages[i] = NULL;
928 else
929 __set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
932 one_round = 0;
933 for (i = 0; i < nr_pages; i++) {
934 if (c_pages[i] == NULL)
935 continue;
936 err = null_flush_cache_page(nullb, c_pages[i]);
937 if (err)
938 return err;
939 one_round++;
941 flushed += one_round << PAGE_SHIFT;
943 if (n > flushed) {
944 if (nr_pages == 0)
945 nullb->cache_flush_pos = 0;
946 if (one_round == 0) {
947 /* give other threads a chance */
948 spin_unlock_irq(&nullb->lock);
949 spin_lock_irq(&nullb->lock);
951 goto again;
953 return 0;
956 static int copy_to_nullb(struct nullb *nullb, struct page *source,
957 unsigned int off, sector_t sector, size_t n, bool is_fua)
959 size_t temp, count = 0;
960 unsigned int offset;
961 struct nullb_page *t_page;
962 void *dst, *src;
964 while (count < n) {
965 temp = min_t(size_t, nullb->dev->blocksize, n - count);
967 if (null_cache_active(nullb) && !is_fua)
968 null_make_cache_space(nullb, PAGE_SIZE);
970 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
971 t_page = null_insert_page(nullb, sector,
972 !null_cache_active(nullb) || is_fua);
973 if (!t_page)
974 return -ENOSPC;
976 src = kmap_atomic(source);
977 dst = kmap_atomic(t_page->page);
978 memcpy(dst + offset, src + off + count, temp);
979 kunmap_atomic(dst);
980 kunmap_atomic(src);
982 __set_bit(sector & SECTOR_MASK, t_page->bitmap);
984 if (is_fua)
985 null_free_sector(nullb, sector, true);
987 count += temp;
988 sector += temp >> SECTOR_SHIFT;
990 return 0;
993 static int copy_from_nullb(struct nullb *nullb, struct page *dest,
994 unsigned int off, sector_t sector, size_t n)
996 size_t temp, count = 0;
997 unsigned int offset;
998 struct nullb_page *t_page;
999 void *dst, *src;
1001 while (count < n) {
1002 temp = min_t(size_t, nullb->dev->blocksize, n - count);
1004 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
1005 t_page = null_lookup_page(nullb, sector, false,
1006 !null_cache_active(nullb));
1008 dst = kmap_atomic(dest);
1009 if (!t_page) {
1010 memset(dst + off + count, 0, temp);
1011 goto next;
1013 src = kmap_atomic(t_page->page);
1014 memcpy(dst + off + count, src + offset, temp);
1015 kunmap_atomic(src);
1016 next:
1017 kunmap_atomic(dst);
1019 count += temp;
1020 sector += temp >> SECTOR_SHIFT;
1022 return 0;
1025 static void nullb_fill_pattern(struct nullb *nullb, struct page *page,
1026 unsigned int len, unsigned int off)
1028 void *dst;
1030 dst = kmap_atomic(page);
1031 memset(dst + off, 0xFF, len);
1032 kunmap_atomic(dst);
1035 static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
1037 size_t temp;
1039 spin_lock_irq(&nullb->lock);
1040 while (n > 0) {
1041 temp = min_t(size_t, n, nullb->dev->blocksize);
1042 null_free_sector(nullb, sector, false);
1043 if (null_cache_active(nullb))
1044 null_free_sector(nullb, sector, true);
1045 sector += temp >> SECTOR_SHIFT;
1046 n -= temp;
1048 spin_unlock_irq(&nullb->lock);
1051 static int null_handle_flush(struct nullb *nullb)
1053 int err;
1055 if (!null_cache_active(nullb))
1056 return 0;
1058 spin_lock_irq(&nullb->lock);
1059 while (true) {
1060 err = null_make_cache_space(nullb,
1061 nullb->dev->cache_size * 1024 * 1024);
1062 if (err || nullb->dev->curr_cache == 0)
1063 break;
1066 WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1067 spin_unlock_irq(&nullb->lock);
1068 return err;
1071 static int null_transfer(struct nullb *nullb, struct page *page,
1072 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1073 bool is_fua)
1075 struct nullb_device *dev = nullb->dev;
1076 unsigned int valid_len = len;
1077 int err = 0;
1079 if (!is_write) {
1080 if (dev->zoned)
1081 valid_len = null_zone_valid_read_len(nullb,
1082 sector, len);
1084 if (valid_len) {
1085 err = copy_from_nullb(nullb, page, off,
1086 sector, valid_len);
1087 off += valid_len;
1088 len -= valid_len;
1091 if (len)
1092 nullb_fill_pattern(nullb, page, len, off);
1093 flush_dcache_page(page);
1094 } else {
1095 flush_dcache_page(page);
1096 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
1099 return err;
1102 static int null_handle_rq(struct nullb_cmd *cmd)
1104 struct request *rq = cmd->rq;
1105 struct nullb *nullb = cmd->nq->dev->nullb;
1106 int err;
1107 unsigned int len;
1108 sector_t sector;
1109 struct req_iterator iter;
1110 struct bio_vec bvec;
1112 sector = blk_rq_pos(rq);
1114 if (req_op(rq) == REQ_OP_DISCARD) {
1115 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1116 return 0;
1119 spin_lock_irq(&nullb->lock);
1120 rq_for_each_segment(bvec, rq, iter) {
1121 len = bvec.bv_len;
1122 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
1123 op_is_write(req_op(rq)), sector,
1124 req_op(rq) & 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);
1133 return 0;
1136 static int null_handle_bio(struct nullb_cmd *cmd)
1138 struct bio *bio = cmd->bio;
1139 struct nullb *nullb = cmd->nq->dev->nullb;
1140 int err;
1141 unsigned int len;
1142 sector_t sector;
1143 struct bio_vec bvec;
1144 struct bvec_iter iter;
1146 sector = bio->bi_iter.bi_sector;
1148 if (bio_op(bio) == REQ_OP_DISCARD) {
1149 null_handle_discard(nullb, sector,
1150 bio_sectors(bio) << SECTOR_SHIFT);
1151 return 0;
1154 spin_lock_irq(&nullb->lock);
1155 bio_for_each_segment(bvec, bio, iter) {
1156 len = bvec.bv_len;
1157 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
1158 op_is_write(bio_op(bio)), sector,
1159 bio->bi_opf & REQ_FUA);
1160 if (err) {
1161 spin_unlock_irq(&nullb->lock);
1162 return err;
1164 sector += len >> SECTOR_SHIFT;
1166 spin_unlock_irq(&nullb->lock);
1167 return 0;
1170 static void null_stop_queue(struct nullb *nullb)
1172 struct request_queue *q = nullb->q;
1174 if (nullb->dev->queue_mode == NULL_Q_MQ)
1175 blk_mq_stop_hw_queues(q);
1178 static void null_restart_queue_async(struct nullb *nullb)
1180 struct request_queue *q = nullb->q;
1182 if (nullb->dev->queue_mode == NULL_Q_MQ)
1183 blk_mq_start_stopped_hw_queues(q, true);
1186 static inline blk_status_t null_handle_throttled(struct nullb_cmd *cmd)
1188 struct nullb_device *dev = cmd->nq->dev;
1189 struct nullb *nullb = dev->nullb;
1190 blk_status_t sts = BLK_STS_OK;
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), &nullb->cur_bytes) < 0) {
1197 null_stop_queue(nullb);
1198 /* race with timer */
1199 if (atomic_long_read(&nullb->cur_bytes) > 0)
1200 null_restart_queue_async(nullb);
1201 /* requeue request */
1202 sts = BLK_STS_DEV_RESOURCE;
1204 return sts;
1207 static inline blk_status_t null_handle_badblocks(struct nullb_cmd *cmd,
1208 sector_t sector,
1209 sector_t nr_sectors)
1211 struct badblocks *bb = &cmd->nq->dev->badblocks;
1212 sector_t first_bad;
1213 int bad_sectors;
1215 if (badblocks_check(bb, sector, nr_sectors, &first_bad, &bad_sectors))
1216 return BLK_STS_IOERR;
1218 return BLK_STS_OK;
1221 static inline blk_status_t null_handle_memory_backed(struct nullb_cmd *cmd,
1222 enum req_opf op)
1224 struct nullb_device *dev = cmd->nq->dev;
1225 int err;
1227 if (dev->queue_mode == NULL_Q_BIO)
1228 err = null_handle_bio(cmd);
1229 else
1230 err = null_handle_rq(cmd);
1232 return errno_to_blk_status(err);
1235 static inline void nullb_complete_cmd(struct nullb_cmd *cmd)
1237 /* Complete IO by inline, softirq or timer */
1238 switch (cmd->nq->dev->irqmode) {
1239 case NULL_IRQ_SOFTIRQ:
1240 switch (cmd->nq->dev->queue_mode) {
1241 case NULL_Q_MQ:
1242 blk_mq_complete_request(cmd->rq);
1243 break;
1244 case NULL_Q_BIO:
1246 * XXX: no proper submitting cpu information available.
1248 end_cmd(cmd);
1249 break;
1251 break;
1252 case NULL_IRQ_NONE:
1253 end_cmd(cmd);
1254 break;
1255 case NULL_IRQ_TIMER:
1256 null_cmd_end_timer(cmd);
1257 break;
1261 static blk_status_t null_handle_cmd(struct nullb_cmd *cmd, sector_t sector,
1262 sector_t nr_sectors, enum req_opf op)
1264 struct nullb_device *dev = cmd->nq->dev;
1265 struct nullb *nullb = dev->nullb;
1266 blk_status_t sts;
1268 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1269 sts = null_handle_throttled(cmd);
1270 if (sts != BLK_STS_OK)
1271 return sts;
1274 if (op == REQ_OP_FLUSH) {
1275 cmd->error = errno_to_blk_status(null_handle_flush(nullb));
1276 goto out;
1279 if (nullb->dev->badblocks.shift != -1) {
1280 cmd->error = null_handle_badblocks(cmd, sector, nr_sectors);
1281 if (cmd->error != BLK_STS_OK)
1282 goto out;
1285 if (dev->memory_backed)
1286 cmd->error = null_handle_memory_backed(cmd, op);
1288 if (!cmd->error && dev->zoned)
1289 cmd->error = null_handle_zoned(cmd, op, sector, nr_sectors);
1291 out:
1292 nullb_complete_cmd(cmd);
1293 return BLK_STS_OK;
1296 static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1298 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1299 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1300 unsigned int mbps = nullb->dev->mbps;
1302 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1303 return HRTIMER_NORESTART;
1305 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1306 null_restart_queue_async(nullb);
1308 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1310 return HRTIMER_RESTART;
1313 static void nullb_setup_bwtimer(struct nullb *nullb)
1315 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1317 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1318 nullb->bw_timer.function = nullb_bwtimer_fn;
1319 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1320 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
1323 static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1325 int index = 0;
1327 if (nullb->nr_queues != 1)
1328 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1330 return &nullb->queues[index];
1333 static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
1335 sector_t sector = bio->bi_iter.bi_sector;
1336 sector_t nr_sectors = bio_sectors(bio);
1337 struct nullb *nullb = q->queuedata;
1338 struct nullb_queue *nq = nullb_to_queue(nullb);
1339 struct nullb_cmd *cmd;
1341 cmd = alloc_cmd(nq, 1);
1342 cmd->bio = bio;
1344 null_handle_cmd(cmd, sector, nr_sectors, bio_op(bio));
1345 return BLK_QC_T_NONE;
1348 static bool should_timeout_request(struct request *rq)
1350 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1351 if (g_timeout_str[0])
1352 return should_fail(&null_timeout_attr, 1);
1353 #endif
1354 return false;
1357 static bool should_requeue_request(struct request *rq)
1359 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1360 if (g_requeue_str[0])
1361 return should_fail(&null_requeue_attr, 1);
1362 #endif
1363 return false;
1366 static enum blk_eh_timer_return null_timeout_rq(struct request *rq, bool res)
1368 pr_info("rq %p timed out\n", rq);
1369 blk_mq_complete_request(rq);
1370 return BLK_EH_DONE;
1373 static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
1374 const struct blk_mq_queue_data *bd)
1376 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
1377 struct nullb_queue *nq = hctx->driver_data;
1378 sector_t nr_sectors = blk_rq_sectors(bd->rq);
1379 sector_t sector = blk_rq_pos(bd->rq);
1381 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1383 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
1384 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1385 cmd->timer.function = null_cmd_timer_expired;
1387 cmd->rq = bd->rq;
1388 cmd->nq = nq;
1390 blk_mq_start_request(bd->rq);
1392 if (should_requeue_request(bd->rq)) {
1394 * Alternate between hitting the core BUSY path, and the
1395 * driver driven requeue path
1397 nq->requeue_selection++;
1398 if (nq->requeue_selection & 1)
1399 return BLK_STS_RESOURCE;
1400 else {
1401 blk_mq_requeue_request(bd->rq, true);
1402 return BLK_STS_OK;
1405 if (should_timeout_request(bd->rq))
1406 return BLK_STS_OK;
1408 return null_handle_cmd(cmd, sector, nr_sectors, req_op(bd->rq));
1411 static const struct blk_mq_ops null_mq_ops = {
1412 .queue_rq = null_queue_rq,
1413 .complete = null_complete_rq,
1414 .timeout = null_timeout_rq,
1417 static void cleanup_queue(struct nullb_queue *nq)
1419 kfree(nq->tag_map);
1420 kfree(nq->cmds);
1423 static void cleanup_queues(struct nullb *nullb)
1425 int i;
1427 for (i = 0; i < nullb->nr_queues; i++)
1428 cleanup_queue(&nullb->queues[i]);
1430 kfree(nullb->queues);
1433 static void null_del_dev(struct nullb *nullb)
1435 struct nullb_device *dev = nullb->dev;
1437 ida_simple_remove(&nullb_indexes, nullb->index);
1439 list_del_init(&nullb->list);
1441 del_gendisk(nullb->disk);
1443 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1444 hrtimer_cancel(&nullb->bw_timer);
1445 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1446 null_restart_queue_async(nullb);
1449 blk_cleanup_queue(nullb->q);
1450 if (dev->queue_mode == NULL_Q_MQ &&
1451 nullb->tag_set == &nullb->__tag_set)
1452 blk_mq_free_tag_set(nullb->tag_set);
1453 put_disk(nullb->disk);
1454 cleanup_queues(nullb);
1455 if (null_cache_active(nullb))
1456 null_free_device_storage(nullb->dev, true);
1457 kfree(nullb);
1458 dev->nullb = NULL;
1461 static void null_config_discard(struct nullb *nullb)
1463 if (nullb->dev->discard == false)
1464 return;
1465 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1466 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1467 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
1468 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nullb->q);
1471 static const struct block_device_operations null_ops = {
1472 .owner = THIS_MODULE,
1473 .report_zones = null_report_zones,
1476 static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1478 BUG_ON(!nullb);
1479 BUG_ON(!nq);
1481 init_waitqueue_head(&nq->wait);
1482 nq->queue_depth = nullb->queue_depth;
1483 nq->dev = nullb->dev;
1486 static void null_init_queues(struct nullb *nullb)
1488 struct request_queue *q = nullb->q;
1489 struct blk_mq_hw_ctx *hctx;
1490 struct nullb_queue *nq;
1491 int i;
1493 queue_for_each_hw_ctx(q, hctx, i) {
1494 if (!hctx->nr_ctx || !hctx->tags)
1495 continue;
1496 nq = &nullb->queues[i];
1497 hctx->driver_data = nq;
1498 null_init_queue(nullb, nq);
1499 nullb->nr_queues++;
1503 static int setup_commands(struct nullb_queue *nq)
1505 struct nullb_cmd *cmd;
1506 int i, tag_size;
1508 nq->cmds = kcalloc(nq->queue_depth, sizeof(*cmd), GFP_KERNEL);
1509 if (!nq->cmds)
1510 return -ENOMEM;
1512 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
1513 nq->tag_map = kcalloc(tag_size, sizeof(unsigned long), GFP_KERNEL);
1514 if (!nq->tag_map) {
1515 kfree(nq->cmds);
1516 return -ENOMEM;
1519 for (i = 0; i < nq->queue_depth; i++) {
1520 cmd = &nq->cmds[i];
1521 INIT_LIST_HEAD(&cmd->list);
1522 cmd->ll_list.next = NULL;
1523 cmd->tag = -1U;
1526 return 0;
1529 static int setup_queues(struct nullb *nullb)
1531 nullb->queues = kcalloc(nullb->dev->submit_queues,
1532 sizeof(struct nullb_queue),
1533 GFP_KERNEL);
1534 if (!nullb->queues)
1535 return -ENOMEM;
1537 nullb->queue_depth = nullb->dev->hw_queue_depth;
1539 return 0;
1542 static int init_driver_queues(struct nullb *nullb)
1544 struct nullb_queue *nq;
1545 int i, ret = 0;
1547 for (i = 0; i < nullb->dev->submit_queues; i++) {
1548 nq = &nullb->queues[i];
1550 null_init_queue(nullb, nq);
1552 ret = setup_commands(nq);
1553 if (ret)
1554 return ret;
1555 nullb->nr_queues++;
1557 return 0;
1560 static int null_gendisk_register(struct nullb *nullb)
1562 sector_t size = ((sector_t)nullb->dev->size * SZ_1M) >> SECTOR_SHIFT;
1563 struct gendisk *disk;
1565 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
1566 if (!disk)
1567 return -ENOMEM;
1568 set_capacity(disk, size);
1570 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1571 disk->major = null_major;
1572 disk->first_minor = nullb->index;
1573 disk->fops = &null_ops;
1574 disk->private_data = nullb;
1575 disk->queue = nullb->q;
1576 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1578 #ifdef CONFIG_BLK_DEV_ZONED
1579 if (nullb->dev->zoned) {
1580 if (queue_is_mq(nullb->q)) {
1581 int ret = blk_revalidate_disk_zones(disk);
1582 if (ret)
1583 return ret;
1584 } else {
1585 blk_queue_chunk_sectors(nullb->q,
1586 nullb->dev->zone_size_sects);
1587 nullb->q->nr_zones = blkdev_nr_zones(disk);
1590 #endif
1592 add_disk(disk);
1593 return 0;
1596 static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
1598 set->ops = &null_mq_ops;
1599 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1600 g_submit_queues;
1601 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1602 g_hw_queue_depth;
1603 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
1604 set->cmd_size = sizeof(struct nullb_cmd);
1605 set->flags = BLK_MQ_F_SHOULD_MERGE;
1606 if (g_no_sched)
1607 set->flags |= BLK_MQ_F_NO_SCHED;
1608 set->driver_data = NULL;
1610 if ((nullb && nullb->dev->blocking) || g_blocking)
1611 set->flags |= BLK_MQ_F_BLOCKING;
1613 return blk_mq_alloc_tag_set(set);
1616 static int null_validate_conf(struct nullb_device *dev)
1618 dev->blocksize = round_down(dev->blocksize, 512);
1619 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
1621 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1622 if (dev->submit_queues != nr_online_nodes)
1623 dev->submit_queues = nr_online_nodes;
1624 } else if (dev->submit_queues > nr_cpu_ids)
1625 dev->submit_queues = nr_cpu_ids;
1626 else if (dev->submit_queues == 0)
1627 dev->submit_queues = 1;
1629 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1630 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
1632 /* Do memory allocation, so set blocking */
1633 if (dev->memory_backed)
1634 dev->blocking = true;
1635 else /* cache is meaningless */
1636 dev->cache_size = 0;
1637 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1638 dev->cache_size);
1639 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1640 /* can not stop a queue */
1641 if (dev->queue_mode == NULL_Q_BIO)
1642 dev->mbps = 0;
1644 if (dev->zoned &&
1645 (!dev->zone_size || !is_power_of_2(dev->zone_size))) {
1646 pr_err("zone_size must be power-of-two\n");
1647 return -EINVAL;
1650 return 0;
1653 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1654 static bool __null_setup_fault(struct fault_attr *attr, char *str)
1656 if (!str[0])
1657 return true;
1659 if (!setup_fault_attr(attr, str))
1660 return false;
1662 attr->verbose = 0;
1663 return true;
1665 #endif
1667 static bool null_setup_fault(void)
1669 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1670 if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
1671 return false;
1672 if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
1673 return false;
1674 #endif
1675 return true;
1678 static int null_add_dev(struct nullb_device *dev)
1680 struct nullb *nullb;
1681 int rv;
1683 rv = null_validate_conf(dev);
1684 if (rv)
1685 return rv;
1687 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
1688 if (!nullb) {
1689 rv = -ENOMEM;
1690 goto out;
1692 nullb->dev = dev;
1693 dev->nullb = nullb;
1695 spin_lock_init(&nullb->lock);
1697 rv = setup_queues(nullb);
1698 if (rv)
1699 goto out_free_nullb;
1701 if (dev->queue_mode == NULL_Q_MQ) {
1702 if (shared_tags) {
1703 nullb->tag_set = &tag_set;
1704 rv = 0;
1705 } else {
1706 nullb->tag_set = &nullb->__tag_set;
1707 rv = null_init_tag_set(nullb, nullb->tag_set);
1710 if (rv)
1711 goto out_cleanup_queues;
1713 if (!null_setup_fault())
1714 goto out_cleanup_queues;
1716 nullb->tag_set->timeout = 5 * HZ;
1717 nullb->q = blk_mq_init_queue(nullb->tag_set);
1718 if (IS_ERR(nullb->q)) {
1719 rv = -ENOMEM;
1720 goto out_cleanup_tags;
1722 null_init_queues(nullb);
1723 } else if (dev->queue_mode == NULL_Q_BIO) {
1724 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
1725 if (!nullb->q) {
1726 rv = -ENOMEM;
1727 goto out_cleanup_queues;
1729 blk_queue_make_request(nullb->q, null_queue_bio);
1730 rv = init_driver_queues(nullb);
1731 if (rv)
1732 goto out_cleanup_blk_queue;
1735 if (dev->mbps) {
1736 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1737 nullb_setup_bwtimer(nullb);
1740 if (dev->cache_size > 0) {
1741 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1742 blk_queue_write_cache(nullb->q, true, true);
1745 if (dev->zoned) {
1746 rv = null_zone_init(dev);
1747 if (rv)
1748 goto out_cleanup_blk_queue;
1750 nullb->q->limits.zoned = BLK_ZONED_HM;
1751 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, nullb->q);
1752 blk_queue_required_elevator_features(nullb->q,
1753 ELEVATOR_F_ZBD_SEQ_WRITE);
1756 nullb->q->queuedata = nullb;
1757 blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
1758 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
1760 mutex_lock(&lock);
1761 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
1762 dev->index = nullb->index;
1763 mutex_unlock(&lock);
1765 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1766 blk_queue_physical_block_size(nullb->q, dev->blocksize);
1768 null_config_discard(nullb);
1770 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1772 rv = null_gendisk_register(nullb);
1773 if (rv)
1774 goto out_cleanup_zone;
1776 mutex_lock(&lock);
1777 list_add_tail(&nullb->list, &nullb_list);
1778 mutex_unlock(&lock);
1780 return 0;
1781 out_cleanup_zone:
1782 if (dev->zoned)
1783 null_zone_exit(dev);
1784 out_cleanup_blk_queue:
1785 blk_cleanup_queue(nullb->q);
1786 out_cleanup_tags:
1787 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
1788 blk_mq_free_tag_set(nullb->tag_set);
1789 out_cleanup_queues:
1790 cleanup_queues(nullb);
1791 out_free_nullb:
1792 kfree(nullb);
1793 out:
1794 return rv;
1797 static int __init null_init(void)
1799 int ret = 0;
1800 unsigned int i;
1801 struct nullb *nullb;
1802 struct nullb_device *dev;
1804 if (g_bs > PAGE_SIZE) {
1805 pr_warn("invalid block size\n");
1806 pr_warn("defaults block size to %lu\n", PAGE_SIZE);
1807 g_bs = PAGE_SIZE;
1810 if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) {
1811 pr_err("invalid home_node value\n");
1812 g_home_node = NUMA_NO_NODE;
1815 if (g_queue_mode == NULL_Q_RQ) {
1816 pr_err("legacy IO path no longer available\n");
1817 return -EINVAL;
1819 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1820 if (g_submit_queues != nr_online_nodes) {
1821 pr_warn("submit_queues param is set to %u.\n",
1822 nr_online_nodes);
1823 g_submit_queues = nr_online_nodes;
1825 } else if (g_submit_queues > nr_cpu_ids)
1826 g_submit_queues = nr_cpu_ids;
1827 else if (g_submit_queues <= 0)
1828 g_submit_queues = 1;
1830 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1831 ret = null_init_tag_set(NULL, &tag_set);
1832 if (ret)
1833 return ret;
1836 config_group_init(&nullb_subsys.su_group);
1837 mutex_init(&nullb_subsys.su_mutex);
1839 ret = configfs_register_subsystem(&nullb_subsys);
1840 if (ret)
1841 goto err_tagset;
1843 mutex_init(&lock);
1845 null_major = register_blkdev(0, "nullb");
1846 if (null_major < 0) {
1847 ret = null_major;
1848 goto err_conf;
1851 for (i = 0; i < nr_devices; i++) {
1852 dev = null_alloc_dev();
1853 if (!dev) {
1854 ret = -ENOMEM;
1855 goto err_dev;
1857 ret = null_add_dev(dev);
1858 if (ret) {
1859 null_free_dev(dev);
1860 goto err_dev;
1864 pr_info("module loaded\n");
1865 return 0;
1867 err_dev:
1868 while (!list_empty(&nullb_list)) {
1869 nullb = list_entry(nullb_list.next, struct nullb, list);
1870 dev = nullb->dev;
1871 null_del_dev(nullb);
1872 null_free_dev(dev);
1874 unregister_blkdev(null_major, "nullb");
1875 err_conf:
1876 configfs_unregister_subsystem(&nullb_subsys);
1877 err_tagset:
1878 if (g_queue_mode == NULL_Q_MQ && shared_tags)
1879 blk_mq_free_tag_set(&tag_set);
1880 return ret;
1883 static void __exit null_exit(void)
1885 struct nullb *nullb;
1887 configfs_unregister_subsystem(&nullb_subsys);
1889 unregister_blkdev(null_major, "nullb");
1891 mutex_lock(&lock);
1892 while (!list_empty(&nullb_list)) {
1893 struct nullb_device *dev;
1895 nullb = list_entry(nullb_list.next, struct nullb, list);
1896 dev = nullb->dev;
1897 null_del_dev(nullb);
1898 null_free_dev(dev);
1900 mutex_unlock(&lock);
1902 if (g_queue_mode == NULL_Q_MQ && shared_tags)
1903 blk_mq_free_tag_set(&tag_set);
1906 module_init(null_init);
1907 module_exit(null_exit);
1909 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
1910 MODULE_LICENSE("GPL");