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 int g_max_sectors
;
156 module_param_named(max_sectors
, g_max_sectors
, int, 0444);
157 MODULE_PARM_DESC(max_sectors
, "Maximum size of a command (in 512B sectors)");
159 static unsigned int nr_devices
= 1;
160 module_param(nr_devices
, uint
, 0444);
161 MODULE_PARM_DESC(nr_devices
, "Number of devices to register");
163 static bool g_blocking
;
164 module_param_named(blocking
, g_blocking
, bool, 0444);
165 MODULE_PARM_DESC(blocking
, "Register as a blocking blk-mq driver device");
167 static bool shared_tags
;
168 module_param(shared_tags
, bool, 0444);
169 MODULE_PARM_DESC(shared_tags
, "Share tag set between devices for blk-mq");
171 static bool g_shared_tag_bitmap
;
172 module_param_named(shared_tag_bitmap
, g_shared_tag_bitmap
, bool, 0444);
173 MODULE_PARM_DESC(shared_tag_bitmap
, "Use shared tag bitmap for all submission queues for blk-mq");
175 static int g_irqmode
= NULL_IRQ_SOFTIRQ
;
177 static int null_set_irqmode(const char *str
, const struct kernel_param
*kp
)
179 return null_param_store_val(str
, &g_irqmode
, NULL_IRQ_NONE
,
183 static const struct kernel_param_ops null_irqmode_param_ops
= {
184 .set
= null_set_irqmode
,
185 .get
= param_get_int
,
188 device_param_cb(irqmode
, &null_irqmode_param_ops
, &g_irqmode
, 0444);
189 MODULE_PARM_DESC(irqmode
, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
191 static unsigned long g_completion_nsec
= 10000;
192 module_param_named(completion_nsec
, g_completion_nsec
, ulong
, 0444);
193 MODULE_PARM_DESC(completion_nsec
, "Time in ns to complete a request in hardware. Default: 10,000ns");
195 static int g_hw_queue_depth
= 64;
196 module_param_named(hw_queue_depth
, g_hw_queue_depth
, int, 0444);
197 MODULE_PARM_DESC(hw_queue_depth
, "Queue depth for each hardware queue. Default: 64");
199 static bool g_use_per_node_hctx
;
200 module_param_named(use_per_node_hctx
, g_use_per_node_hctx
, bool, 0444);
201 MODULE_PARM_DESC(use_per_node_hctx
, "Use per-node allocation for hardware context queues. Default: false");
204 module_param_named(zoned
, g_zoned
, bool, S_IRUGO
);
205 MODULE_PARM_DESC(zoned
, "Make device as a host-managed zoned block device. Default: false");
207 static unsigned long g_zone_size
= 256;
208 module_param_named(zone_size
, g_zone_size
, ulong
, S_IRUGO
);
209 MODULE_PARM_DESC(zone_size
, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
211 static unsigned long g_zone_capacity
;
212 module_param_named(zone_capacity
, g_zone_capacity
, ulong
, 0444);
213 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");
215 static unsigned int g_zone_nr_conv
;
216 module_param_named(zone_nr_conv
, g_zone_nr_conv
, uint
, 0444);
217 MODULE_PARM_DESC(zone_nr_conv
, "Number of conventional zones when block device is zoned. Default: 0");
219 static unsigned int g_zone_max_open
;
220 module_param_named(zone_max_open
, g_zone_max_open
, uint
, 0444);
221 MODULE_PARM_DESC(zone_max_open
, "Maximum number of open zones when block device is zoned. Default: 0 (no limit)");
223 static unsigned int g_zone_max_active
;
224 module_param_named(zone_max_active
, g_zone_max_active
, uint
, 0444);
225 MODULE_PARM_DESC(zone_max_active
, "Maximum number of active zones when block device is zoned. Default: 0 (no limit)");
227 static struct nullb_device
*null_alloc_dev(void);
228 static void null_free_dev(struct nullb_device
*dev
);
229 static void null_del_dev(struct nullb
*nullb
);
230 static int null_add_dev(struct nullb_device
*dev
);
231 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
);
233 static inline struct nullb_device
*to_nullb_device(struct config_item
*item
)
235 return item
? container_of(item
, struct nullb_device
, item
) : NULL
;
238 static inline ssize_t
nullb_device_uint_attr_show(unsigned int val
, char *page
)
240 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
243 static inline ssize_t
nullb_device_ulong_attr_show(unsigned long val
,
246 return snprintf(page
, PAGE_SIZE
, "%lu\n", val
);
249 static inline ssize_t
nullb_device_bool_attr_show(bool val
, char *page
)
251 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
254 static ssize_t
nullb_device_uint_attr_store(unsigned int *val
,
255 const char *page
, size_t count
)
260 result
= kstrtouint(page
, 0, &tmp
);
268 static ssize_t
nullb_device_ulong_attr_store(unsigned long *val
,
269 const char *page
, size_t count
)
274 result
= kstrtoul(page
, 0, &tmp
);
282 static ssize_t
nullb_device_bool_attr_store(bool *val
, const char *page
,
288 result
= kstrtobool(page
, &tmp
);
296 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
297 #define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY) \
299 nullb_device_##NAME##_show(struct config_item *item, char *page) \
301 return nullb_device_##TYPE##_attr_show( \
302 to_nullb_device(item)->NAME, page); \
305 nullb_device_##NAME##_store(struct config_item *item, const char *page, \
308 int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
309 struct nullb_device *dev = to_nullb_device(item); \
310 TYPE new_value = 0; \
313 ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
317 ret = apply_fn(dev, new_value); \
318 else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) \
322 dev->NAME = new_value; \
325 CONFIGFS_ATTR(nullb_device_, NAME);
327 static int nullb_apply_submit_queues(struct nullb_device
*dev
,
328 unsigned int submit_queues
)
330 struct nullb
*nullb
= dev
->nullb
;
331 struct blk_mq_tag_set
*set
;
337 * Make sure that null_init_hctx() does not access nullb->queues[] past
338 * the end of that array.
340 if (submit_queues
> nr_cpu_ids
)
342 set
= nullb
->tag_set
;
343 blk_mq_update_nr_hw_queues(set
, submit_queues
);
344 return set
->nr_hw_queues
== submit_queues
? 0 : -ENOMEM
;
347 NULLB_DEVICE_ATTR(size
, ulong
, NULL
);
348 NULLB_DEVICE_ATTR(completion_nsec
, ulong
, NULL
);
349 NULLB_DEVICE_ATTR(submit_queues
, uint
, nullb_apply_submit_queues
);
350 NULLB_DEVICE_ATTR(home_node
, uint
, NULL
);
351 NULLB_DEVICE_ATTR(queue_mode
, uint
, NULL
);
352 NULLB_DEVICE_ATTR(blocksize
, uint
, NULL
);
353 NULLB_DEVICE_ATTR(max_sectors
, uint
, NULL
);
354 NULLB_DEVICE_ATTR(irqmode
, uint
, NULL
);
355 NULLB_DEVICE_ATTR(hw_queue_depth
, uint
, NULL
);
356 NULLB_DEVICE_ATTR(index
, uint
, NULL
);
357 NULLB_DEVICE_ATTR(blocking
, bool, NULL
);
358 NULLB_DEVICE_ATTR(use_per_node_hctx
, bool, NULL
);
359 NULLB_DEVICE_ATTR(memory_backed
, bool, NULL
);
360 NULLB_DEVICE_ATTR(discard
, bool, NULL
);
361 NULLB_DEVICE_ATTR(mbps
, uint
, NULL
);
362 NULLB_DEVICE_ATTR(cache_size
, ulong
, NULL
);
363 NULLB_DEVICE_ATTR(zoned
, bool, NULL
);
364 NULLB_DEVICE_ATTR(zone_size
, ulong
, NULL
);
365 NULLB_DEVICE_ATTR(zone_capacity
, ulong
, NULL
);
366 NULLB_DEVICE_ATTR(zone_nr_conv
, uint
, NULL
);
367 NULLB_DEVICE_ATTR(zone_max_open
, uint
, NULL
);
368 NULLB_DEVICE_ATTR(zone_max_active
, uint
, NULL
);
370 static ssize_t
nullb_device_power_show(struct config_item
*item
, char *page
)
372 return nullb_device_bool_attr_show(to_nullb_device(item
)->power
, page
);
375 static ssize_t
nullb_device_power_store(struct config_item
*item
,
376 const char *page
, size_t count
)
378 struct nullb_device
*dev
= to_nullb_device(item
);
382 ret
= nullb_device_bool_attr_store(&newp
, page
, count
);
386 if (!dev
->power
&& newp
) {
387 if (test_and_set_bit(NULLB_DEV_FL_UP
, &dev
->flags
))
389 if (null_add_dev(dev
)) {
390 clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
);
394 set_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
396 } else if (dev
->power
&& !newp
) {
397 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
400 null_del_dev(dev
->nullb
);
403 clear_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
409 CONFIGFS_ATTR(nullb_device_
, power
);
411 static ssize_t
nullb_device_badblocks_show(struct config_item
*item
, char *page
)
413 struct nullb_device
*t_dev
= to_nullb_device(item
);
415 return badblocks_show(&t_dev
->badblocks
, page
, 0);
418 static ssize_t
nullb_device_badblocks_store(struct config_item
*item
,
419 const char *page
, size_t count
)
421 struct nullb_device
*t_dev
= to_nullb_device(item
);
422 char *orig
, *buf
, *tmp
;
426 orig
= kstrndup(page
, count
, GFP_KERNEL
);
430 buf
= strstrip(orig
);
433 if (buf
[0] != '+' && buf
[0] != '-')
435 tmp
= strchr(&buf
[1], '-');
439 ret
= kstrtoull(buf
+ 1, 0, &start
);
442 ret
= kstrtoull(tmp
+ 1, 0, &end
);
448 /* enable badblocks */
449 cmpxchg(&t_dev
->badblocks
.shift
, -1, 0);
451 ret
= badblocks_set(&t_dev
->badblocks
, start
,
454 ret
= badblocks_clear(&t_dev
->badblocks
, start
,
462 CONFIGFS_ATTR(nullb_device_
, badblocks
);
464 static struct configfs_attribute
*nullb_device_attrs
[] = {
465 &nullb_device_attr_size
,
466 &nullb_device_attr_completion_nsec
,
467 &nullb_device_attr_submit_queues
,
468 &nullb_device_attr_home_node
,
469 &nullb_device_attr_queue_mode
,
470 &nullb_device_attr_blocksize
,
471 &nullb_device_attr_max_sectors
,
472 &nullb_device_attr_irqmode
,
473 &nullb_device_attr_hw_queue_depth
,
474 &nullb_device_attr_index
,
475 &nullb_device_attr_blocking
,
476 &nullb_device_attr_use_per_node_hctx
,
477 &nullb_device_attr_power
,
478 &nullb_device_attr_memory_backed
,
479 &nullb_device_attr_discard
,
480 &nullb_device_attr_mbps
,
481 &nullb_device_attr_cache_size
,
482 &nullb_device_attr_badblocks
,
483 &nullb_device_attr_zoned
,
484 &nullb_device_attr_zone_size
,
485 &nullb_device_attr_zone_capacity
,
486 &nullb_device_attr_zone_nr_conv
,
487 &nullb_device_attr_zone_max_open
,
488 &nullb_device_attr_zone_max_active
,
492 static void nullb_device_release(struct config_item
*item
)
494 struct nullb_device
*dev
= to_nullb_device(item
);
496 null_free_device_storage(dev
, false);
500 static struct configfs_item_operations nullb_device_ops
= {
501 .release
= nullb_device_release
,
504 static const struct config_item_type nullb_device_type
= {
505 .ct_item_ops
= &nullb_device_ops
,
506 .ct_attrs
= nullb_device_attrs
,
507 .ct_owner
= THIS_MODULE
,
511 config_item
*nullb_group_make_item(struct config_group
*group
, const char *name
)
513 struct nullb_device
*dev
;
515 dev
= null_alloc_dev();
517 return ERR_PTR(-ENOMEM
);
519 config_item_init_type_name(&dev
->item
, name
, &nullb_device_type
);
525 nullb_group_drop_item(struct config_group
*group
, struct config_item
*item
)
527 struct nullb_device
*dev
= to_nullb_device(item
);
529 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
532 null_del_dev(dev
->nullb
);
536 config_item_put(item
);
539 static ssize_t
memb_group_features_show(struct config_item
*item
, char *page
)
541 return snprintf(page
, PAGE_SIZE
,
542 "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size,zone_capacity,zone_nr_conv,zone_max_open,zone_max_active,blocksize,max_sectors\n");
545 CONFIGFS_ATTR_RO(memb_group_
, features
);
547 static struct configfs_attribute
*nullb_group_attrs
[] = {
548 &memb_group_attr_features
,
552 static struct configfs_group_operations nullb_group_ops
= {
553 .make_item
= nullb_group_make_item
,
554 .drop_item
= nullb_group_drop_item
,
557 static const struct config_item_type nullb_group_type
= {
558 .ct_group_ops
= &nullb_group_ops
,
559 .ct_attrs
= nullb_group_attrs
,
560 .ct_owner
= THIS_MODULE
,
563 static struct configfs_subsystem nullb_subsys
= {
566 .ci_namebuf
= "nullb",
567 .ci_type
= &nullb_group_type
,
572 static inline int null_cache_active(struct nullb
*nullb
)
574 return test_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
577 static struct nullb_device
*null_alloc_dev(void)
579 struct nullb_device
*dev
;
581 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
584 INIT_RADIX_TREE(&dev
->data
, GFP_ATOMIC
);
585 INIT_RADIX_TREE(&dev
->cache
, GFP_ATOMIC
);
586 if (badblocks_init(&dev
->badblocks
, 0)) {
591 dev
->size
= g_gb
* 1024;
592 dev
->completion_nsec
= g_completion_nsec
;
593 dev
->submit_queues
= g_submit_queues
;
594 dev
->home_node
= g_home_node
;
595 dev
->queue_mode
= g_queue_mode
;
596 dev
->blocksize
= g_bs
;
597 dev
->max_sectors
= g_max_sectors
;
598 dev
->irqmode
= g_irqmode
;
599 dev
->hw_queue_depth
= g_hw_queue_depth
;
600 dev
->blocking
= g_blocking
;
601 dev
->use_per_node_hctx
= g_use_per_node_hctx
;
602 dev
->zoned
= g_zoned
;
603 dev
->zone_size
= g_zone_size
;
604 dev
->zone_capacity
= g_zone_capacity
;
605 dev
->zone_nr_conv
= g_zone_nr_conv
;
606 dev
->zone_max_open
= g_zone_max_open
;
607 dev
->zone_max_active
= g_zone_max_active
;
611 static void null_free_dev(struct nullb_device
*dev
)
616 null_free_zoned_dev(dev
);
617 badblocks_exit(&dev
->badblocks
);
621 static void put_tag(struct nullb_queue
*nq
, unsigned int tag
)
623 clear_bit_unlock(tag
, nq
->tag_map
);
625 if (waitqueue_active(&nq
->wait
))
629 static unsigned int get_tag(struct nullb_queue
*nq
)
634 tag
= find_first_zero_bit(nq
->tag_map
, nq
->queue_depth
);
635 if (tag
>= nq
->queue_depth
)
637 } while (test_and_set_bit_lock(tag
, nq
->tag_map
));
642 static void free_cmd(struct nullb_cmd
*cmd
)
644 put_tag(cmd
->nq
, cmd
->tag
);
647 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
);
649 static struct nullb_cmd
*__alloc_cmd(struct nullb_queue
*nq
)
651 struct nullb_cmd
*cmd
;
656 cmd
= &nq
->cmds
[tag
];
658 cmd
->error
= BLK_STS_OK
;
660 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
661 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
,
663 cmd
->timer
.function
= null_cmd_timer_expired
;
671 static struct nullb_cmd
*alloc_cmd(struct nullb_queue
*nq
, int can_wait
)
673 struct nullb_cmd
*cmd
;
676 cmd
= __alloc_cmd(nq
);
677 if (cmd
|| !can_wait
)
681 prepare_to_wait(&nq
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
682 cmd
= __alloc_cmd(nq
);
689 finish_wait(&nq
->wait
, &wait
);
693 static void end_cmd(struct nullb_cmd
*cmd
)
695 int queue_mode
= cmd
->nq
->dev
->queue_mode
;
697 switch (queue_mode
) {
699 blk_mq_end_request(cmd
->rq
, cmd
->error
);
702 cmd
->bio
->bi_status
= cmd
->error
;
710 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
)
712 end_cmd(container_of(timer
, struct nullb_cmd
, timer
));
714 return HRTIMER_NORESTART
;
717 static void null_cmd_end_timer(struct nullb_cmd
*cmd
)
719 ktime_t kt
= cmd
->nq
->dev
->completion_nsec
;
721 hrtimer_start(&cmd
->timer
, kt
, HRTIMER_MODE_REL
);
724 static void null_complete_rq(struct request
*rq
)
726 end_cmd(blk_mq_rq_to_pdu(rq
));
729 static struct nullb_page
*null_alloc_page(gfp_t gfp_flags
)
731 struct nullb_page
*t_page
;
733 t_page
= kmalloc(sizeof(struct nullb_page
), gfp_flags
);
737 t_page
->page
= alloc_pages(gfp_flags
, 0);
741 memset(t_page
->bitmap
, 0, sizeof(t_page
->bitmap
));
749 static void null_free_page(struct nullb_page
*t_page
)
751 __set_bit(NULLB_PAGE_FREE
, t_page
->bitmap
);
752 if (test_bit(NULLB_PAGE_LOCK
, t_page
->bitmap
))
754 __free_page(t_page
->page
);
758 static bool null_page_empty(struct nullb_page
*page
)
760 int size
= MAP_SZ
- 2;
762 return find_first_bit(page
->bitmap
, size
) == size
;
765 static void null_free_sector(struct nullb
*nullb
, sector_t sector
,
768 unsigned int sector_bit
;
770 struct nullb_page
*t_page
, *ret
;
771 struct radix_tree_root
*root
;
773 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
774 idx
= sector
>> PAGE_SECTORS_SHIFT
;
775 sector_bit
= (sector
& SECTOR_MASK
);
777 t_page
= radix_tree_lookup(root
, idx
);
779 __clear_bit(sector_bit
, t_page
->bitmap
);
781 if (null_page_empty(t_page
)) {
782 ret
= radix_tree_delete_item(root
, idx
, t_page
);
783 WARN_ON(ret
!= t_page
);
786 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
791 static struct nullb_page
*null_radix_tree_insert(struct nullb
*nullb
, u64 idx
,
792 struct nullb_page
*t_page
, bool is_cache
)
794 struct radix_tree_root
*root
;
796 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
798 if (radix_tree_insert(root
, idx
, t_page
)) {
799 null_free_page(t_page
);
800 t_page
= radix_tree_lookup(root
, idx
);
801 WARN_ON(!t_page
|| t_page
->page
->index
!= idx
);
803 nullb
->dev
->curr_cache
+= PAGE_SIZE
;
808 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
)
810 unsigned long pos
= 0;
812 struct nullb_page
*ret
, *t_pages
[FREE_BATCH
];
813 struct radix_tree_root
*root
;
815 root
= is_cache
? &dev
->cache
: &dev
->data
;
820 nr_pages
= radix_tree_gang_lookup(root
,
821 (void **)t_pages
, pos
, FREE_BATCH
);
823 for (i
= 0; i
< nr_pages
; i
++) {
824 pos
= t_pages
[i
]->page
->index
;
825 ret
= radix_tree_delete_item(root
, pos
, t_pages
[i
]);
826 WARN_ON(ret
!= t_pages
[i
]);
831 } while (nr_pages
== FREE_BATCH
);
837 static struct nullb_page
*__null_lookup_page(struct nullb
*nullb
,
838 sector_t sector
, bool for_write
, bool is_cache
)
840 unsigned int sector_bit
;
842 struct nullb_page
*t_page
;
843 struct radix_tree_root
*root
;
845 idx
= sector
>> PAGE_SECTORS_SHIFT
;
846 sector_bit
= (sector
& SECTOR_MASK
);
848 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
849 t_page
= radix_tree_lookup(root
, idx
);
850 WARN_ON(t_page
&& t_page
->page
->index
!= idx
);
852 if (t_page
&& (for_write
|| test_bit(sector_bit
, t_page
->bitmap
)))
858 static struct nullb_page
*null_lookup_page(struct nullb
*nullb
,
859 sector_t sector
, bool for_write
, bool ignore_cache
)
861 struct nullb_page
*page
= NULL
;
864 page
= __null_lookup_page(nullb
, sector
, for_write
, true);
867 return __null_lookup_page(nullb
, sector
, for_write
, false);
870 static struct nullb_page
*null_insert_page(struct nullb
*nullb
,
871 sector_t sector
, bool ignore_cache
)
872 __releases(&nullb
->lock
)
873 __acquires(&nullb
->lock
)
876 struct nullb_page
*t_page
;
878 t_page
= null_lookup_page(nullb
, sector
, true, ignore_cache
);
882 spin_unlock_irq(&nullb
->lock
);
884 t_page
= null_alloc_page(GFP_NOIO
);
888 if (radix_tree_preload(GFP_NOIO
))
891 spin_lock_irq(&nullb
->lock
);
892 idx
= sector
>> PAGE_SECTORS_SHIFT
;
893 t_page
->page
->index
= idx
;
894 t_page
= null_radix_tree_insert(nullb
, idx
, t_page
, !ignore_cache
);
895 radix_tree_preload_end();
899 null_free_page(t_page
);
901 spin_lock_irq(&nullb
->lock
);
902 return null_lookup_page(nullb
, sector
, true, ignore_cache
);
905 static int null_flush_cache_page(struct nullb
*nullb
, struct nullb_page
*c_page
)
910 struct nullb_page
*t_page
, *ret
;
913 idx
= c_page
->page
->index
;
915 t_page
= null_insert_page(nullb
, idx
<< PAGE_SECTORS_SHIFT
, true);
917 __clear_bit(NULLB_PAGE_LOCK
, c_page
->bitmap
);
918 if (test_bit(NULLB_PAGE_FREE
, c_page
->bitmap
)) {
919 null_free_page(c_page
);
920 if (t_page
&& null_page_empty(t_page
)) {
921 ret
= radix_tree_delete_item(&nullb
->dev
->data
,
923 null_free_page(t_page
);
931 src
= kmap_atomic(c_page
->page
);
932 dst
= kmap_atomic(t_page
->page
);
934 for (i
= 0; i
< PAGE_SECTORS
;
935 i
+= (nullb
->dev
->blocksize
>> SECTOR_SHIFT
)) {
936 if (test_bit(i
, c_page
->bitmap
)) {
937 offset
= (i
<< SECTOR_SHIFT
);
938 memcpy(dst
+ offset
, src
+ offset
,
939 nullb
->dev
->blocksize
);
940 __set_bit(i
, t_page
->bitmap
);
947 ret
= radix_tree_delete_item(&nullb
->dev
->cache
, idx
, c_page
);
949 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
954 static int null_make_cache_space(struct nullb
*nullb
, unsigned long n
)
956 int i
, err
, nr_pages
;
957 struct nullb_page
*c_pages
[FREE_BATCH
];
958 unsigned long flushed
= 0, one_round
;
961 if ((nullb
->dev
->cache_size
* 1024 * 1024) >
962 nullb
->dev
->curr_cache
+ n
|| nullb
->dev
->curr_cache
== 0)
965 nr_pages
= radix_tree_gang_lookup(&nullb
->dev
->cache
,
966 (void **)c_pages
, nullb
->cache_flush_pos
, FREE_BATCH
);
968 * nullb_flush_cache_page could unlock before using the c_pages. To
969 * avoid race, we don't allow page free
971 for (i
= 0; i
< nr_pages
; i
++) {
972 nullb
->cache_flush_pos
= c_pages
[i
]->page
->index
;
974 * We found the page which is being flushed to disk by other
977 if (test_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
))
980 __set_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
);
984 for (i
= 0; i
< nr_pages
; i
++) {
985 if (c_pages
[i
] == NULL
)
987 err
= null_flush_cache_page(nullb
, c_pages
[i
]);
992 flushed
+= one_round
<< PAGE_SHIFT
;
996 nullb
->cache_flush_pos
= 0;
997 if (one_round
== 0) {
998 /* give other threads a chance */
999 spin_unlock_irq(&nullb
->lock
);
1000 spin_lock_irq(&nullb
->lock
);
1007 static int copy_to_nullb(struct nullb
*nullb
, struct page
*source
,
1008 unsigned int off
, sector_t sector
, size_t n
, bool is_fua
)
1010 size_t temp
, count
= 0;
1011 unsigned int offset
;
1012 struct nullb_page
*t_page
;
1016 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
1018 if (null_cache_active(nullb
) && !is_fua
)
1019 null_make_cache_space(nullb
, PAGE_SIZE
);
1021 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
1022 t_page
= null_insert_page(nullb
, sector
,
1023 !null_cache_active(nullb
) || is_fua
);
1027 src
= kmap_atomic(source
);
1028 dst
= kmap_atomic(t_page
->page
);
1029 memcpy(dst
+ offset
, src
+ off
+ count
, temp
);
1033 __set_bit(sector
& SECTOR_MASK
, t_page
->bitmap
);
1036 null_free_sector(nullb
, sector
, true);
1039 sector
+= temp
>> SECTOR_SHIFT
;
1044 static int copy_from_nullb(struct nullb
*nullb
, struct page
*dest
,
1045 unsigned int off
, sector_t sector
, size_t n
)
1047 size_t temp
, count
= 0;
1048 unsigned int offset
;
1049 struct nullb_page
*t_page
;
1053 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
1055 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
1056 t_page
= null_lookup_page(nullb
, sector
, false,
1057 !null_cache_active(nullb
));
1059 dst
= kmap_atomic(dest
);
1061 memset(dst
+ off
+ count
, 0, temp
);
1064 src
= kmap_atomic(t_page
->page
);
1065 memcpy(dst
+ off
+ count
, src
+ offset
, temp
);
1071 sector
+= temp
>> SECTOR_SHIFT
;
1076 static void nullb_fill_pattern(struct nullb
*nullb
, struct page
*page
,
1077 unsigned int len
, unsigned int off
)
1081 dst
= kmap_atomic(page
);
1082 memset(dst
+ off
, 0xFF, len
);
1086 blk_status_t
null_handle_discard(struct nullb_device
*dev
,
1087 sector_t sector
, sector_t nr_sectors
)
1089 struct nullb
*nullb
= dev
->nullb
;
1090 size_t n
= nr_sectors
<< SECTOR_SHIFT
;
1093 spin_lock_irq(&nullb
->lock
);
1095 temp
= min_t(size_t, n
, dev
->blocksize
);
1096 null_free_sector(nullb
, sector
, false);
1097 if (null_cache_active(nullb
))
1098 null_free_sector(nullb
, sector
, true);
1099 sector
+= temp
>> SECTOR_SHIFT
;
1102 spin_unlock_irq(&nullb
->lock
);
1107 static int null_handle_flush(struct nullb
*nullb
)
1111 if (!null_cache_active(nullb
))
1114 spin_lock_irq(&nullb
->lock
);
1116 err
= null_make_cache_space(nullb
,
1117 nullb
->dev
->cache_size
* 1024 * 1024);
1118 if (err
|| nullb
->dev
->curr_cache
== 0)
1122 WARN_ON(!radix_tree_empty(&nullb
->dev
->cache
));
1123 spin_unlock_irq(&nullb
->lock
);
1127 static int null_transfer(struct nullb
*nullb
, struct page
*page
,
1128 unsigned int len
, unsigned int off
, bool is_write
, sector_t sector
,
1131 struct nullb_device
*dev
= nullb
->dev
;
1132 unsigned int valid_len
= len
;
1137 valid_len
= null_zone_valid_read_len(nullb
,
1141 err
= copy_from_nullb(nullb
, page
, off
,
1148 nullb_fill_pattern(nullb
, page
, len
, off
);
1149 flush_dcache_page(page
);
1151 flush_dcache_page(page
);
1152 err
= copy_to_nullb(nullb
, page
, off
, sector
, len
, is_fua
);
1158 static int null_handle_rq(struct nullb_cmd
*cmd
)
1160 struct request
*rq
= cmd
->rq
;
1161 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1164 sector_t sector
= blk_rq_pos(rq
);
1165 struct req_iterator iter
;
1166 struct bio_vec bvec
;
1168 spin_lock_irq(&nullb
->lock
);
1169 rq_for_each_segment(bvec
, rq
, iter
) {
1171 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1172 op_is_write(req_op(rq
)), sector
,
1173 rq
->cmd_flags
& REQ_FUA
);
1175 spin_unlock_irq(&nullb
->lock
);
1178 sector
+= len
>> SECTOR_SHIFT
;
1180 spin_unlock_irq(&nullb
->lock
);
1185 static int null_handle_bio(struct nullb_cmd
*cmd
)
1187 struct bio
*bio
= cmd
->bio
;
1188 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1191 sector_t sector
= bio
->bi_iter
.bi_sector
;
1192 struct bio_vec bvec
;
1193 struct bvec_iter iter
;
1195 spin_lock_irq(&nullb
->lock
);
1196 bio_for_each_segment(bvec
, bio
, iter
) {
1198 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1199 op_is_write(bio_op(bio
)), sector
,
1200 bio
->bi_opf
& REQ_FUA
);
1202 spin_unlock_irq(&nullb
->lock
);
1205 sector
+= len
>> SECTOR_SHIFT
;
1207 spin_unlock_irq(&nullb
->lock
);
1211 static void null_stop_queue(struct nullb
*nullb
)
1213 struct request_queue
*q
= nullb
->q
;
1215 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1216 blk_mq_stop_hw_queues(q
);
1219 static void null_restart_queue_async(struct nullb
*nullb
)
1221 struct request_queue
*q
= nullb
->q
;
1223 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1224 blk_mq_start_stopped_hw_queues(q
, true);
1227 static inline blk_status_t
null_handle_throttled(struct nullb_cmd
*cmd
)
1229 struct nullb_device
*dev
= cmd
->nq
->dev
;
1230 struct nullb
*nullb
= dev
->nullb
;
1231 blk_status_t sts
= BLK_STS_OK
;
1232 struct request
*rq
= cmd
->rq
;
1234 if (!hrtimer_active(&nullb
->bw_timer
))
1235 hrtimer_restart(&nullb
->bw_timer
);
1237 if (atomic_long_sub_return(blk_rq_bytes(rq
), &nullb
->cur_bytes
) < 0) {
1238 null_stop_queue(nullb
);
1239 /* race with timer */
1240 if (atomic_long_read(&nullb
->cur_bytes
) > 0)
1241 null_restart_queue_async(nullb
);
1242 /* requeue request */
1243 sts
= BLK_STS_DEV_RESOURCE
;
1248 static inline blk_status_t
null_handle_badblocks(struct nullb_cmd
*cmd
,
1250 sector_t nr_sectors
)
1252 struct badblocks
*bb
= &cmd
->nq
->dev
->badblocks
;
1256 if (badblocks_check(bb
, sector
, nr_sectors
, &first_bad
, &bad_sectors
))
1257 return BLK_STS_IOERR
;
1262 static inline blk_status_t
null_handle_memory_backed(struct nullb_cmd
*cmd
,
1265 sector_t nr_sectors
)
1267 struct nullb_device
*dev
= cmd
->nq
->dev
;
1270 if (op
== REQ_OP_DISCARD
)
1271 return null_handle_discard(dev
, sector
, nr_sectors
);
1273 if (dev
->queue_mode
== NULL_Q_BIO
)
1274 err
= null_handle_bio(cmd
);
1276 err
= null_handle_rq(cmd
);
1278 return errno_to_blk_status(err
);
1281 static void nullb_zero_read_cmd_buffer(struct nullb_cmd
*cmd
)
1283 struct nullb_device
*dev
= cmd
->nq
->dev
;
1286 if (dev
->memory_backed
)
1289 if (dev
->queue_mode
== NULL_Q_BIO
&& bio_op(cmd
->bio
) == REQ_OP_READ
) {
1290 zero_fill_bio(cmd
->bio
);
1291 } else if (req_op(cmd
->rq
) == REQ_OP_READ
) {
1292 __rq_for_each_bio(bio
, cmd
->rq
)
1297 static inline void nullb_complete_cmd(struct nullb_cmd
*cmd
)
1300 * Since root privileges are required to configure the null_blk
1301 * driver, it is fine that this driver does not initialize the
1302 * data buffers of read commands. Zero-initialize these buffers
1303 * anyway if KMSAN is enabled to prevent that KMSAN complains
1304 * about null_blk not initializing read data buffers.
1306 if (IS_ENABLED(CONFIG_KMSAN
))
1307 nullb_zero_read_cmd_buffer(cmd
);
1309 /* Complete IO by inline, softirq or timer */
1310 switch (cmd
->nq
->dev
->irqmode
) {
1311 case NULL_IRQ_SOFTIRQ
:
1312 switch (cmd
->nq
->dev
->queue_mode
) {
1314 if (likely(!blk_should_fake_timeout(cmd
->rq
->q
)))
1315 blk_mq_complete_request(cmd
->rq
);
1319 * XXX: no proper submitting cpu information available.
1328 case NULL_IRQ_TIMER
:
1329 null_cmd_end_timer(cmd
);
1334 blk_status_t
null_process_cmd(struct nullb_cmd
*cmd
,
1335 enum req_opf op
, sector_t sector
,
1336 unsigned int nr_sectors
)
1338 struct nullb_device
*dev
= cmd
->nq
->dev
;
1341 if (dev
->badblocks
.shift
!= -1) {
1342 ret
= null_handle_badblocks(cmd
, sector
, nr_sectors
);
1343 if (ret
!= BLK_STS_OK
)
1347 if (dev
->memory_backed
)
1348 return null_handle_memory_backed(cmd
, op
, sector
, nr_sectors
);
1353 static blk_status_t
null_handle_cmd(struct nullb_cmd
*cmd
, sector_t sector
,
1354 sector_t nr_sectors
, enum req_opf op
)
1356 struct nullb_device
*dev
= cmd
->nq
->dev
;
1357 struct nullb
*nullb
= dev
->nullb
;
1360 if (test_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
)) {
1361 sts
= null_handle_throttled(cmd
);
1362 if (sts
!= BLK_STS_OK
)
1366 if (op
== REQ_OP_FLUSH
) {
1367 cmd
->error
= errno_to_blk_status(null_handle_flush(nullb
));
1372 cmd
->error
= null_process_zoned_cmd(cmd
, op
,
1373 sector
, nr_sectors
);
1375 cmd
->error
= null_process_cmd(cmd
, op
, sector
, nr_sectors
);
1378 nullb_complete_cmd(cmd
);
1382 static enum hrtimer_restart
nullb_bwtimer_fn(struct hrtimer
*timer
)
1384 struct nullb
*nullb
= container_of(timer
, struct nullb
, bw_timer
);
1385 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1386 unsigned int mbps
= nullb
->dev
->mbps
;
1388 if (atomic_long_read(&nullb
->cur_bytes
) == mb_per_tick(mbps
))
1389 return HRTIMER_NORESTART
;
1391 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(mbps
));
1392 null_restart_queue_async(nullb
);
1394 hrtimer_forward_now(&nullb
->bw_timer
, timer_interval
);
1396 return HRTIMER_RESTART
;
1399 static void nullb_setup_bwtimer(struct nullb
*nullb
)
1401 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1403 hrtimer_init(&nullb
->bw_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1404 nullb
->bw_timer
.function
= nullb_bwtimer_fn
;
1405 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(nullb
->dev
->mbps
));
1406 hrtimer_start(&nullb
->bw_timer
, timer_interval
, HRTIMER_MODE_REL
);
1409 static struct nullb_queue
*nullb_to_queue(struct nullb
*nullb
)
1413 if (nullb
->nr_queues
!= 1)
1414 index
= raw_smp_processor_id() / ((nr_cpu_ids
+ nullb
->nr_queues
- 1) / nullb
->nr_queues
);
1416 return &nullb
->queues
[index
];
1419 static blk_qc_t
null_submit_bio(struct bio
*bio
)
1421 sector_t sector
= bio
->bi_iter
.bi_sector
;
1422 sector_t nr_sectors
= bio_sectors(bio
);
1423 struct nullb
*nullb
= bio
->bi_disk
->private_data
;
1424 struct nullb_queue
*nq
= nullb_to_queue(nullb
);
1425 struct nullb_cmd
*cmd
;
1427 cmd
= alloc_cmd(nq
, 1);
1430 null_handle_cmd(cmd
, sector
, nr_sectors
, bio_op(bio
));
1431 return BLK_QC_T_NONE
;
1434 static bool should_timeout_request(struct request
*rq
)
1436 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1437 if (g_timeout_str
[0])
1438 return should_fail(&null_timeout_attr
, 1);
1443 static bool should_requeue_request(struct request
*rq
)
1445 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1446 if (g_requeue_str
[0])
1447 return should_fail(&null_requeue_attr
, 1);
1452 static enum blk_eh_timer_return
null_timeout_rq(struct request
*rq
, bool res
)
1454 pr_info("rq %p timed out\n", rq
);
1455 blk_mq_complete_request(rq
);
1459 static blk_status_t
null_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1460 const struct blk_mq_queue_data
*bd
)
1462 struct nullb_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
1463 struct nullb_queue
*nq
= hctx
->driver_data
;
1464 sector_t nr_sectors
= blk_rq_sectors(bd
->rq
);
1465 sector_t sector
= blk_rq_pos(bd
->rq
);
1467 might_sleep_if(hctx
->flags
& BLK_MQ_F_BLOCKING
);
1469 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
1470 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1471 cmd
->timer
.function
= null_cmd_timer_expired
;
1474 cmd
->error
= BLK_STS_OK
;
1477 blk_mq_start_request(bd
->rq
);
1479 if (should_requeue_request(bd
->rq
)) {
1481 * Alternate between hitting the core BUSY path, and the
1482 * driver driven requeue path
1484 nq
->requeue_selection
++;
1485 if (nq
->requeue_selection
& 1)
1486 return BLK_STS_RESOURCE
;
1488 blk_mq_requeue_request(bd
->rq
, true);
1492 if (should_timeout_request(bd
->rq
))
1495 return null_handle_cmd(cmd
, sector
, nr_sectors
, req_op(bd
->rq
));
1498 static void cleanup_queue(struct nullb_queue
*nq
)
1504 static void cleanup_queues(struct nullb
*nullb
)
1508 for (i
= 0; i
< nullb
->nr_queues
; i
++)
1509 cleanup_queue(&nullb
->queues
[i
]);
1511 kfree(nullb
->queues
);
1514 static void null_exit_hctx(struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1516 struct nullb_queue
*nq
= hctx
->driver_data
;
1517 struct nullb
*nullb
= nq
->dev
->nullb
;
1522 static void null_init_queue(struct nullb
*nullb
, struct nullb_queue
*nq
)
1524 init_waitqueue_head(&nq
->wait
);
1525 nq
->queue_depth
= nullb
->queue_depth
;
1526 nq
->dev
= nullb
->dev
;
1529 static int null_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *driver_data
,
1530 unsigned int hctx_idx
)
1532 struct nullb
*nullb
= hctx
->queue
->queuedata
;
1533 struct nullb_queue
*nq
;
1535 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1536 if (g_init_hctx_str
[0] && should_fail(&null_init_hctx_attr
, 1))
1540 nq
= &nullb
->queues
[hctx_idx
];
1541 hctx
->driver_data
= nq
;
1542 null_init_queue(nullb
, nq
);
1548 static const struct blk_mq_ops null_mq_ops
= {
1549 .queue_rq
= null_queue_rq
,
1550 .complete
= null_complete_rq
,
1551 .timeout
= null_timeout_rq
,
1552 .init_hctx
= null_init_hctx
,
1553 .exit_hctx
= null_exit_hctx
,
1556 static void null_del_dev(struct nullb
*nullb
)
1558 struct nullb_device
*dev
;
1565 ida_simple_remove(&nullb_indexes
, nullb
->index
);
1567 list_del_init(&nullb
->list
);
1569 del_gendisk(nullb
->disk
);
1571 if (test_bit(NULLB_DEV_FL_THROTTLED
, &nullb
->dev
->flags
)) {
1572 hrtimer_cancel(&nullb
->bw_timer
);
1573 atomic_long_set(&nullb
->cur_bytes
, LONG_MAX
);
1574 null_restart_queue_async(nullb
);
1577 blk_cleanup_queue(nullb
->q
);
1578 if (dev
->queue_mode
== NULL_Q_MQ
&&
1579 nullb
->tag_set
== &nullb
->__tag_set
)
1580 blk_mq_free_tag_set(nullb
->tag_set
);
1581 put_disk(nullb
->disk
);
1582 cleanup_queues(nullb
);
1583 if (null_cache_active(nullb
))
1584 null_free_device_storage(nullb
->dev
, true);
1589 static void null_config_discard(struct nullb
*nullb
)
1591 if (nullb
->dev
->discard
== false)
1594 if (!nullb
->dev
->memory_backed
) {
1595 nullb
->dev
->discard
= false;
1596 pr_info("discard option is ignored without memory backing\n");
1600 if (nullb
->dev
->zoned
) {
1601 nullb
->dev
->discard
= false;
1602 pr_info("discard option is ignored in zoned mode\n");
1606 nullb
->q
->limits
.discard_granularity
= nullb
->dev
->blocksize
;
1607 nullb
->q
->limits
.discard_alignment
= nullb
->dev
->blocksize
;
1608 blk_queue_max_discard_sectors(nullb
->q
, UINT_MAX
>> 9);
1609 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, nullb
->q
);
1612 static const struct block_device_operations null_bio_ops
= {
1613 .owner
= THIS_MODULE
,
1614 .submit_bio
= null_submit_bio
,
1615 .report_zones
= null_report_zones
,
1618 static const struct block_device_operations null_rq_ops
= {
1619 .owner
= THIS_MODULE
,
1620 .report_zones
= null_report_zones
,
1623 static int setup_commands(struct nullb_queue
*nq
)
1625 struct nullb_cmd
*cmd
;
1628 nq
->cmds
= kcalloc(nq
->queue_depth
, sizeof(*cmd
), GFP_KERNEL
);
1632 tag_size
= ALIGN(nq
->queue_depth
, BITS_PER_LONG
) / BITS_PER_LONG
;
1633 nq
->tag_map
= kcalloc(tag_size
, sizeof(unsigned long), GFP_KERNEL
);
1639 for (i
= 0; i
< nq
->queue_depth
; i
++) {
1647 static int setup_queues(struct nullb
*nullb
)
1649 nullb
->queues
= kcalloc(nr_cpu_ids
, sizeof(struct nullb_queue
),
1654 nullb
->queue_depth
= nullb
->dev
->hw_queue_depth
;
1659 static int init_driver_queues(struct nullb
*nullb
)
1661 struct nullb_queue
*nq
;
1664 for (i
= 0; i
< nullb
->dev
->submit_queues
; i
++) {
1665 nq
= &nullb
->queues
[i
];
1667 null_init_queue(nullb
, nq
);
1669 ret
= setup_commands(nq
);
1677 static int null_gendisk_register(struct nullb
*nullb
)
1679 sector_t size
= ((sector_t
)nullb
->dev
->size
* SZ_1M
) >> SECTOR_SHIFT
;
1680 struct gendisk
*disk
;
1682 disk
= nullb
->disk
= alloc_disk_node(1, nullb
->dev
->home_node
);
1685 set_capacity(disk
, size
);
1687 disk
->flags
|= GENHD_FL_EXT_DEVT
| GENHD_FL_SUPPRESS_PARTITION_INFO
;
1688 disk
->major
= null_major
;
1689 disk
->first_minor
= nullb
->index
;
1690 if (queue_is_mq(nullb
->q
))
1691 disk
->fops
= &null_rq_ops
;
1693 disk
->fops
= &null_bio_ops
;
1694 disk
->private_data
= nullb
;
1695 disk
->queue
= nullb
->q
;
1696 strncpy(disk
->disk_name
, nullb
->disk_name
, DISK_NAME_LEN
);
1698 if (nullb
->dev
->zoned
) {
1699 int ret
= null_register_zoned_dev(nullb
);
1709 static int null_init_tag_set(struct nullb
*nullb
, struct blk_mq_tag_set
*set
)
1711 set
->ops
= &null_mq_ops
;
1712 set
->nr_hw_queues
= nullb
? nullb
->dev
->submit_queues
:
1714 set
->queue_depth
= nullb
? nullb
->dev
->hw_queue_depth
:
1716 set
->numa_node
= nullb
? nullb
->dev
->home_node
: g_home_node
;
1717 set
->cmd_size
= sizeof(struct nullb_cmd
);
1718 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
1720 set
->flags
|= BLK_MQ_F_NO_SCHED
;
1721 if (g_shared_tag_bitmap
)
1722 set
->flags
|= BLK_MQ_F_TAG_HCTX_SHARED
;
1723 set
->driver_data
= NULL
;
1725 if ((nullb
&& nullb
->dev
->blocking
) || g_blocking
)
1726 set
->flags
|= BLK_MQ_F_BLOCKING
;
1728 return blk_mq_alloc_tag_set(set
);
1731 static int null_validate_conf(struct nullb_device
*dev
)
1733 dev
->blocksize
= round_down(dev
->blocksize
, 512);
1734 dev
->blocksize
= clamp_t(unsigned int, dev
->blocksize
, 512, 4096);
1736 if (dev
->queue_mode
== NULL_Q_MQ
&& dev
->use_per_node_hctx
) {
1737 if (dev
->submit_queues
!= nr_online_nodes
)
1738 dev
->submit_queues
= nr_online_nodes
;
1739 } else if (dev
->submit_queues
> nr_cpu_ids
)
1740 dev
->submit_queues
= nr_cpu_ids
;
1741 else if (dev
->submit_queues
== 0)
1742 dev
->submit_queues
= 1;
1744 dev
->queue_mode
= min_t(unsigned int, dev
->queue_mode
, NULL_Q_MQ
);
1745 dev
->irqmode
= min_t(unsigned int, dev
->irqmode
, NULL_IRQ_TIMER
);
1747 /* Do memory allocation, so set blocking */
1748 if (dev
->memory_backed
)
1749 dev
->blocking
= true;
1750 else /* cache is meaningless */
1751 dev
->cache_size
= 0;
1752 dev
->cache_size
= min_t(unsigned long, ULONG_MAX
/ 1024 / 1024,
1754 dev
->mbps
= min_t(unsigned int, 1024 * 40, dev
->mbps
);
1755 /* can not stop a queue */
1756 if (dev
->queue_mode
== NULL_Q_BIO
)
1760 (!dev
->zone_size
|| !is_power_of_2(dev
->zone_size
))) {
1761 pr_err("zone_size must be power-of-two\n");
1768 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1769 static bool __null_setup_fault(struct fault_attr
*attr
, char *str
)
1774 if (!setup_fault_attr(attr
, str
))
1782 static bool null_setup_fault(void)
1784 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1785 if (!__null_setup_fault(&null_timeout_attr
, g_timeout_str
))
1787 if (!__null_setup_fault(&null_requeue_attr
, g_requeue_str
))
1789 if (!__null_setup_fault(&null_init_hctx_attr
, g_init_hctx_str
))
1795 static int null_add_dev(struct nullb_device
*dev
)
1797 struct nullb
*nullb
;
1800 rv
= null_validate_conf(dev
);
1804 nullb
= kzalloc_node(sizeof(*nullb
), GFP_KERNEL
, dev
->home_node
);
1812 spin_lock_init(&nullb
->lock
);
1814 rv
= setup_queues(nullb
);
1816 goto out_free_nullb
;
1818 if (dev
->queue_mode
== NULL_Q_MQ
) {
1820 nullb
->tag_set
= &tag_set
;
1823 nullb
->tag_set
= &nullb
->__tag_set
;
1824 rv
= null_init_tag_set(nullb
, nullb
->tag_set
);
1828 goto out_cleanup_queues
;
1830 if (!null_setup_fault())
1831 goto out_cleanup_queues
;
1833 nullb
->tag_set
->timeout
= 5 * HZ
;
1834 nullb
->q
= blk_mq_init_queue_data(nullb
->tag_set
, nullb
);
1835 if (IS_ERR(nullb
->q
)) {
1837 goto out_cleanup_tags
;
1839 } else if (dev
->queue_mode
== NULL_Q_BIO
) {
1840 nullb
->q
= blk_alloc_queue(dev
->home_node
);
1843 goto out_cleanup_queues
;
1845 rv
= init_driver_queues(nullb
);
1847 goto out_cleanup_blk_queue
;
1851 set_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
);
1852 nullb_setup_bwtimer(nullb
);
1855 if (dev
->cache_size
> 0) {
1856 set_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
1857 blk_queue_write_cache(nullb
->q
, true, true);
1861 rv
= null_init_zoned_dev(dev
, nullb
->q
);
1863 goto out_cleanup_blk_queue
;
1866 nullb
->q
->queuedata
= nullb
;
1867 blk_queue_flag_set(QUEUE_FLAG_NONROT
, nullb
->q
);
1868 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, nullb
->q
);
1871 nullb
->index
= ida_simple_get(&nullb_indexes
, 0, 0, GFP_KERNEL
);
1872 dev
->index
= nullb
->index
;
1873 mutex_unlock(&lock
);
1875 blk_queue_logical_block_size(nullb
->q
, dev
->blocksize
);
1876 blk_queue_physical_block_size(nullb
->q
, dev
->blocksize
);
1877 if (!dev
->max_sectors
)
1878 dev
->max_sectors
= queue_max_hw_sectors(nullb
->q
);
1879 dev
->max_sectors
= min_t(unsigned int, dev
->max_sectors
,
1880 BLK_DEF_MAX_SECTORS
);
1881 blk_queue_max_hw_sectors(nullb
->q
, dev
->max_sectors
);
1883 null_config_discard(nullb
);
1885 sprintf(nullb
->disk_name
, "nullb%d", nullb
->index
);
1887 rv
= null_gendisk_register(nullb
);
1889 goto out_cleanup_zone
;
1892 list_add_tail(&nullb
->list
, &nullb_list
);
1893 mutex_unlock(&lock
);
1897 null_free_zoned_dev(dev
);
1898 out_cleanup_blk_queue
:
1899 blk_cleanup_queue(nullb
->q
);
1901 if (dev
->queue_mode
== NULL_Q_MQ
&& nullb
->tag_set
== &nullb
->__tag_set
)
1902 blk_mq_free_tag_set(nullb
->tag_set
);
1904 cleanup_queues(nullb
);
1912 static int __init
null_init(void)
1916 struct nullb
*nullb
;
1917 struct nullb_device
*dev
;
1919 if (g_bs
> PAGE_SIZE
) {
1920 pr_warn("invalid block size\n");
1921 pr_warn("defaults block size to %lu\n", PAGE_SIZE
);
1925 if (g_max_sectors
> BLK_DEF_MAX_SECTORS
) {
1926 pr_warn("invalid max sectors\n");
1927 pr_warn("defaults max sectors to %u\n", BLK_DEF_MAX_SECTORS
);
1928 g_max_sectors
= BLK_DEF_MAX_SECTORS
;
1931 if (g_home_node
!= NUMA_NO_NODE
&& g_home_node
>= nr_online_nodes
) {
1932 pr_err("invalid home_node value\n");
1933 g_home_node
= NUMA_NO_NODE
;
1936 if (g_queue_mode
== NULL_Q_RQ
) {
1937 pr_err("legacy IO path no longer available\n");
1940 if (g_queue_mode
== NULL_Q_MQ
&& g_use_per_node_hctx
) {
1941 if (g_submit_queues
!= nr_online_nodes
) {
1942 pr_warn("submit_queues param is set to %u.\n",
1944 g_submit_queues
= nr_online_nodes
;
1946 } else if (g_submit_queues
> nr_cpu_ids
)
1947 g_submit_queues
= nr_cpu_ids
;
1948 else if (g_submit_queues
<= 0)
1949 g_submit_queues
= 1;
1951 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
) {
1952 ret
= null_init_tag_set(NULL
, &tag_set
);
1957 config_group_init(&nullb_subsys
.su_group
);
1958 mutex_init(&nullb_subsys
.su_mutex
);
1960 ret
= configfs_register_subsystem(&nullb_subsys
);
1966 null_major
= register_blkdev(0, "nullb");
1967 if (null_major
< 0) {
1972 for (i
= 0; i
< nr_devices
; i
++) {
1973 dev
= null_alloc_dev();
1978 ret
= null_add_dev(dev
);
1985 pr_info("module loaded\n");
1989 while (!list_empty(&nullb_list
)) {
1990 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
1992 null_del_dev(nullb
);
1995 unregister_blkdev(null_major
, "nullb");
1997 configfs_unregister_subsystem(&nullb_subsys
);
1999 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
2000 blk_mq_free_tag_set(&tag_set
);
2004 static void __exit
null_exit(void)
2006 struct nullb
*nullb
;
2008 configfs_unregister_subsystem(&nullb_subsys
);
2010 unregister_blkdev(null_major
, "nullb");
2013 while (!list_empty(&nullb_list
)) {
2014 struct nullb_device
*dev
;
2016 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
2018 null_del_dev(nullb
);
2021 mutex_unlock(&lock
);
2023 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
2024 blk_mq_free_tag_set(&tag_set
);
2027 module_init(null_init
);
2028 module_exit(null_exit
);
2030 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
2031 MODULE_LICENSE("GPL");