1 // SPDX-License-Identifier: GPL-2.0-only
3 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
4 * Shaohua Li <shli@fb.com>
6 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/sched.h>
11 #include <linux/init.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)
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 static DECLARE_FAULT_ATTR(null_init_hctx_attr
);
29 static inline u64
mb_per_tick(int mbps
)
31 return (1 << 20) / TICKS_PER_SEC
* ((u64
) mbps
);
35 * Status flags for nullb_device.
37 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
38 * UP: Device is currently on and visible in userspace.
39 * THROTTLED: Device is being throttled.
40 * CACHE: Device is using a write-back cache.
42 enum nullb_device_flags
{
43 NULLB_DEV_FL_CONFIGURED
= 0,
45 NULLB_DEV_FL_THROTTLED
= 2,
46 NULLB_DEV_FL_CACHE
= 3,
49 #define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
51 * nullb_page is a page in memory for nullb devices.
53 * @page: The page holding the data.
54 * @bitmap: The bitmap represents which sector in the page has data.
55 * Each bit represents one block size. For example, sector 8
56 * will use the 7th bit
57 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
58 * page is being flushing to storage. FREE means the cache page is freed and
59 * should be skipped from flushing to storage. Please see
60 * null_make_cache_space
64 DECLARE_BITMAP(bitmap
, MAP_SZ
);
66 #define NULLB_PAGE_LOCK (MAP_SZ - 1)
67 #define NULLB_PAGE_FREE (MAP_SZ - 2)
69 static LIST_HEAD(nullb_list
);
70 static struct mutex lock
;
71 static int null_major
;
72 static DEFINE_IDA(nullb_indexes
);
73 static struct blk_mq_tag_set tag_set
;
87 static int g_no_sched
;
88 module_param_named(no_sched
, g_no_sched
, int, 0444);
89 MODULE_PARM_DESC(no_sched
, "No io scheduler");
91 static int g_submit_queues
= 1;
92 module_param_named(submit_queues
, g_submit_queues
, int, 0444);
93 MODULE_PARM_DESC(submit_queues
, "Number of submission queues");
95 static int g_home_node
= NUMA_NO_NODE
;
96 module_param_named(home_node
, g_home_node
, int, 0444);
97 MODULE_PARM_DESC(home_node
, "Home node for the device");
99 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
101 * For more details about fault injection, please refer to
102 * Documentation/fault-injection/fault-injection.rst.
104 static char g_timeout_str
[80];
105 module_param_string(timeout
, g_timeout_str
, sizeof(g_timeout_str
), 0444);
106 MODULE_PARM_DESC(timeout
, "Fault injection. timeout=<interval>,<probability>,<space>,<times>");
108 static char g_requeue_str
[80];
109 module_param_string(requeue
, g_requeue_str
, sizeof(g_requeue_str
), 0444);
110 MODULE_PARM_DESC(requeue
, "Fault injection. requeue=<interval>,<probability>,<space>,<times>");
112 static char g_init_hctx_str
[80];
113 module_param_string(init_hctx
, g_init_hctx_str
, sizeof(g_init_hctx_str
), 0444);
114 MODULE_PARM_DESC(init_hctx
, "Fault injection to fail hctx init. init_hctx=<interval>,<probability>,<space>,<times>");
117 static int g_queue_mode
= NULL_Q_MQ
;
119 static int null_param_store_val(const char *str
, int *val
, int min
, int max
)
123 ret
= kstrtoint(str
, 10, &new_val
);
127 if (new_val
< min
|| new_val
> max
)
134 static int null_set_queue_mode(const char *str
, const struct kernel_param
*kp
)
136 return null_param_store_val(str
, &g_queue_mode
, NULL_Q_BIO
, NULL_Q_MQ
);
139 static const struct kernel_param_ops null_queue_mode_param_ops
= {
140 .set
= null_set_queue_mode
,
141 .get
= param_get_int
,
144 device_param_cb(queue_mode
, &null_queue_mode_param_ops
, &g_queue_mode
, 0444);
145 MODULE_PARM_DESC(queue_mode
, "Block interface to use (0=bio,1=rq,2=multiqueue)");
147 static int g_gb
= 250;
148 module_param_named(gb
, g_gb
, int, 0444);
149 MODULE_PARM_DESC(gb
, "Size in GB");
151 static int g_bs
= 512;
152 module_param_named(bs
, g_bs
, int, 0444);
153 MODULE_PARM_DESC(bs
, "Block size (in bytes)");
155 static unsigned int nr_devices
= 1;
156 module_param(nr_devices
, uint
, 0444);
157 MODULE_PARM_DESC(nr_devices
, "Number of devices to register");
159 static bool g_blocking
;
160 module_param_named(blocking
, g_blocking
, bool, 0444);
161 MODULE_PARM_DESC(blocking
, "Register as a blocking blk-mq driver device");
163 static bool shared_tags
;
164 module_param(shared_tags
, bool, 0444);
165 MODULE_PARM_DESC(shared_tags
, "Share tag set between devices for blk-mq");
167 static int g_irqmode
= NULL_IRQ_SOFTIRQ
;
169 static int null_set_irqmode(const char *str
, const struct kernel_param
*kp
)
171 return null_param_store_val(str
, &g_irqmode
, NULL_IRQ_NONE
,
175 static const struct kernel_param_ops null_irqmode_param_ops
= {
176 .set
= null_set_irqmode
,
177 .get
= param_get_int
,
180 device_param_cb(irqmode
, &null_irqmode_param_ops
, &g_irqmode
, 0444);
181 MODULE_PARM_DESC(irqmode
, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
183 static unsigned long g_completion_nsec
= 10000;
184 module_param_named(completion_nsec
, g_completion_nsec
, ulong
, 0444);
185 MODULE_PARM_DESC(completion_nsec
, "Time in ns to complete a request in hardware. Default: 10,000ns");
187 static int g_hw_queue_depth
= 64;
188 module_param_named(hw_queue_depth
, g_hw_queue_depth
, int, 0444);
189 MODULE_PARM_DESC(hw_queue_depth
, "Queue depth for each hardware queue. Default: 64");
191 static bool g_use_per_node_hctx
;
192 module_param_named(use_per_node_hctx
, g_use_per_node_hctx
, bool, 0444);
193 MODULE_PARM_DESC(use_per_node_hctx
, "Use per-node allocation for hardware context queues. Default: false");
196 module_param_named(zoned
, g_zoned
, bool, S_IRUGO
);
197 MODULE_PARM_DESC(zoned
, "Make device as a host-managed zoned block device. Default: false");
199 static unsigned long g_zone_size
= 256;
200 module_param_named(zone_size
, g_zone_size
, ulong
, S_IRUGO
);
201 MODULE_PARM_DESC(zone_size
, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
203 static unsigned long g_zone_capacity
;
204 module_param_named(zone_capacity
, g_zone_capacity
, ulong
, 0444);
205 MODULE_PARM_DESC(zone_capacity
, "Zone capacity in MB when block device is zoned. Can be less than or equal to zone size. Default: Zone size");
207 static unsigned int g_zone_nr_conv
;
208 module_param_named(zone_nr_conv
, g_zone_nr_conv
, uint
, 0444);
209 MODULE_PARM_DESC(zone_nr_conv
, "Number of conventional zones when block device is zoned. Default: 0");
211 static struct nullb_device
*null_alloc_dev(void);
212 static void null_free_dev(struct nullb_device
*dev
);
213 static void null_del_dev(struct nullb
*nullb
);
214 static int null_add_dev(struct nullb_device
*dev
);
215 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
);
217 static inline struct nullb_device
*to_nullb_device(struct config_item
*item
)
219 return item
? container_of(item
, struct nullb_device
, item
) : NULL
;
222 static inline ssize_t
nullb_device_uint_attr_show(unsigned int val
, char *page
)
224 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
227 static inline ssize_t
nullb_device_ulong_attr_show(unsigned long val
,
230 return snprintf(page
, PAGE_SIZE
, "%lu\n", val
);
233 static inline ssize_t
nullb_device_bool_attr_show(bool val
, char *page
)
235 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
238 static ssize_t
nullb_device_uint_attr_store(unsigned int *val
,
239 const char *page
, size_t count
)
244 result
= kstrtouint(page
, 0, &tmp
);
252 static ssize_t
nullb_device_ulong_attr_store(unsigned long *val
,
253 const char *page
, size_t count
)
258 result
= kstrtoul(page
, 0, &tmp
);
266 static ssize_t
nullb_device_bool_attr_store(bool *val
, const char *page
,
272 result
= kstrtobool(page
, &tmp
);
280 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
281 #define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY) \
283 nullb_device_##NAME##_show(struct config_item *item, char *page) \
285 return nullb_device_##TYPE##_attr_show( \
286 to_nullb_device(item)->NAME, page); \
289 nullb_device_##NAME##_store(struct config_item *item, const char *page, \
292 int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
293 struct nullb_device *dev = to_nullb_device(item); \
294 TYPE new_value = 0; \
297 ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
301 ret = apply_fn(dev, new_value); \
302 else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) \
306 dev->NAME = new_value; \
309 CONFIGFS_ATTR(nullb_device_, NAME);
311 static int nullb_apply_submit_queues(struct nullb_device
*dev
,
312 unsigned int submit_queues
)
314 struct nullb
*nullb
= dev
->nullb
;
315 struct blk_mq_tag_set
*set
;
321 * Make sure that null_init_hctx() does not access nullb->queues[] past
322 * the end of that array.
324 if (submit_queues
> nr_cpu_ids
)
326 set
= nullb
->tag_set
;
327 blk_mq_update_nr_hw_queues(set
, submit_queues
);
328 return set
->nr_hw_queues
== submit_queues
? 0 : -ENOMEM
;
331 NULLB_DEVICE_ATTR(size
, ulong
, NULL
);
332 NULLB_DEVICE_ATTR(completion_nsec
, ulong
, NULL
);
333 NULLB_DEVICE_ATTR(submit_queues
, uint
, nullb_apply_submit_queues
);
334 NULLB_DEVICE_ATTR(home_node
, uint
, NULL
);
335 NULLB_DEVICE_ATTR(queue_mode
, uint
, NULL
);
336 NULLB_DEVICE_ATTR(blocksize
, uint
, NULL
);
337 NULLB_DEVICE_ATTR(irqmode
, uint
, NULL
);
338 NULLB_DEVICE_ATTR(hw_queue_depth
, uint
, NULL
);
339 NULLB_DEVICE_ATTR(index
, uint
, NULL
);
340 NULLB_DEVICE_ATTR(blocking
, bool, NULL
);
341 NULLB_DEVICE_ATTR(use_per_node_hctx
, bool, NULL
);
342 NULLB_DEVICE_ATTR(memory_backed
, bool, NULL
);
343 NULLB_DEVICE_ATTR(discard
, bool, NULL
);
344 NULLB_DEVICE_ATTR(mbps
, uint
, NULL
);
345 NULLB_DEVICE_ATTR(cache_size
, ulong
, NULL
);
346 NULLB_DEVICE_ATTR(zoned
, bool, NULL
);
347 NULLB_DEVICE_ATTR(zone_size
, ulong
, NULL
);
348 NULLB_DEVICE_ATTR(zone_capacity
, ulong
, NULL
);
349 NULLB_DEVICE_ATTR(zone_nr_conv
, uint
, NULL
);
351 static ssize_t
nullb_device_power_show(struct config_item
*item
, char *page
)
353 return nullb_device_bool_attr_show(to_nullb_device(item
)->power
, page
);
356 static ssize_t
nullb_device_power_store(struct config_item
*item
,
357 const char *page
, size_t count
)
359 struct nullb_device
*dev
= to_nullb_device(item
);
363 ret
= nullb_device_bool_attr_store(&newp
, page
, count
);
367 if (!dev
->power
&& newp
) {
368 if (test_and_set_bit(NULLB_DEV_FL_UP
, &dev
->flags
))
370 if (null_add_dev(dev
)) {
371 clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
);
375 set_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
377 } else if (dev
->power
&& !newp
) {
378 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
381 null_del_dev(dev
->nullb
);
384 clear_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
390 CONFIGFS_ATTR(nullb_device_
, power
);
392 static ssize_t
nullb_device_badblocks_show(struct config_item
*item
, char *page
)
394 struct nullb_device
*t_dev
= to_nullb_device(item
);
396 return badblocks_show(&t_dev
->badblocks
, page
, 0);
399 static ssize_t
nullb_device_badblocks_store(struct config_item
*item
,
400 const char *page
, size_t count
)
402 struct nullb_device
*t_dev
= to_nullb_device(item
);
403 char *orig
, *buf
, *tmp
;
407 orig
= kstrndup(page
, count
, GFP_KERNEL
);
411 buf
= strstrip(orig
);
414 if (buf
[0] != '+' && buf
[0] != '-')
416 tmp
= strchr(&buf
[1], '-');
420 ret
= kstrtoull(buf
+ 1, 0, &start
);
423 ret
= kstrtoull(tmp
+ 1, 0, &end
);
429 /* enable badblocks */
430 cmpxchg(&t_dev
->badblocks
.shift
, -1, 0);
432 ret
= badblocks_set(&t_dev
->badblocks
, start
,
435 ret
= badblocks_clear(&t_dev
->badblocks
, start
,
443 CONFIGFS_ATTR(nullb_device_
, badblocks
);
445 static struct configfs_attribute
*nullb_device_attrs
[] = {
446 &nullb_device_attr_size
,
447 &nullb_device_attr_completion_nsec
,
448 &nullb_device_attr_submit_queues
,
449 &nullb_device_attr_home_node
,
450 &nullb_device_attr_queue_mode
,
451 &nullb_device_attr_blocksize
,
452 &nullb_device_attr_irqmode
,
453 &nullb_device_attr_hw_queue_depth
,
454 &nullb_device_attr_index
,
455 &nullb_device_attr_blocking
,
456 &nullb_device_attr_use_per_node_hctx
,
457 &nullb_device_attr_power
,
458 &nullb_device_attr_memory_backed
,
459 &nullb_device_attr_discard
,
460 &nullb_device_attr_mbps
,
461 &nullb_device_attr_cache_size
,
462 &nullb_device_attr_badblocks
,
463 &nullb_device_attr_zoned
,
464 &nullb_device_attr_zone_size
,
465 &nullb_device_attr_zone_capacity
,
466 &nullb_device_attr_zone_nr_conv
,
470 static void nullb_device_release(struct config_item
*item
)
472 struct nullb_device
*dev
= to_nullb_device(item
);
474 null_free_device_storage(dev
, false);
478 static struct configfs_item_operations nullb_device_ops
= {
479 .release
= nullb_device_release
,
482 static const struct config_item_type nullb_device_type
= {
483 .ct_item_ops
= &nullb_device_ops
,
484 .ct_attrs
= nullb_device_attrs
,
485 .ct_owner
= THIS_MODULE
,
489 config_item
*nullb_group_make_item(struct config_group
*group
, const char *name
)
491 struct nullb_device
*dev
;
493 dev
= null_alloc_dev();
495 return ERR_PTR(-ENOMEM
);
497 config_item_init_type_name(&dev
->item
, name
, &nullb_device_type
);
503 nullb_group_drop_item(struct config_group
*group
, struct config_item
*item
)
505 struct nullb_device
*dev
= to_nullb_device(item
);
507 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
510 null_del_dev(dev
->nullb
);
514 config_item_put(item
);
517 static ssize_t
memb_group_features_show(struct config_item
*item
, char *page
)
519 return snprintf(page
, PAGE_SIZE
,
520 "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size,zone_capacity,zone_nr_conv\n");
523 CONFIGFS_ATTR_RO(memb_group_
, features
);
525 static struct configfs_attribute
*nullb_group_attrs
[] = {
526 &memb_group_attr_features
,
530 static struct configfs_group_operations nullb_group_ops
= {
531 .make_item
= nullb_group_make_item
,
532 .drop_item
= nullb_group_drop_item
,
535 static const struct config_item_type nullb_group_type
= {
536 .ct_group_ops
= &nullb_group_ops
,
537 .ct_attrs
= nullb_group_attrs
,
538 .ct_owner
= THIS_MODULE
,
541 static struct configfs_subsystem nullb_subsys
= {
544 .ci_namebuf
= "nullb",
545 .ci_type
= &nullb_group_type
,
550 static inline int null_cache_active(struct nullb
*nullb
)
552 return test_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
555 static struct nullb_device
*null_alloc_dev(void)
557 struct nullb_device
*dev
;
559 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
562 INIT_RADIX_TREE(&dev
->data
, GFP_ATOMIC
);
563 INIT_RADIX_TREE(&dev
->cache
, GFP_ATOMIC
);
564 if (badblocks_init(&dev
->badblocks
, 0)) {
569 dev
->size
= g_gb
* 1024;
570 dev
->completion_nsec
= g_completion_nsec
;
571 dev
->submit_queues
= g_submit_queues
;
572 dev
->home_node
= g_home_node
;
573 dev
->queue_mode
= g_queue_mode
;
574 dev
->blocksize
= g_bs
;
575 dev
->irqmode
= g_irqmode
;
576 dev
->hw_queue_depth
= g_hw_queue_depth
;
577 dev
->blocking
= g_blocking
;
578 dev
->use_per_node_hctx
= g_use_per_node_hctx
;
579 dev
->zoned
= g_zoned
;
580 dev
->zone_size
= g_zone_size
;
581 dev
->zone_capacity
= g_zone_capacity
;
582 dev
->zone_nr_conv
= g_zone_nr_conv
;
586 static void null_free_dev(struct nullb_device
*dev
)
591 null_free_zoned_dev(dev
);
592 badblocks_exit(&dev
->badblocks
);
596 static void put_tag(struct nullb_queue
*nq
, unsigned int tag
)
598 clear_bit_unlock(tag
, nq
->tag_map
);
600 if (waitqueue_active(&nq
->wait
))
604 static unsigned int get_tag(struct nullb_queue
*nq
)
609 tag
= find_first_zero_bit(nq
->tag_map
, nq
->queue_depth
);
610 if (tag
>= nq
->queue_depth
)
612 } while (test_and_set_bit_lock(tag
, nq
->tag_map
));
617 static void free_cmd(struct nullb_cmd
*cmd
)
619 put_tag(cmd
->nq
, cmd
->tag
);
622 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
);
624 static struct nullb_cmd
*__alloc_cmd(struct nullb_queue
*nq
)
626 struct nullb_cmd
*cmd
;
631 cmd
= &nq
->cmds
[tag
];
633 cmd
->error
= BLK_STS_OK
;
635 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
636 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
,
638 cmd
->timer
.function
= null_cmd_timer_expired
;
646 static struct nullb_cmd
*alloc_cmd(struct nullb_queue
*nq
, int can_wait
)
648 struct nullb_cmd
*cmd
;
651 cmd
= __alloc_cmd(nq
);
652 if (cmd
|| !can_wait
)
656 prepare_to_wait(&nq
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
657 cmd
= __alloc_cmd(nq
);
664 finish_wait(&nq
->wait
, &wait
);
668 static void end_cmd(struct nullb_cmd
*cmd
)
670 int queue_mode
= cmd
->nq
->dev
->queue_mode
;
672 switch (queue_mode
) {
674 blk_mq_end_request(cmd
->rq
, cmd
->error
);
677 cmd
->bio
->bi_status
= cmd
->error
;
685 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
)
687 end_cmd(container_of(timer
, struct nullb_cmd
, timer
));
689 return HRTIMER_NORESTART
;
692 static void null_cmd_end_timer(struct nullb_cmd
*cmd
)
694 ktime_t kt
= cmd
->nq
->dev
->completion_nsec
;
696 hrtimer_start(&cmd
->timer
, kt
, HRTIMER_MODE_REL
);
699 static void null_complete_rq(struct request
*rq
)
701 end_cmd(blk_mq_rq_to_pdu(rq
));
704 static struct nullb_page
*null_alloc_page(gfp_t gfp_flags
)
706 struct nullb_page
*t_page
;
708 t_page
= kmalloc(sizeof(struct nullb_page
), gfp_flags
);
712 t_page
->page
= alloc_pages(gfp_flags
, 0);
716 memset(t_page
->bitmap
, 0, sizeof(t_page
->bitmap
));
724 static void null_free_page(struct nullb_page
*t_page
)
726 __set_bit(NULLB_PAGE_FREE
, t_page
->bitmap
);
727 if (test_bit(NULLB_PAGE_LOCK
, t_page
->bitmap
))
729 __free_page(t_page
->page
);
733 static bool null_page_empty(struct nullb_page
*page
)
735 int size
= MAP_SZ
- 2;
737 return find_first_bit(page
->bitmap
, size
) == size
;
740 static void null_free_sector(struct nullb
*nullb
, sector_t sector
,
743 unsigned int sector_bit
;
745 struct nullb_page
*t_page
, *ret
;
746 struct radix_tree_root
*root
;
748 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
749 idx
= sector
>> PAGE_SECTORS_SHIFT
;
750 sector_bit
= (sector
& SECTOR_MASK
);
752 t_page
= radix_tree_lookup(root
, idx
);
754 __clear_bit(sector_bit
, t_page
->bitmap
);
756 if (null_page_empty(t_page
)) {
757 ret
= radix_tree_delete_item(root
, idx
, t_page
);
758 WARN_ON(ret
!= t_page
);
761 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
766 static struct nullb_page
*null_radix_tree_insert(struct nullb
*nullb
, u64 idx
,
767 struct nullb_page
*t_page
, bool is_cache
)
769 struct radix_tree_root
*root
;
771 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
773 if (radix_tree_insert(root
, idx
, t_page
)) {
774 null_free_page(t_page
);
775 t_page
= radix_tree_lookup(root
, idx
);
776 WARN_ON(!t_page
|| t_page
->page
->index
!= idx
);
778 nullb
->dev
->curr_cache
+= PAGE_SIZE
;
783 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
)
785 unsigned long pos
= 0;
787 struct nullb_page
*ret
, *t_pages
[FREE_BATCH
];
788 struct radix_tree_root
*root
;
790 root
= is_cache
? &dev
->cache
: &dev
->data
;
795 nr_pages
= radix_tree_gang_lookup(root
,
796 (void **)t_pages
, pos
, FREE_BATCH
);
798 for (i
= 0; i
< nr_pages
; i
++) {
799 pos
= t_pages
[i
]->page
->index
;
800 ret
= radix_tree_delete_item(root
, pos
, t_pages
[i
]);
801 WARN_ON(ret
!= t_pages
[i
]);
806 } while (nr_pages
== FREE_BATCH
);
812 static struct nullb_page
*__null_lookup_page(struct nullb
*nullb
,
813 sector_t sector
, bool for_write
, bool is_cache
)
815 unsigned int sector_bit
;
817 struct nullb_page
*t_page
;
818 struct radix_tree_root
*root
;
820 idx
= sector
>> PAGE_SECTORS_SHIFT
;
821 sector_bit
= (sector
& SECTOR_MASK
);
823 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
824 t_page
= radix_tree_lookup(root
, idx
);
825 WARN_ON(t_page
&& t_page
->page
->index
!= idx
);
827 if (t_page
&& (for_write
|| test_bit(sector_bit
, t_page
->bitmap
)))
833 static struct nullb_page
*null_lookup_page(struct nullb
*nullb
,
834 sector_t sector
, bool for_write
, bool ignore_cache
)
836 struct nullb_page
*page
= NULL
;
839 page
= __null_lookup_page(nullb
, sector
, for_write
, true);
842 return __null_lookup_page(nullb
, sector
, for_write
, false);
845 static struct nullb_page
*null_insert_page(struct nullb
*nullb
,
846 sector_t sector
, bool ignore_cache
)
847 __releases(&nullb
->lock
)
848 __acquires(&nullb
->lock
)
851 struct nullb_page
*t_page
;
853 t_page
= null_lookup_page(nullb
, sector
, true, ignore_cache
);
857 spin_unlock_irq(&nullb
->lock
);
859 t_page
= null_alloc_page(GFP_NOIO
);
863 if (radix_tree_preload(GFP_NOIO
))
866 spin_lock_irq(&nullb
->lock
);
867 idx
= sector
>> PAGE_SECTORS_SHIFT
;
868 t_page
->page
->index
= idx
;
869 t_page
= null_radix_tree_insert(nullb
, idx
, t_page
, !ignore_cache
);
870 radix_tree_preload_end();
874 null_free_page(t_page
);
876 spin_lock_irq(&nullb
->lock
);
877 return null_lookup_page(nullb
, sector
, true, ignore_cache
);
880 static int null_flush_cache_page(struct nullb
*nullb
, struct nullb_page
*c_page
)
885 struct nullb_page
*t_page
, *ret
;
888 idx
= c_page
->page
->index
;
890 t_page
= null_insert_page(nullb
, idx
<< PAGE_SECTORS_SHIFT
, true);
892 __clear_bit(NULLB_PAGE_LOCK
, c_page
->bitmap
);
893 if (test_bit(NULLB_PAGE_FREE
, c_page
->bitmap
)) {
894 null_free_page(c_page
);
895 if (t_page
&& null_page_empty(t_page
)) {
896 ret
= radix_tree_delete_item(&nullb
->dev
->data
,
898 null_free_page(t_page
);
906 src
= kmap_atomic(c_page
->page
);
907 dst
= kmap_atomic(t_page
->page
);
909 for (i
= 0; i
< PAGE_SECTORS
;
910 i
+= (nullb
->dev
->blocksize
>> SECTOR_SHIFT
)) {
911 if (test_bit(i
, c_page
->bitmap
)) {
912 offset
= (i
<< SECTOR_SHIFT
);
913 memcpy(dst
+ offset
, src
+ offset
,
914 nullb
->dev
->blocksize
);
915 __set_bit(i
, t_page
->bitmap
);
922 ret
= radix_tree_delete_item(&nullb
->dev
->cache
, idx
, c_page
);
924 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
929 static int null_make_cache_space(struct nullb
*nullb
, unsigned long n
)
931 int i
, err
, nr_pages
;
932 struct nullb_page
*c_pages
[FREE_BATCH
];
933 unsigned long flushed
= 0, one_round
;
936 if ((nullb
->dev
->cache_size
* 1024 * 1024) >
937 nullb
->dev
->curr_cache
+ n
|| nullb
->dev
->curr_cache
== 0)
940 nr_pages
= radix_tree_gang_lookup(&nullb
->dev
->cache
,
941 (void **)c_pages
, nullb
->cache_flush_pos
, FREE_BATCH
);
943 * nullb_flush_cache_page could unlock before using the c_pages. To
944 * avoid race, we don't allow page free
946 for (i
= 0; i
< nr_pages
; i
++) {
947 nullb
->cache_flush_pos
= c_pages
[i
]->page
->index
;
949 * We found the page which is being flushed to disk by other
952 if (test_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
))
955 __set_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
);
959 for (i
= 0; i
< nr_pages
; i
++) {
960 if (c_pages
[i
] == NULL
)
962 err
= null_flush_cache_page(nullb
, c_pages
[i
]);
967 flushed
+= one_round
<< PAGE_SHIFT
;
971 nullb
->cache_flush_pos
= 0;
972 if (one_round
== 0) {
973 /* give other threads a chance */
974 spin_unlock_irq(&nullb
->lock
);
975 spin_lock_irq(&nullb
->lock
);
982 static int copy_to_nullb(struct nullb
*nullb
, struct page
*source
,
983 unsigned int off
, sector_t sector
, size_t n
, bool is_fua
)
985 size_t temp
, count
= 0;
987 struct nullb_page
*t_page
;
991 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
993 if (null_cache_active(nullb
) && !is_fua
)
994 null_make_cache_space(nullb
, PAGE_SIZE
);
996 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
997 t_page
= null_insert_page(nullb
, sector
,
998 !null_cache_active(nullb
) || is_fua
);
1002 src
= kmap_atomic(source
);
1003 dst
= kmap_atomic(t_page
->page
);
1004 memcpy(dst
+ offset
, src
+ off
+ count
, temp
);
1008 __set_bit(sector
& SECTOR_MASK
, t_page
->bitmap
);
1011 null_free_sector(nullb
, sector
, true);
1014 sector
+= temp
>> SECTOR_SHIFT
;
1019 static int copy_from_nullb(struct nullb
*nullb
, struct page
*dest
,
1020 unsigned int off
, sector_t sector
, size_t n
)
1022 size_t temp
, count
= 0;
1023 unsigned int offset
;
1024 struct nullb_page
*t_page
;
1028 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
1030 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
1031 t_page
= null_lookup_page(nullb
, sector
, false,
1032 !null_cache_active(nullb
));
1034 dst
= kmap_atomic(dest
);
1036 memset(dst
+ off
+ count
, 0, temp
);
1039 src
= kmap_atomic(t_page
->page
);
1040 memcpy(dst
+ off
+ count
, src
+ offset
, temp
);
1046 sector
+= temp
>> SECTOR_SHIFT
;
1051 static void nullb_fill_pattern(struct nullb
*nullb
, struct page
*page
,
1052 unsigned int len
, unsigned int off
)
1056 dst
= kmap_atomic(page
);
1057 memset(dst
+ off
, 0xFF, len
);
1061 static void null_handle_discard(struct nullb
*nullb
, sector_t sector
, size_t n
)
1065 spin_lock_irq(&nullb
->lock
);
1067 temp
= min_t(size_t, n
, nullb
->dev
->blocksize
);
1068 null_free_sector(nullb
, sector
, false);
1069 if (null_cache_active(nullb
))
1070 null_free_sector(nullb
, sector
, true);
1071 sector
+= temp
>> SECTOR_SHIFT
;
1074 spin_unlock_irq(&nullb
->lock
);
1077 static int null_handle_flush(struct nullb
*nullb
)
1081 if (!null_cache_active(nullb
))
1084 spin_lock_irq(&nullb
->lock
);
1086 err
= null_make_cache_space(nullb
,
1087 nullb
->dev
->cache_size
* 1024 * 1024);
1088 if (err
|| nullb
->dev
->curr_cache
== 0)
1092 WARN_ON(!radix_tree_empty(&nullb
->dev
->cache
));
1093 spin_unlock_irq(&nullb
->lock
);
1097 static int null_transfer(struct nullb
*nullb
, struct page
*page
,
1098 unsigned int len
, unsigned int off
, bool is_write
, sector_t sector
,
1101 struct nullb_device
*dev
= nullb
->dev
;
1102 unsigned int valid_len
= len
;
1107 valid_len
= null_zone_valid_read_len(nullb
,
1111 err
= copy_from_nullb(nullb
, page
, off
,
1118 nullb_fill_pattern(nullb
, page
, len
, off
);
1119 flush_dcache_page(page
);
1121 flush_dcache_page(page
);
1122 err
= copy_to_nullb(nullb
, page
, off
, sector
, len
, is_fua
);
1128 static int null_handle_rq(struct nullb_cmd
*cmd
)
1130 struct request
*rq
= cmd
->rq
;
1131 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1135 struct req_iterator iter
;
1136 struct bio_vec bvec
;
1138 sector
= blk_rq_pos(rq
);
1140 if (req_op(rq
) == REQ_OP_DISCARD
) {
1141 null_handle_discard(nullb
, sector
, blk_rq_bytes(rq
));
1145 spin_lock_irq(&nullb
->lock
);
1146 rq_for_each_segment(bvec
, rq
, iter
) {
1148 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1149 op_is_write(req_op(rq
)), sector
,
1150 req_op(rq
) & REQ_FUA
);
1152 spin_unlock_irq(&nullb
->lock
);
1155 sector
+= len
>> SECTOR_SHIFT
;
1157 spin_unlock_irq(&nullb
->lock
);
1162 static int null_handle_bio(struct nullb_cmd
*cmd
)
1164 struct bio
*bio
= cmd
->bio
;
1165 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1169 struct bio_vec bvec
;
1170 struct bvec_iter iter
;
1172 sector
= bio
->bi_iter
.bi_sector
;
1174 if (bio_op(bio
) == REQ_OP_DISCARD
) {
1175 null_handle_discard(nullb
, sector
,
1176 bio_sectors(bio
) << SECTOR_SHIFT
);
1180 spin_lock_irq(&nullb
->lock
);
1181 bio_for_each_segment(bvec
, bio
, iter
) {
1183 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1184 op_is_write(bio_op(bio
)), sector
,
1185 bio
->bi_opf
& REQ_FUA
);
1187 spin_unlock_irq(&nullb
->lock
);
1190 sector
+= len
>> SECTOR_SHIFT
;
1192 spin_unlock_irq(&nullb
->lock
);
1196 static void null_stop_queue(struct nullb
*nullb
)
1198 struct request_queue
*q
= nullb
->q
;
1200 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1201 blk_mq_stop_hw_queues(q
);
1204 static void null_restart_queue_async(struct nullb
*nullb
)
1206 struct request_queue
*q
= nullb
->q
;
1208 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1209 blk_mq_start_stopped_hw_queues(q
, true);
1212 static inline blk_status_t
null_handle_throttled(struct nullb_cmd
*cmd
)
1214 struct nullb_device
*dev
= cmd
->nq
->dev
;
1215 struct nullb
*nullb
= dev
->nullb
;
1216 blk_status_t sts
= BLK_STS_OK
;
1217 struct request
*rq
= cmd
->rq
;
1219 if (!hrtimer_active(&nullb
->bw_timer
))
1220 hrtimer_restart(&nullb
->bw_timer
);
1222 if (atomic_long_sub_return(blk_rq_bytes(rq
), &nullb
->cur_bytes
) < 0) {
1223 null_stop_queue(nullb
);
1224 /* race with timer */
1225 if (atomic_long_read(&nullb
->cur_bytes
) > 0)
1226 null_restart_queue_async(nullb
);
1227 /* requeue request */
1228 sts
= BLK_STS_DEV_RESOURCE
;
1233 static inline blk_status_t
null_handle_badblocks(struct nullb_cmd
*cmd
,
1235 sector_t nr_sectors
)
1237 struct badblocks
*bb
= &cmd
->nq
->dev
->badblocks
;
1241 if (badblocks_check(bb
, sector
, nr_sectors
, &first_bad
, &bad_sectors
))
1242 return BLK_STS_IOERR
;
1247 static inline blk_status_t
null_handle_memory_backed(struct nullb_cmd
*cmd
,
1250 struct nullb_device
*dev
= cmd
->nq
->dev
;
1253 if (dev
->queue_mode
== NULL_Q_BIO
)
1254 err
= null_handle_bio(cmd
);
1256 err
= null_handle_rq(cmd
);
1258 return errno_to_blk_status(err
);
1261 static void nullb_zero_read_cmd_buffer(struct nullb_cmd
*cmd
)
1263 struct nullb_device
*dev
= cmd
->nq
->dev
;
1266 if (dev
->memory_backed
)
1269 if (dev
->queue_mode
== NULL_Q_BIO
&& bio_op(cmd
->bio
) == REQ_OP_READ
) {
1270 zero_fill_bio(cmd
->bio
);
1271 } else if (req_op(cmd
->rq
) == REQ_OP_READ
) {
1272 __rq_for_each_bio(bio
, cmd
->rq
)
1277 static inline void nullb_complete_cmd(struct nullb_cmd
*cmd
)
1280 * Since root privileges are required to configure the null_blk
1281 * driver, it is fine that this driver does not initialize the
1282 * data buffers of read commands. Zero-initialize these buffers
1283 * anyway if KMSAN is enabled to prevent that KMSAN complains
1284 * about null_blk not initializing read data buffers.
1286 if (IS_ENABLED(CONFIG_KMSAN
))
1287 nullb_zero_read_cmd_buffer(cmd
);
1289 /* Complete IO by inline, softirq or timer */
1290 switch (cmd
->nq
->dev
->irqmode
) {
1291 case NULL_IRQ_SOFTIRQ
:
1292 switch (cmd
->nq
->dev
->queue_mode
) {
1294 if (likely(!blk_should_fake_timeout(cmd
->rq
->q
)))
1295 blk_mq_complete_request(cmd
->rq
);
1299 * XXX: no proper submitting cpu information available.
1308 case NULL_IRQ_TIMER
:
1309 null_cmd_end_timer(cmd
);
1314 blk_status_t
null_process_cmd(struct nullb_cmd
*cmd
,
1315 enum req_opf op
, sector_t sector
,
1316 unsigned int nr_sectors
)
1318 struct nullb_device
*dev
= cmd
->nq
->dev
;
1321 if (dev
->badblocks
.shift
!= -1) {
1322 ret
= null_handle_badblocks(cmd
, sector
, nr_sectors
);
1323 if (ret
!= BLK_STS_OK
)
1327 if (dev
->memory_backed
)
1328 return null_handle_memory_backed(cmd
, op
);
1333 static blk_status_t
null_handle_cmd(struct nullb_cmd
*cmd
, sector_t sector
,
1334 sector_t nr_sectors
, enum req_opf op
)
1336 struct nullb_device
*dev
= cmd
->nq
->dev
;
1337 struct nullb
*nullb
= dev
->nullb
;
1340 if (test_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
)) {
1341 sts
= null_handle_throttled(cmd
);
1342 if (sts
!= BLK_STS_OK
)
1346 if (op
== REQ_OP_FLUSH
) {
1347 cmd
->error
= errno_to_blk_status(null_handle_flush(nullb
));
1352 cmd
->error
= null_process_zoned_cmd(cmd
, op
,
1353 sector
, nr_sectors
);
1355 cmd
->error
= null_process_cmd(cmd
, op
, sector
, nr_sectors
);
1358 nullb_complete_cmd(cmd
);
1362 static enum hrtimer_restart
nullb_bwtimer_fn(struct hrtimer
*timer
)
1364 struct nullb
*nullb
= container_of(timer
, struct nullb
, bw_timer
);
1365 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1366 unsigned int mbps
= nullb
->dev
->mbps
;
1368 if (atomic_long_read(&nullb
->cur_bytes
) == mb_per_tick(mbps
))
1369 return HRTIMER_NORESTART
;
1371 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(mbps
));
1372 null_restart_queue_async(nullb
);
1374 hrtimer_forward_now(&nullb
->bw_timer
, timer_interval
);
1376 return HRTIMER_RESTART
;
1379 static void nullb_setup_bwtimer(struct nullb
*nullb
)
1381 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1383 hrtimer_init(&nullb
->bw_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1384 nullb
->bw_timer
.function
= nullb_bwtimer_fn
;
1385 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(nullb
->dev
->mbps
));
1386 hrtimer_start(&nullb
->bw_timer
, timer_interval
, HRTIMER_MODE_REL
);
1389 static struct nullb_queue
*nullb_to_queue(struct nullb
*nullb
)
1393 if (nullb
->nr_queues
!= 1)
1394 index
= raw_smp_processor_id() / ((nr_cpu_ids
+ nullb
->nr_queues
- 1) / nullb
->nr_queues
);
1396 return &nullb
->queues
[index
];
1399 static blk_qc_t
null_submit_bio(struct bio
*bio
)
1401 sector_t sector
= bio
->bi_iter
.bi_sector
;
1402 sector_t nr_sectors
= bio_sectors(bio
);
1403 struct nullb
*nullb
= bio
->bi_disk
->private_data
;
1404 struct nullb_queue
*nq
= nullb_to_queue(nullb
);
1405 struct nullb_cmd
*cmd
;
1407 cmd
= alloc_cmd(nq
, 1);
1410 null_handle_cmd(cmd
, sector
, nr_sectors
, bio_op(bio
));
1411 return BLK_QC_T_NONE
;
1414 static bool should_timeout_request(struct request
*rq
)
1416 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1417 if (g_timeout_str
[0])
1418 return should_fail(&null_timeout_attr
, 1);
1423 static bool should_requeue_request(struct request
*rq
)
1425 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1426 if (g_requeue_str
[0])
1427 return should_fail(&null_requeue_attr
, 1);
1432 static enum blk_eh_timer_return
null_timeout_rq(struct request
*rq
, bool res
)
1434 pr_info("rq %p timed out\n", rq
);
1435 blk_mq_complete_request(rq
);
1439 static blk_status_t
null_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1440 const struct blk_mq_queue_data
*bd
)
1442 struct nullb_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
1443 struct nullb_queue
*nq
= hctx
->driver_data
;
1444 sector_t nr_sectors
= blk_rq_sectors(bd
->rq
);
1445 sector_t sector
= blk_rq_pos(bd
->rq
);
1447 might_sleep_if(hctx
->flags
& BLK_MQ_F_BLOCKING
);
1449 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
1450 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1451 cmd
->timer
.function
= null_cmd_timer_expired
;
1454 cmd
->error
= BLK_STS_OK
;
1457 blk_mq_start_request(bd
->rq
);
1459 if (should_requeue_request(bd
->rq
)) {
1461 * Alternate between hitting the core BUSY path, and the
1462 * driver driven requeue path
1464 nq
->requeue_selection
++;
1465 if (nq
->requeue_selection
& 1)
1466 return BLK_STS_RESOURCE
;
1468 blk_mq_requeue_request(bd
->rq
, true);
1472 if (should_timeout_request(bd
->rq
))
1475 return null_handle_cmd(cmd
, sector
, nr_sectors
, req_op(bd
->rq
));
1478 static void cleanup_queue(struct nullb_queue
*nq
)
1484 static void cleanup_queues(struct nullb
*nullb
)
1488 for (i
= 0; i
< nullb
->nr_queues
; i
++)
1489 cleanup_queue(&nullb
->queues
[i
]);
1491 kfree(nullb
->queues
);
1494 static void null_exit_hctx(struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1496 struct nullb_queue
*nq
= hctx
->driver_data
;
1497 struct nullb
*nullb
= nq
->dev
->nullb
;
1502 static void null_init_queue(struct nullb
*nullb
, struct nullb_queue
*nq
)
1504 init_waitqueue_head(&nq
->wait
);
1505 nq
->queue_depth
= nullb
->queue_depth
;
1506 nq
->dev
= nullb
->dev
;
1509 static int null_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *driver_data
,
1510 unsigned int hctx_idx
)
1512 struct nullb
*nullb
= hctx
->queue
->queuedata
;
1513 struct nullb_queue
*nq
;
1515 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1516 if (g_init_hctx_str
[0] && should_fail(&null_init_hctx_attr
, 1))
1520 nq
= &nullb
->queues
[hctx_idx
];
1521 hctx
->driver_data
= nq
;
1522 null_init_queue(nullb
, nq
);
1528 static const struct blk_mq_ops null_mq_ops
= {
1529 .queue_rq
= null_queue_rq
,
1530 .complete
= null_complete_rq
,
1531 .timeout
= null_timeout_rq
,
1532 .init_hctx
= null_init_hctx
,
1533 .exit_hctx
= null_exit_hctx
,
1536 static void null_del_dev(struct nullb
*nullb
)
1538 struct nullb_device
*dev
;
1545 ida_simple_remove(&nullb_indexes
, nullb
->index
);
1547 list_del_init(&nullb
->list
);
1549 del_gendisk(nullb
->disk
);
1551 if (test_bit(NULLB_DEV_FL_THROTTLED
, &nullb
->dev
->flags
)) {
1552 hrtimer_cancel(&nullb
->bw_timer
);
1553 atomic_long_set(&nullb
->cur_bytes
, LONG_MAX
);
1554 null_restart_queue_async(nullb
);
1557 blk_cleanup_queue(nullb
->q
);
1558 if (dev
->queue_mode
== NULL_Q_MQ
&&
1559 nullb
->tag_set
== &nullb
->__tag_set
)
1560 blk_mq_free_tag_set(nullb
->tag_set
);
1561 put_disk(nullb
->disk
);
1562 cleanup_queues(nullb
);
1563 if (null_cache_active(nullb
))
1564 null_free_device_storage(nullb
->dev
, true);
1569 static void null_config_discard(struct nullb
*nullb
)
1571 if (nullb
->dev
->discard
== false)
1574 if (nullb
->dev
->zoned
) {
1575 nullb
->dev
->discard
= false;
1576 pr_info("discard option is ignored in zoned mode\n");
1580 nullb
->q
->limits
.discard_granularity
= nullb
->dev
->blocksize
;
1581 nullb
->q
->limits
.discard_alignment
= nullb
->dev
->blocksize
;
1582 blk_queue_max_discard_sectors(nullb
->q
, UINT_MAX
>> 9);
1583 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, nullb
->q
);
1586 static const struct block_device_operations null_bio_ops
= {
1587 .owner
= THIS_MODULE
,
1588 .submit_bio
= null_submit_bio
,
1589 .report_zones
= null_report_zones
,
1592 static const struct block_device_operations null_rq_ops
= {
1593 .owner
= THIS_MODULE
,
1594 .report_zones
= null_report_zones
,
1597 static int setup_commands(struct nullb_queue
*nq
)
1599 struct nullb_cmd
*cmd
;
1602 nq
->cmds
= kcalloc(nq
->queue_depth
, sizeof(*cmd
), GFP_KERNEL
);
1606 tag_size
= ALIGN(nq
->queue_depth
, BITS_PER_LONG
) / BITS_PER_LONG
;
1607 nq
->tag_map
= kcalloc(tag_size
, sizeof(unsigned long), GFP_KERNEL
);
1613 for (i
= 0; i
< nq
->queue_depth
; i
++) {
1621 static int setup_queues(struct nullb
*nullb
)
1623 nullb
->queues
= kcalloc(nr_cpu_ids
, sizeof(struct nullb_queue
),
1628 nullb
->queue_depth
= nullb
->dev
->hw_queue_depth
;
1633 static int init_driver_queues(struct nullb
*nullb
)
1635 struct nullb_queue
*nq
;
1638 for (i
= 0; i
< nullb
->dev
->submit_queues
; i
++) {
1639 nq
= &nullb
->queues
[i
];
1641 null_init_queue(nullb
, nq
);
1643 ret
= setup_commands(nq
);
1651 static int null_gendisk_register(struct nullb
*nullb
)
1653 sector_t size
= ((sector_t
)nullb
->dev
->size
* SZ_1M
) >> SECTOR_SHIFT
;
1654 struct gendisk
*disk
;
1656 disk
= nullb
->disk
= alloc_disk_node(1, nullb
->dev
->home_node
);
1659 set_capacity(disk
, size
);
1661 disk
->flags
|= GENHD_FL_EXT_DEVT
| GENHD_FL_SUPPRESS_PARTITION_INFO
;
1662 disk
->major
= null_major
;
1663 disk
->first_minor
= nullb
->index
;
1664 if (queue_is_mq(nullb
->q
))
1665 disk
->fops
= &null_rq_ops
;
1667 disk
->fops
= &null_bio_ops
;
1668 disk
->private_data
= nullb
;
1669 disk
->queue
= nullb
->q
;
1670 strncpy(disk
->disk_name
, nullb
->disk_name
, DISK_NAME_LEN
);
1672 if (nullb
->dev
->zoned
) {
1673 int ret
= null_register_zoned_dev(nullb
);
1683 static int null_init_tag_set(struct nullb
*nullb
, struct blk_mq_tag_set
*set
)
1685 set
->ops
= &null_mq_ops
;
1686 set
->nr_hw_queues
= nullb
? nullb
->dev
->submit_queues
:
1688 set
->queue_depth
= nullb
? nullb
->dev
->hw_queue_depth
:
1690 set
->numa_node
= nullb
? nullb
->dev
->home_node
: g_home_node
;
1691 set
->cmd_size
= sizeof(struct nullb_cmd
);
1692 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
1694 set
->flags
|= BLK_MQ_F_NO_SCHED
;
1695 set
->driver_data
= NULL
;
1697 if ((nullb
&& nullb
->dev
->blocking
) || g_blocking
)
1698 set
->flags
|= BLK_MQ_F_BLOCKING
;
1700 return blk_mq_alloc_tag_set(set
);
1703 static int null_validate_conf(struct nullb_device
*dev
)
1705 dev
->blocksize
= round_down(dev
->blocksize
, 512);
1706 dev
->blocksize
= clamp_t(unsigned int, dev
->blocksize
, 512, 4096);
1708 if (dev
->queue_mode
== NULL_Q_MQ
&& dev
->use_per_node_hctx
) {
1709 if (dev
->submit_queues
!= nr_online_nodes
)
1710 dev
->submit_queues
= nr_online_nodes
;
1711 } else if (dev
->submit_queues
> nr_cpu_ids
)
1712 dev
->submit_queues
= nr_cpu_ids
;
1713 else if (dev
->submit_queues
== 0)
1714 dev
->submit_queues
= 1;
1716 dev
->queue_mode
= min_t(unsigned int, dev
->queue_mode
, NULL_Q_MQ
);
1717 dev
->irqmode
= min_t(unsigned int, dev
->irqmode
, NULL_IRQ_TIMER
);
1719 /* Do memory allocation, so set blocking */
1720 if (dev
->memory_backed
)
1721 dev
->blocking
= true;
1722 else /* cache is meaningless */
1723 dev
->cache_size
= 0;
1724 dev
->cache_size
= min_t(unsigned long, ULONG_MAX
/ 1024 / 1024,
1726 dev
->mbps
= min_t(unsigned int, 1024 * 40, dev
->mbps
);
1727 /* can not stop a queue */
1728 if (dev
->queue_mode
== NULL_Q_BIO
)
1732 (!dev
->zone_size
|| !is_power_of_2(dev
->zone_size
))) {
1733 pr_err("zone_size must be power-of-two\n");
1740 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1741 static bool __null_setup_fault(struct fault_attr
*attr
, char *str
)
1746 if (!setup_fault_attr(attr
, str
))
1754 static bool null_setup_fault(void)
1756 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1757 if (!__null_setup_fault(&null_timeout_attr
, g_timeout_str
))
1759 if (!__null_setup_fault(&null_requeue_attr
, g_requeue_str
))
1761 if (!__null_setup_fault(&null_init_hctx_attr
, g_init_hctx_str
))
1767 static int null_add_dev(struct nullb_device
*dev
)
1769 struct nullb
*nullb
;
1772 rv
= null_validate_conf(dev
);
1776 nullb
= kzalloc_node(sizeof(*nullb
), GFP_KERNEL
, dev
->home_node
);
1784 spin_lock_init(&nullb
->lock
);
1786 rv
= setup_queues(nullb
);
1788 goto out_free_nullb
;
1790 if (dev
->queue_mode
== NULL_Q_MQ
) {
1792 nullb
->tag_set
= &tag_set
;
1795 nullb
->tag_set
= &nullb
->__tag_set
;
1796 rv
= null_init_tag_set(nullb
, nullb
->tag_set
);
1800 goto out_cleanup_queues
;
1802 if (!null_setup_fault())
1803 goto out_cleanup_queues
;
1805 nullb
->tag_set
->timeout
= 5 * HZ
;
1806 nullb
->q
= blk_mq_init_queue_data(nullb
->tag_set
, nullb
);
1807 if (IS_ERR(nullb
->q
)) {
1809 goto out_cleanup_tags
;
1811 } else if (dev
->queue_mode
== NULL_Q_BIO
) {
1812 nullb
->q
= blk_alloc_queue(dev
->home_node
);
1815 goto out_cleanup_queues
;
1817 rv
= init_driver_queues(nullb
);
1819 goto out_cleanup_blk_queue
;
1823 set_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
);
1824 nullb_setup_bwtimer(nullb
);
1827 if (dev
->cache_size
> 0) {
1828 set_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
1829 blk_queue_write_cache(nullb
->q
, true, true);
1833 rv
= null_init_zoned_dev(dev
, nullb
->q
);
1835 goto out_cleanup_blk_queue
;
1838 nullb
->q
->queuedata
= nullb
;
1839 blk_queue_flag_set(QUEUE_FLAG_NONROT
, nullb
->q
);
1840 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, nullb
->q
);
1843 nullb
->index
= ida_simple_get(&nullb_indexes
, 0, 0, GFP_KERNEL
);
1844 dev
->index
= nullb
->index
;
1845 mutex_unlock(&lock
);
1847 blk_queue_logical_block_size(nullb
->q
, dev
->blocksize
);
1848 blk_queue_physical_block_size(nullb
->q
, dev
->blocksize
);
1850 null_config_discard(nullb
);
1852 sprintf(nullb
->disk_name
, "nullb%d", nullb
->index
);
1854 rv
= null_gendisk_register(nullb
);
1856 goto out_cleanup_zone
;
1859 list_add_tail(&nullb
->list
, &nullb_list
);
1860 mutex_unlock(&lock
);
1864 null_free_zoned_dev(dev
);
1865 out_cleanup_blk_queue
:
1866 blk_cleanup_queue(nullb
->q
);
1868 if (dev
->queue_mode
== NULL_Q_MQ
&& nullb
->tag_set
== &nullb
->__tag_set
)
1869 blk_mq_free_tag_set(nullb
->tag_set
);
1871 cleanup_queues(nullb
);
1879 static int __init
null_init(void)
1883 struct nullb
*nullb
;
1884 struct nullb_device
*dev
;
1886 if (g_bs
> PAGE_SIZE
) {
1887 pr_warn("invalid block size\n");
1888 pr_warn("defaults block size to %lu\n", PAGE_SIZE
);
1892 if (g_home_node
!= NUMA_NO_NODE
&& g_home_node
>= nr_online_nodes
) {
1893 pr_err("invalid home_node value\n");
1894 g_home_node
= NUMA_NO_NODE
;
1897 if (g_queue_mode
== NULL_Q_RQ
) {
1898 pr_err("legacy IO path no longer available\n");
1901 if (g_queue_mode
== NULL_Q_MQ
&& g_use_per_node_hctx
) {
1902 if (g_submit_queues
!= nr_online_nodes
) {
1903 pr_warn("submit_queues param is set to %u.\n",
1905 g_submit_queues
= nr_online_nodes
;
1907 } else if (g_submit_queues
> nr_cpu_ids
)
1908 g_submit_queues
= nr_cpu_ids
;
1909 else if (g_submit_queues
<= 0)
1910 g_submit_queues
= 1;
1912 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
) {
1913 ret
= null_init_tag_set(NULL
, &tag_set
);
1918 config_group_init(&nullb_subsys
.su_group
);
1919 mutex_init(&nullb_subsys
.su_mutex
);
1921 ret
= configfs_register_subsystem(&nullb_subsys
);
1927 null_major
= register_blkdev(0, "nullb");
1928 if (null_major
< 0) {
1933 for (i
= 0; i
< nr_devices
; i
++) {
1934 dev
= null_alloc_dev();
1939 ret
= null_add_dev(dev
);
1946 pr_info("module loaded\n");
1950 while (!list_empty(&nullb_list
)) {
1951 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
1953 null_del_dev(nullb
);
1956 unregister_blkdev(null_major
, "nullb");
1958 configfs_unregister_subsystem(&nullb_subsys
);
1960 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
1961 blk_mq_free_tag_set(&tag_set
);
1965 static void __exit
null_exit(void)
1967 struct nullb
*nullb
;
1969 configfs_unregister_subsystem(&nullb_subsys
);
1971 unregister_blkdev(null_major
, "nullb");
1974 while (!list_empty(&nullb_list
)) {
1975 struct nullb_device
*dev
;
1977 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
1979 null_del_dev(nullb
);
1982 mutex_unlock(&lock
);
1984 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
1985 blk_mq_free_tag_set(&tag_set
);
1988 module_init(null_init
);
1989 module_exit(null_exit
);
1991 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
1992 MODULE_LICENSE("GPL");