2 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
3 * Shaohua Li <shli@fb.com>
5 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/sched.h>
10 #include <linux/init.h>
13 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
14 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
15 #define SECTOR_MASK (PAGE_SECTORS - 1)
19 #define TICKS_PER_SEC 50ULL
20 #define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
22 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
23 static DECLARE_FAULT_ATTR(null_timeout_attr
);
24 static DECLARE_FAULT_ATTR(null_requeue_attr
);
27 static inline u64
mb_per_tick(int mbps
)
29 return (1 << 20) / TICKS_PER_SEC
* ((u64
) mbps
);
33 * Status flags for nullb_device.
35 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
36 * UP: Device is currently on and visible in userspace.
37 * THROTTLED: Device is being throttled.
38 * CACHE: Device is using a write-back cache.
40 enum nullb_device_flags
{
41 NULLB_DEV_FL_CONFIGURED
= 0,
43 NULLB_DEV_FL_THROTTLED
= 2,
44 NULLB_DEV_FL_CACHE
= 3,
47 #define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
49 * nullb_page is a page in memory for nullb devices.
51 * @page: The page holding the data.
52 * @bitmap: The bitmap represents which sector in the page has data.
53 * Each bit represents one block size. For example, sector 8
54 * will use the 7th bit
55 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
56 * page is being flushing to storage. FREE means the cache page is freed and
57 * should be skipped from flushing to storage. Please see
58 * null_make_cache_space
62 DECLARE_BITMAP(bitmap
, MAP_SZ
);
64 #define NULLB_PAGE_LOCK (MAP_SZ - 1)
65 #define NULLB_PAGE_FREE (MAP_SZ - 2)
67 static LIST_HEAD(nullb_list
);
68 static struct mutex lock
;
69 static int null_major
;
70 static DEFINE_IDA(nullb_indexes
);
71 static struct blk_mq_tag_set tag_set
;
85 static int g_no_sched
;
86 module_param_named(no_sched
, g_no_sched
, int, 0444);
87 MODULE_PARM_DESC(no_sched
, "No io scheduler");
89 static int g_submit_queues
= 1;
90 module_param_named(submit_queues
, g_submit_queues
, int, 0444);
91 MODULE_PARM_DESC(submit_queues
, "Number of submission queues");
93 static int g_home_node
= NUMA_NO_NODE
;
94 module_param_named(home_node
, g_home_node
, int, 0444);
95 MODULE_PARM_DESC(home_node
, "Home node for the device");
97 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
98 static char g_timeout_str
[80];
99 module_param_string(timeout
, g_timeout_str
, sizeof(g_timeout_str
), 0444);
101 static char g_requeue_str
[80];
102 module_param_string(requeue
, g_requeue_str
, sizeof(g_requeue_str
), 0444);
105 static int g_queue_mode
= NULL_Q_MQ
;
107 static int null_param_store_val(const char *str
, int *val
, int min
, int max
)
111 ret
= kstrtoint(str
, 10, &new_val
);
115 if (new_val
< min
|| new_val
> max
)
122 static int null_set_queue_mode(const char *str
, const struct kernel_param
*kp
)
124 return null_param_store_val(str
, &g_queue_mode
, NULL_Q_BIO
, NULL_Q_MQ
);
127 static const struct kernel_param_ops null_queue_mode_param_ops
= {
128 .set
= null_set_queue_mode
,
129 .get
= param_get_int
,
132 device_param_cb(queue_mode
, &null_queue_mode_param_ops
, &g_queue_mode
, 0444);
133 MODULE_PARM_DESC(queue_mode
, "Block interface to use (0=bio,1=rq,2=multiqueue)");
135 static int g_gb
= 250;
136 module_param_named(gb
, g_gb
, int, 0444);
137 MODULE_PARM_DESC(gb
, "Size in GB");
139 static int g_bs
= 512;
140 module_param_named(bs
, g_bs
, int, 0444);
141 MODULE_PARM_DESC(bs
, "Block size (in bytes)");
143 static int nr_devices
= 1;
144 module_param(nr_devices
, int, 0444);
145 MODULE_PARM_DESC(nr_devices
, "Number of devices to register");
147 static bool g_blocking
;
148 module_param_named(blocking
, g_blocking
, bool, 0444);
149 MODULE_PARM_DESC(blocking
, "Register as a blocking blk-mq driver device");
151 static bool shared_tags
;
152 module_param(shared_tags
, bool, 0444);
153 MODULE_PARM_DESC(shared_tags
, "Share tag set between devices for blk-mq");
155 static int g_irqmode
= NULL_IRQ_SOFTIRQ
;
157 static int null_set_irqmode(const char *str
, const struct kernel_param
*kp
)
159 return null_param_store_val(str
, &g_irqmode
, NULL_IRQ_NONE
,
163 static const struct kernel_param_ops null_irqmode_param_ops
= {
164 .set
= null_set_irqmode
,
165 .get
= param_get_int
,
168 device_param_cb(irqmode
, &null_irqmode_param_ops
, &g_irqmode
, 0444);
169 MODULE_PARM_DESC(irqmode
, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
171 static unsigned long g_completion_nsec
= 10000;
172 module_param_named(completion_nsec
, g_completion_nsec
, ulong
, 0444);
173 MODULE_PARM_DESC(completion_nsec
, "Time in ns to complete a request in hardware. Default: 10,000ns");
175 static int g_hw_queue_depth
= 64;
176 module_param_named(hw_queue_depth
, g_hw_queue_depth
, int, 0444);
177 MODULE_PARM_DESC(hw_queue_depth
, "Queue depth for each hardware queue. Default: 64");
179 static bool g_use_per_node_hctx
;
180 module_param_named(use_per_node_hctx
, g_use_per_node_hctx
, bool, 0444);
181 MODULE_PARM_DESC(use_per_node_hctx
, "Use per-node allocation for hardware context queues. Default: false");
184 module_param_named(zoned
, g_zoned
, bool, S_IRUGO
);
185 MODULE_PARM_DESC(zoned
, "Make device as a host-managed zoned block device. Default: false");
187 static unsigned long g_zone_size
= 256;
188 module_param_named(zone_size
, g_zone_size
, ulong
, S_IRUGO
);
189 MODULE_PARM_DESC(zone_size
, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
191 static struct nullb_device
*null_alloc_dev(void);
192 static void null_free_dev(struct nullb_device
*dev
);
193 static void null_del_dev(struct nullb
*nullb
);
194 static int null_add_dev(struct nullb_device
*dev
);
195 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
);
197 static inline struct nullb_device
*to_nullb_device(struct config_item
*item
)
199 return item
? container_of(item
, struct nullb_device
, item
) : NULL
;
202 static inline ssize_t
nullb_device_uint_attr_show(unsigned int val
, char *page
)
204 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
207 static inline ssize_t
nullb_device_ulong_attr_show(unsigned long val
,
210 return snprintf(page
, PAGE_SIZE
, "%lu\n", val
);
213 static inline ssize_t
nullb_device_bool_attr_show(bool val
, char *page
)
215 return snprintf(page
, PAGE_SIZE
, "%u\n", val
);
218 static ssize_t
nullb_device_uint_attr_store(unsigned int *val
,
219 const char *page
, size_t count
)
224 result
= kstrtouint(page
, 0, &tmp
);
232 static ssize_t
nullb_device_ulong_attr_store(unsigned long *val
,
233 const char *page
, size_t count
)
238 result
= kstrtoul(page
, 0, &tmp
);
246 static ssize_t
nullb_device_bool_attr_store(bool *val
, const char *page
,
252 result
= kstrtobool(page
, &tmp
);
260 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
261 #define NULLB_DEVICE_ATTR(NAME, TYPE) \
263 nullb_device_##NAME##_show(struct config_item *item, char *page) \
265 return nullb_device_##TYPE##_attr_show( \
266 to_nullb_device(item)->NAME, page); \
269 nullb_device_##NAME##_store(struct config_item *item, const char *page, \
272 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
274 return nullb_device_##TYPE##_attr_store( \
275 &to_nullb_device(item)->NAME, page, count); \
277 CONFIGFS_ATTR(nullb_device_, NAME);
279 NULLB_DEVICE_ATTR(size
, ulong
);
280 NULLB_DEVICE_ATTR(completion_nsec
, ulong
);
281 NULLB_DEVICE_ATTR(submit_queues
, uint
);
282 NULLB_DEVICE_ATTR(home_node
, uint
);
283 NULLB_DEVICE_ATTR(queue_mode
, uint
);
284 NULLB_DEVICE_ATTR(blocksize
, uint
);
285 NULLB_DEVICE_ATTR(irqmode
, uint
);
286 NULLB_DEVICE_ATTR(hw_queue_depth
, uint
);
287 NULLB_DEVICE_ATTR(index
, uint
);
288 NULLB_DEVICE_ATTR(blocking
, bool);
289 NULLB_DEVICE_ATTR(use_per_node_hctx
, bool);
290 NULLB_DEVICE_ATTR(memory_backed
, bool);
291 NULLB_DEVICE_ATTR(discard
, bool);
292 NULLB_DEVICE_ATTR(mbps
, uint
);
293 NULLB_DEVICE_ATTR(cache_size
, ulong
);
294 NULLB_DEVICE_ATTR(zoned
, bool);
295 NULLB_DEVICE_ATTR(zone_size
, ulong
);
297 static ssize_t
nullb_device_power_show(struct config_item
*item
, char *page
)
299 return nullb_device_bool_attr_show(to_nullb_device(item
)->power
, page
);
302 static ssize_t
nullb_device_power_store(struct config_item
*item
,
303 const char *page
, size_t count
)
305 struct nullb_device
*dev
= to_nullb_device(item
);
309 ret
= nullb_device_bool_attr_store(&newp
, page
, count
);
313 if (!dev
->power
&& newp
) {
314 if (test_and_set_bit(NULLB_DEV_FL_UP
, &dev
->flags
))
316 if (null_add_dev(dev
)) {
317 clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
);
321 set_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
323 } else if (dev
->power
&& !newp
) {
324 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
327 null_del_dev(dev
->nullb
);
330 clear_bit(NULLB_DEV_FL_CONFIGURED
, &dev
->flags
);
336 CONFIGFS_ATTR(nullb_device_
, power
);
338 static ssize_t
nullb_device_badblocks_show(struct config_item
*item
, char *page
)
340 struct nullb_device
*t_dev
= to_nullb_device(item
);
342 return badblocks_show(&t_dev
->badblocks
, page
, 0);
345 static ssize_t
nullb_device_badblocks_store(struct config_item
*item
,
346 const char *page
, size_t count
)
348 struct nullb_device
*t_dev
= to_nullb_device(item
);
349 char *orig
, *buf
, *tmp
;
353 orig
= kstrndup(page
, count
, GFP_KERNEL
);
357 buf
= strstrip(orig
);
360 if (buf
[0] != '+' && buf
[0] != '-')
362 tmp
= strchr(&buf
[1], '-');
366 ret
= kstrtoull(buf
+ 1, 0, &start
);
369 ret
= kstrtoull(tmp
+ 1, 0, &end
);
375 /* enable badblocks */
376 cmpxchg(&t_dev
->badblocks
.shift
, -1, 0);
378 ret
= badblocks_set(&t_dev
->badblocks
, start
,
381 ret
= badblocks_clear(&t_dev
->badblocks
, start
,
389 CONFIGFS_ATTR(nullb_device_
, badblocks
);
391 static struct configfs_attribute
*nullb_device_attrs
[] = {
392 &nullb_device_attr_size
,
393 &nullb_device_attr_completion_nsec
,
394 &nullb_device_attr_submit_queues
,
395 &nullb_device_attr_home_node
,
396 &nullb_device_attr_queue_mode
,
397 &nullb_device_attr_blocksize
,
398 &nullb_device_attr_irqmode
,
399 &nullb_device_attr_hw_queue_depth
,
400 &nullb_device_attr_index
,
401 &nullb_device_attr_blocking
,
402 &nullb_device_attr_use_per_node_hctx
,
403 &nullb_device_attr_power
,
404 &nullb_device_attr_memory_backed
,
405 &nullb_device_attr_discard
,
406 &nullb_device_attr_mbps
,
407 &nullb_device_attr_cache_size
,
408 &nullb_device_attr_badblocks
,
409 &nullb_device_attr_zoned
,
410 &nullb_device_attr_zone_size
,
414 static void nullb_device_release(struct config_item
*item
)
416 struct nullb_device
*dev
= to_nullb_device(item
);
418 null_free_device_storage(dev
, false);
422 static struct configfs_item_operations nullb_device_ops
= {
423 .release
= nullb_device_release
,
426 static const struct config_item_type nullb_device_type
= {
427 .ct_item_ops
= &nullb_device_ops
,
428 .ct_attrs
= nullb_device_attrs
,
429 .ct_owner
= THIS_MODULE
,
433 config_item
*nullb_group_make_item(struct config_group
*group
, const char *name
)
435 struct nullb_device
*dev
;
437 dev
= null_alloc_dev();
439 return ERR_PTR(-ENOMEM
);
441 config_item_init_type_name(&dev
->item
, name
, &nullb_device_type
);
447 nullb_group_drop_item(struct config_group
*group
, struct config_item
*item
)
449 struct nullb_device
*dev
= to_nullb_device(item
);
451 if (test_and_clear_bit(NULLB_DEV_FL_UP
, &dev
->flags
)) {
454 null_del_dev(dev
->nullb
);
458 config_item_put(item
);
461 static ssize_t
memb_group_features_show(struct config_item
*item
, char *page
)
463 return snprintf(page
, PAGE_SIZE
, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size\n");
466 CONFIGFS_ATTR_RO(memb_group_
, features
);
468 static struct configfs_attribute
*nullb_group_attrs
[] = {
469 &memb_group_attr_features
,
473 static struct configfs_group_operations nullb_group_ops
= {
474 .make_item
= nullb_group_make_item
,
475 .drop_item
= nullb_group_drop_item
,
478 static const struct config_item_type nullb_group_type
= {
479 .ct_group_ops
= &nullb_group_ops
,
480 .ct_attrs
= nullb_group_attrs
,
481 .ct_owner
= THIS_MODULE
,
484 static struct configfs_subsystem nullb_subsys
= {
487 .ci_namebuf
= "nullb",
488 .ci_type
= &nullb_group_type
,
493 static inline int null_cache_active(struct nullb
*nullb
)
495 return test_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
498 static struct nullb_device
*null_alloc_dev(void)
500 struct nullb_device
*dev
;
502 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
505 INIT_RADIX_TREE(&dev
->data
, GFP_ATOMIC
);
506 INIT_RADIX_TREE(&dev
->cache
, GFP_ATOMIC
);
507 if (badblocks_init(&dev
->badblocks
, 0)) {
512 dev
->size
= g_gb
* 1024;
513 dev
->completion_nsec
= g_completion_nsec
;
514 dev
->submit_queues
= g_submit_queues
;
515 dev
->home_node
= g_home_node
;
516 dev
->queue_mode
= g_queue_mode
;
517 dev
->blocksize
= g_bs
;
518 dev
->irqmode
= g_irqmode
;
519 dev
->hw_queue_depth
= g_hw_queue_depth
;
520 dev
->blocking
= g_blocking
;
521 dev
->use_per_node_hctx
= g_use_per_node_hctx
;
522 dev
->zoned
= g_zoned
;
523 dev
->zone_size
= g_zone_size
;
527 static void null_free_dev(struct nullb_device
*dev
)
533 badblocks_exit(&dev
->badblocks
);
537 static void put_tag(struct nullb_queue
*nq
, unsigned int tag
)
539 clear_bit_unlock(tag
, nq
->tag_map
);
541 if (waitqueue_active(&nq
->wait
))
545 static unsigned int get_tag(struct nullb_queue
*nq
)
550 tag
= find_first_zero_bit(nq
->tag_map
, nq
->queue_depth
);
551 if (tag
>= nq
->queue_depth
)
553 } while (test_and_set_bit_lock(tag
, nq
->tag_map
));
558 static void free_cmd(struct nullb_cmd
*cmd
)
560 put_tag(cmd
->nq
, cmd
->tag
);
563 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
);
565 static struct nullb_cmd
*__alloc_cmd(struct nullb_queue
*nq
)
567 struct nullb_cmd
*cmd
;
572 cmd
= &nq
->cmds
[tag
];
574 cmd
->error
= BLK_STS_OK
;
576 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
577 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
,
579 cmd
->timer
.function
= null_cmd_timer_expired
;
587 static struct nullb_cmd
*alloc_cmd(struct nullb_queue
*nq
, int can_wait
)
589 struct nullb_cmd
*cmd
;
592 cmd
= __alloc_cmd(nq
);
593 if (cmd
|| !can_wait
)
597 prepare_to_wait(&nq
->wait
, &wait
, TASK_UNINTERRUPTIBLE
);
598 cmd
= __alloc_cmd(nq
);
605 finish_wait(&nq
->wait
, &wait
);
609 static void end_cmd(struct nullb_cmd
*cmd
)
611 struct request_queue
*q
= NULL
;
612 int queue_mode
= cmd
->nq
->dev
->queue_mode
;
617 switch (queue_mode
) {
619 blk_mq_end_request(cmd
->rq
, cmd
->error
);
622 INIT_LIST_HEAD(&cmd
->rq
->queuelist
);
623 blk_end_request_all(cmd
->rq
, cmd
->error
);
626 cmd
->bio
->bi_status
= cmd
->error
;
633 /* Restart queue if needed, as we are freeing a tag */
634 if (queue_mode
== NULL_Q_RQ
&& blk_queue_stopped(q
)) {
637 spin_lock_irqsave(q
->queue_lock
, flags
);
638 blk_start_queue_async(q
);
639 spin_unlock_irqrestore(q
->queue_lock
, flags
);
643 static enum hrtimer_restart
null_cmd_timer_expired(struct hrtimer
*timer
)
645 end_cmd(container_of(timer
, struct nullb_cmd
, timer
));
647 return HRTIMER_NORESTART
;
650 static void null_cmd_end_timer(struct nullb_cmd
*cmd
)
652 ktime_t kt
= cmd
->nq
->dev
->completion_nsec
;
654 hrtimer_start(&cmd
->timer
, kt
, HRTIMER_MODE_REL
);
657 static void null_softirq_done_fn(struct request
*rq
)
659 struct nullb
*nullb
= rq
->q
->queuedata
;
661 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
662 end_cmd(blk_mq_rq_to_pdu(rq
));
664 end_cmd(rq
->special
);
667 static struct nullb_page
*null_alloc_page(gfp_t gfp_flags
)
669 struct nullb_page
*t_page
;
671 t_page
= kmalloc(sizeof(struct nullb_page
), gfp_flags
);
675 t_page
->page
= alloc_pages(gfp_flags
, 0);
679 memset(t_page
->bitmap
, 0, sizeof(t_page
->bitmap
));
687 static void null_free_page(struct nullb_page
*t_page
)
689 __set_bit(NULLB_PAGE_FREE
, t_page
->bitmap
);
690 if (test_bit(NULLB_PAGE_LOCK
, t_page
->bitmap
))
692 __free_page(t_page
->page
);
696 static bool null_page_empty(struct nullb_page
*page
)
698 int size
= MAP_SZ
- 2;
700 return find_first_bit(page
->bitmap
, size
) == size
;
703 static void null_free_sector(struct nullb
*nullb
, sector_t sector
,
706 unsigned int sector_bit
;
708 struct nullb_page
*t_page
, *ret
;
709 struct radix_tree_root
*root
;
711 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
712 idx
= sector
>> PAGE_SECTORS_SHIFT
;
713 sector_bit
= (sector
& SECTOR_MASK
);
715 t_page
= radix_tree_lookup(root
, idx
);
717 __clear_bit(sector_bit
, t_page
->bitmap
);
719 if (null_page_empty(t_page
)) {
720 ret
= radix_tree_delete_item(root
, idx
, t_page
);
721 WARN_ON(ret
!= t_page
);
724 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
729 static struct nullb_page
*null_radix_tree_insert(struct nullb
*nullb
, u64 idx
,
730 struct nullb_page
*t_page
, bool is_cache
)
732 struct radix_tree_root
*root
;
734 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
736 if (radix_tree_insert(root
, idx
, t_page
)) {
737 null_free_page(t_page
);
738 t_page
= radix_tree_lookup(root
, idx
);
739 WARN_ON(!t_page
|| t_page
->page
->index
!= idx
);
741 nullb
->dev
->curr_cache
+= PAGE_SIZE
;
746 static void null_free_device_storage(struct nullb_device
*dev
, bool is_cache
)
748 unsigned long pos
= 0;
750 struct nullb_page
*ret
, *t_pages
[FREE_BATCH
];
751 struct radix_tree_root
*root
;
753 root
= is_cache
? &dev
->cache
: &dev
->data
;
758 nr_pages
= radix_tree_gang_lookup(root
,
759 (void **)t_pages
, pos
, FREE_BATCH
);
761 for (i
= 0; i
< nr_pages
; i
++) {
762 pos
= t_pages
[i
]->page
->index
;
763 ret
= radix_tree_delete_item(root
, pos
, t_pages
[i
]);
764 WARN_ON(ret
!= t_pages
[i
]);
769 } while (nr_pages
== FREE_BATCH
);
775 static struct nullb_page
*__null_lookup_page(struct nullb
*nullb
,
776 sector_t sector
, bool for_write
, bool is_cache
)
778 unsigned int sector_bit
;
780 struct nullb_page
*t_page
;
781 struct radix_tree_root
*root
;
783 idx
= sector
>> PAGE_SECTORS_SHIFT
;
784 sector_bit
= (sector
& SECTOR_MASK
);
786 root
= is_cache
? &nullb
->dev
->cache
: &nullb
->dev
->data
;
787 t_page
= radix_tree_lookup(root
, idx
);
788 WARN_ON(t_page
&& t_page
->page
->index
!= idx
);
790 if (t_page
&& (for_write
|| test_bit(sector_bit
, t_page
->bitmap
)))
796 static struct nullb_page
*null_lookup_page(struct nullb
*nullb
,
797 sector_t sector
, bool for_write
, bool ignore_cache
)
799 struct nullb_page
*page
= NULL
;
802 page
= __null_lookup_page(nullb
, sector
, for_write
, true);
805 return __null_lookup_page(nullb
, sector
, for_write
, false);
808 static struct nullb_page
*null_insert_page(struct nullb
*nullb
,
809 sector_t sector
, bool ignore_cache
)
810 __releases(&nullb
->lock
)
811 __acquires(&nullb
->lock
)
814 struct nullb_page
*t_page
;
816 t_page
= null_lookup_page(nullb
, sector
, true, ignore_cache
);
820 spin_unlock_irq(&nullb
->lock
);
822 t_page
= null_alloc_page(GFP_NOIO
);
826 if (radix_tree_preload(GFP_NOIO
))
829 spin_lock_irq(&nullb
->lock
);
830 idx
= sector
>> PAGE_SECTORS_SHIFT
;
831 t_page
->page
->index
= idx
;
832 t_page
= null_radix_tree_insert(nullb
, idx
, t_page
, !ignore_cache
);
833 radix_tree_preload_end();
837 null_free_page(t_page
);
839 spin_lock_irq(&nullb
->lock
);
840 return null_lookup_page(nullb
, sector
, true, ignore_cache
);
843 static int null_flush_cache_page(struct nullb
*nullb
, struct nullb_page
*c_page
)
848 struct nullb_page
*t_page
, *ret
;
851 idx
= c_page
->page
->index
;
853 t_page
= null_insert_page(nullb
, idx
<< PAGE_SECTORS_SHIFT
, true);
855 __clear_bit(NULLB_PAGE_LOCK
, c_page
->bitmap
);
856 if (test_bit(NULLB_PAGE_FREE
, c_page
->bitmap
)) {
857 null_free_page(c_page
);
858 if (t_page
&& null_page_empty(t_page
)) {
859 ret
= radix_tree_delete_item(&nullb
->dev
->data
,
861 null_free_page(t_page
);
869 src
= kmap_atomic(c_page
->page
);
870 dst
= kmap_atomic(t_page
->page
);
872 for (i
= 0; i
< PAGE_SECTORS
;
873 i
+= (nullb
->dev
->blocksize
>> SECTOR_SHIFT
)) {
874 if (test_bit(i
, c_page
->bitmap
)) {
875 offset
= (i
<< SECTOR_SHIFT
);
876 memcpy(dst
+ offset
, src
+ offset
,
877 nullb
->dev
->blocksize
);
878 __set_bit(i
, t_page
->bitmap
);
885 ret
= radix_tree_delete_item(&nullb
->dev
->cache
, idx
, c_page
);
887 nullb
->dev
->curr_cache
-= PAGE_SIZE
;
892 static int null_make_cache_space(struct nullb
*nullb
, unsigned long n
)
894 int i
, err
, nr_pages
;
895 struct nullb_page
*c_pages
[FREE_BATCH
];
896 unsigned long flushed
= 0, one_round
;
899 if ((nullb
->dev
->cache_size
* 1024 * 1024) >
900 nullb
->dev
->curr_cache
+ n
|| nullb
->dev
->curr_cache
== 0)
903 nr_pages
= radix_tree_gang_lookup(&nullb
->dev
->cache
,
904 (void **)c_pages
, nullb
->cache_flush_pos
, FREE_BATCH
);
906 * nullb_flush_cache_page could unlock before using the c_pages. To
907 * avoid race, we don't allow page free
909 for (i
= 0; i
< nr_pages
; i
++) {
910 nullb
->cache_flush_pos
= c_pages
[i
]->page
->index
;
912 * We found the page which is being flushed to disk by other
915 if (test_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
))
918 __set_bit(NULLB_PAGE_LOCK
, c_pages
[i
]->bitmap
);
922 for (i
= 0; i
< nr_pages
; i
++) {
923 if (c_pages
[i
] == NULL
)
925 err
= null_flush_cache_page(nullb
, c_pages
[i
]);
930 flushed
+= one_round
<< PAGE_SHIFT
;
934 nullb
->cache_flush_pos
= 0;
935 if (one_round
== 0) {
936 /* give other threads a chance */
937 spin_unlock_irq(&nullb
->lock
);
938 spin_lock_irq(&nullb
->lock
);
945 static int copy_to_nullb(struct nullb
*nullb
, struct page
*source
,
946 unsigned int off
, sector_t sector
, size_t n
, bool is_fua
)
948 size_t temp
, count
= 0;
950 struct nullb_page
*t_page
;
954 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
956 if (null_cache_active(nullb
) && !is_fua
)
957 null_make_cache_space(nullb
, PAGE_SIZE
);
959 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
960 t_page
= null_insert_page(nullb
, sector
,
961 !null_cache_active(nullb
) || is_fua
);
965 src
= kmap_atomic(source
);
966 dst
= kmap_atomic(t_page
->page
);
967 memcpy(dst
+ offset
, src
+ off
+ count
, temp
);
971 __set_bit(sector
& SECTOR_MASK
, t_page
->bitmap
);
974 null_free_sector(nullb
, sector
, true);
977 sector
+= temp
>> SECTOR_SHIFT
;
982 static int copy_from_nullb(struct nullb
*nullb
, struct page
*dest
,
983 unsigned int off
, sector_t sector
, size_t n
)
985 size_t temp
, count
= 0;
987 struct nullb_page
*t_page
;
991 temp
= min_t(size_t, nullb
->dev
->blocksize
, n
- count
);
993 offset
= (sector
& SECTOR_MASK
) << SECTOR_SHIFT
;
994 t_page
= null_lookup_page(nullb
, sector
, false,
995 !null_cache_active(nullb
));
997 dst
= kmap_atomic(dest
);
999 memset(dst
+ off
+ count
, 0, temp
);
1002 src
= kmap_atomic(t_page
->page
);
1003 memcpy(dst
+ off
+ count
, src
+ offset
, temp
);
1009 sector
+= temp
>> SECTOR_SHIFT
;
1014 static void null_handle_discard(struct nullb
*nullb
, sector_t sector
, size_t n
)
1018 spin_lock_irq(&nullb
->lock
);
1020 temp
= min_t(size_t, n
, nullb
->dev
->blocksize
);
1021 null_free_sector(nullb
, sector
, false);
1022 if (null_cache_active(nullb
))
1023 null_free_sector(nullb
, sector
, true);
1024 sector
+= temp
>> SECTOR_SHIFT
;
1027 spin_unlock_irq(&nullb
->lock
);
1030 static int null_handle_flush(struct nullb
*nullb
)
1034 if (!null_cache_active(nullb
))
1037 spin_lock_irq(&nullb
->lock
);
1039 err
= null_make_cache_space(nullb
,
1040 nullb
->dev
->cache_size
* 1024 * 1024);
1041 if (err
|| nullb
->dev
->curr_cache
== 0)
1045 WARN_ON(!radix_tree_empty(&nullb
->dev
->cache
));
1046 spin_unlock_irq(&nullb
->lock
);
1050 static int null_transfer(struct nullb
*nullb
, struct page
*page
,
1051 unsigned int len
, unsigned int off
, bool is_write
, sector_t sector
,
1057 err
= copy_from_nullb(nullb
, page
, off
, sector
, len
);
1058 flush_dcache_page(page
);
1060 flush_dcache_page(page
);
1061 err
= copy_to_nullb(nullb
, page
, off
, sector
, len
, is_fua
);
1067 static int null_handle_rq(struct nullb_cmd
*cmd
)
1069 struct request
*rq
= cmd
->rq
;
1070 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1074 struct req_iterator iter
;
1075 struct bio_vec bvec
;
1077 sector
= blk_rq_pos(rq
);
1079 if (req_op(rq
) == REQ_OP_DISCARD
) {
1080 null_handle_discard(nullb
, sector
, blk_rq_bytes(rq
));
1084 spin_lock_irq(&nullb
->lock
);
1085 rq_for_each_segment(bvec
, rq
, iter
) {
1087 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1088 op_is_write(req_op(rq
)), sector
,
1089 req_op(rq
) & REQ_FUA
);
1091 spin_unlock_irq(&nullb
->lock
);
1094 sector
+= len
>> SECTOR_SHIFT
;
1096 spin_unlock_irq(&nullb
->lock
);
1101 static int null_handle_bio(struct nullb_cmd
*cmd
)
1103 struct bio
*bio
= cmd
->bio
;
1104 struct nullb
*nullb
= cmd
->nq
->dev
->nullb
;
1108 struct bio_vec bvec
;
1109 struct bvec_iter iter
;
1111 sector
= bio
->bi_iter
.bi_sector
;
1113 if (bio_op(bio
) == REQ_OP_DISCARD
) {
1114 null_handle_discard(nullb
, sector
,
1115 bio_sectors(bio
) << SECTOR_SHIFT
);
1119 spin_lock_irq(&nullb
->lock
);
1120 bio_for_each_segment(bvec
, bio
, iter
) {
1122 err
= null_transfer(nullb
, bvec
.bv_page
, len
, bvec
.bv_offset
,
1123 op_is_write(bio_op(bio
)), sector
,
1124 bio_op(bio
) & REQ_FUA
);
1126 spin_unlock_irq(&nullb
->lock
);
1129 sector
+= len
>> SECTOR_SHIFT
;
1131 spin_unlock_irq(&nullb
->lock
);
1135 static void null_stop_queue(struct nullb
*nullb
)
1137 struct request_queue
*q
= nullb
->q
;
1139 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1140 blk_mq_stop_hw_queues(q
);
1142 spin_lock_irq(q
->queue_lock
);
1144 spin_unlock_irq(q
->queue_lock
);
1148 static void null_restart_queue_async(struct nullb
*nullb
)
1150 struct request_queue
*q
= nullb
->q
;
1151 unsigned long flags
;
1153 if (nullb
->dev
->queue_mode
== NULL_Q_MQ
)
1154 blk_mq_start_stopped_hw_queues(q
, true);
1156 spin_lock_irqsave(q
->queue_lock
, flags
);
1157 blk_start_queue_async(q
);
1158 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1162 static bool cmd_report_zone(struct nullb
*nullb
, struct nullb_cmd
*cmd
)
1164 struct nullb_device
*dev
= cmd
->nq
->dev
;
1166 if (dev
->queue_mode
== NULL_Q_BIO
) {
1167 if (bio_op(cmd
->bio
) == REQ_OP_ZONE_REPORT
) {
1168 cmd
->error
= null_zone_report(nullb
, cmd
->bio
);
1172 if (req_op(cmd
->rq
) == REQ_OP_ZONE_REPORT
) {
1173 cmd
->error
= null_zone_report(nullb
, cmd
->rq
->bio
);
1181 static blk_status_t
null_handle_cmd(struct nullb_cmd
*cmd
)
1183 struct nullb_device
*dev
= cmd
->nq
->dev
;
1184 struct nullb
*nullb
= dev
->nullb
;
1187 if (cmd_report_zone(nullb
, cmd
))
1190 if (test_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
)) {
1191 struct request
*rq
= cmd
->rq
;
1193 if (!hrtimer_active(&nullb
->bw_timer
))
1194 hrtimer_restart(&nullb
->bw_timer
);
1196 if (atomic_long_sub_return(blk_rq_bytes(rq
),
1197 &nullb
->cur_bytes
) < 0) {
1198 null_stop_queue(nullb
);
1199 /* race with timer */
1200 if (atomic_long_read(&nullb
->cur_bytes
) > 0)
1201 null_restart_queue_async(nullb
);
1202 if (dev
->queue_mode
== NULL_Q_RQ
) {
1203 struct request_queue
*q
= nullb
->q
;
1205 spin_lock_irq(q
->queue_lock
);
1206 rq
->rq_flags
|= RQF_DONTPREP
;
1207 blk_requeue_request(q
, rq
);
1208 spin_unlock_irq(q
->queue_lock
);
1211 /* requeue request */
1212 return BLK_STS_DEV_RESOURCE
;
1216 if (nullb
->dev
->badblocks
.shift
!= -1) {
1218 sector_t sector
, size
, first_bad
;
1219 bool is_flush
= true;
1221 if (dev
->queue_mode
== NULL_Q_BIO
&&
1222 bio_op(cmd
->bio
) != REQ_OP_FLUSH
) {
1224 sector
= cmd
->bio
->bi_iter
.bi_sector
;
1225 size
= bio_sectors(cmd
->bio
);
1227 if (dev
->queue_mode
!= NULL_Q_BIO
&&
1228 req_op(cmd
->rq
) != REQ_OP_FLUSH
) {
1230 sector
= blk_rq_pos(cmd
->rq
);
1231 size
= blk_rq_sectors(cmd
->rq
);
1233 if (!is_flush
&& badblocks_check(&nullb
->dev
->badblocks
, sector
,
1234 size
, &first_bad
, &bad_sectors
)) {
1235 cmd
->error
= BLK_STS_IOERR
;
1240 if (dev
->memory_backed
) {
1241 if (dev
->queue_mode
== NULL_Q_BIO
) {
1242 if (bio_op(cmd
->bio
) == REQ_OP_FLUSH
)
1243 err
= null_handle_flush(nullb
);
1245 err
= null_handle_bio(cmd
);
1247 if (req_op(cmd
->rq
) == REQ_OP_FLUSH
)
1248 err
= null_handle_flush(nullb
);
1250 err
= null_handle_rq(cmd
);
1253 cmd
->error
= errno_to_blk_status(err
);
1255 if (!cmd
->error
&& dev
->zoned
) {
1257 unsigned int nr_sectors
;
1260 if (dev
->queue_mode
== NULL_Q_BIO
) {
1261 op
= bio_op(cmd
->bio
);
1262 sector
= cmd
->bio
->bi_iter
.bi_sector
;
1263 nr_sectors
= cmd
->bio
->bi_iter
.bi_size
>> 9;
1265 op
= req_op(cmd
->rq
);
1266 sector
= blk_rq_pos(cmd
->rq
);
1267 nr_sectors
= blk_rq_sectors(cmd
->rq
);
1270 if (op
== REQ_OP_WRITE
)
1271 null_zone_write(cmd
, sector
, nr_sectors
);
1272 else if (op
== REQ_OP_ZONE_RESET
)
1273 null_zone_reset(cmd
, sector
);
1276 /* Complete IO by inline, softirq or timer */
1277 switch (dev
->irqmode
) {
1278 case NULL_IRQ_SOFTIRQ
:
1279 switch (dev
->queue_mode
) {
1281 blk_mq_complete_request(cmd
->rq
);
1284 blk_complete_request(cmd
->rq
);
1288 * XXX: no proper submitting cpu information available.
1297 case NULL_IRQ_TIMER
:
1298 null_cmd_end_timer(cmd
);
1304 static enum hrtimer_restart
nullb_bwtimer_fn(struct hrtimer
*timer
)
1306 struct nullb
*nullb
= container_of(timer
, struct nullb
, bw_timer
);
1307 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1308 unsigned int mbps
= nullb
->dev
->mbps
;
1310 if (atomic_long_read(&nullb
->cur_bytes
) == mb_per_tick(mbps
))
1311 return HRTIMER_NORESTART
;
1313 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(mbps
));
1314 null_restart_queue_async(nullb
);
1316 hrtimer_forward_now(&nullb
->bw_timer
, timer_interval
);
1318 return HRTIMER_RESTART
;
1321 static void nullb_setup_bwtimer(struct nullb
*nullb
)
1323 ktime_t timer_interval
= ktime_set(0, TIMER_INTERVAL
);
1325 hrtimer_init(&nullb
->bw_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1326 nullb
->bw_timer
.function
= nullb_bwtimer_fn
;
1327 atomic_long_set(&nullb
->cur_bytes
, mb_per_tick(nullb
->dev
->mbps
));
1328 hrtimer_start(&nullb
->bw_timer
, timer_interval
, HRTIMER_MODE_REL
);
1331 static struct nullb_queue
*nullb_to_queue(struct nullb
*nullb
)
1335 if (nullb
->nr_queues
!= 1)
1336 index
= raw_smp_processor_id() / ((nr_cpu_ids
+ nullb
->nr_queues
- 1) / nullb
->nr_queues
);
1338 return &nullb
->queues
[index
];
1341 static blk_qc_t
null_queue_bio(struct request_queue
*q
, struct bio
*bio
)
1343 struct nullb
*nullb
= q
->queuedata
;
1344 struct nullb_queue
*nq
= nullb_to_queue(nullb
);
1345 struct nullb_cmd
*cmd
;
1347 cmd
= alloc_cmd(nq
, 1);
1350 null_handle_cmd(cmd
);
1351 return BLK_QC_T_NONE
;
1354 static enum blk_eh_timer_return
null_rq_timed_out_fn(struct request
*rq
)
1356 pr_info("null: rq %p timed out\n", rq
);
1357 __blk_complete_request(rq
);
1361 static int null_rq_prep_fn(struct request_queue
*q
, struct request
*req
)
1363 struct nullb
*nullb
= q
->queuedata
;
1364 struct nullb_queue
*nq
= nullb_to_queue(nullb
);
1365 struct nullb_cmd
*cmd
;
1367 cmd
= alloc_cmd(nq
, 0);
1375 return BLKPREP_DEFER
;
1378 static bool should_timeout_request(struct request
*rq
)
1380 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1381 if (g_timeout_str
[0])
1382 return should_fail(&null_timeout_attr
, 1);
1387 static bool should_requeue_request(struct request
*rq
)
1389 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1390 if (g_requeue_str
[0])
1391 return should_fail(&null_requeue_attr
, 1);
1396 static void null_request_fn(struct request_queue
*q
)
1400 while ((rq
= blk_fetch_request(q
)) != NULL
) {
1401 struct nullb_cmd
*cmd
= rq
->special
;
1403 /* just ignore the request */
1404 if (should_timeout_request(rq
))
1406 if (should_requeue_request(rq
)) {
1407 blk_requeue_request(q
, rq
);
1411 spin_unlock_irq(q
->queue_lock
);
1412 null_handle_cmd(cmd
);
1413 spin_lock_irq(q
->queue_lock
);
1417 static enum blk_eh_timer_return
null_timeout_rq(struct request
*rq
, bool res
)
1419 pr_info("null: rq %p timed out\n", rq
);
1420 blk_mq_complete_request(rq
);
1424 static blk_status_t
null_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1425 const struct blk_mq_queue_data
*bd
)
1427 struct nullb_cmd
*cmd
= blk_mq_rq_to_pdu(bd
->rq
);
1428 struct nullb_queue
*nq
= hctx
->driver_data
;
1430 might_sleep_if(hctx
->flags
& BLK_MQ_F_BLOCKING
);
1432 if (nq
->dev
->irqmode
== NULL_IRQ_TIMER
) {
1433 hrtimer_init(&cmd
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1434 cmd
->timer
.function
= null_cmd_timer_expired
;
1437 cmd
->error
= BLK_STS_OK
;
1440 blk_mq_start_request(bd
->rq
);
1442 if (should_requeue_request(bd
->rq
)) {
1444 * Alternate between hitting the core BUSY path, and the
1445 * driver driven requeue path
1447 nq
->requeue_selection
++;
1448 if (nq
->requeue_selection
& 1)
1449 return BLK_STS_RESOURCE
;
1451 blk_mq_requeue_request(bd
->rq
, true);
1455 if (should_timeout_request(bd
->rq
))
1458 return null_handle_cmd(cmd
);
1461 static const struct blk_mq_ops null_mq_ops
= {
1462 .queue_rq
= null_queue_rq
,
1463 .complete
= null_softirq_done_fn
,
1464 .timeout
= null_timeout_rq
,
1467 static void cleanup_queue(struct nullb_queue
*nq
)
1473 static void cleanup_queues(struct nullb
*nullb
)
1477 for (i
= 0; i
< nullb
->nr_queues
; i
++)
1478 cleanup_queue(&nullb
->queues
[i
]);
1480 kfree(nullb
->queues
);
1483 static void null_del_dev(struct nullb
*nullb
)
1485 struct nullb_device
*dev
;
1492 ida_simple_remove(&nullb_indexes
, nullb
->index
);
1494 list_del_init(&nullb
->list
);
1496 del_gendisk(nullb
->disk
);
1498 if (test_bit(NULLB_DEV_FL_THROTTLED
, &nullb
->dev
->flags
)) {
1499 hrtimer_cancel(&nullb
->bw_timer
);
1500 atomic_long_set(&nullb
->cur_bytes
, LONG_MAX
);
1501 null_restart_queue_async(nullb
);
1504 blk_cleanup_queue(nullb
->q
);
1505 if (dev
->queue_mode
== NULL_Q_MQ
&&
1506 nullb
->tag_set
== &nullb
->__tag_set
)
1507 blk_mq_free_tag_set(nullb
->tag_set
);
1508 put_disk(nullb
->disk
);
1509 cleanup_queues(nullb
);
1510 if (null_cache_active(nullb
))
1511 null_free_device_storage(nullb
->dev
, true);
1516 static void null_config_discard(struct nullb
*nullb
)
1518 if (nullb
->dev
->discard
== false)
1520 nullb
->q
->limits
.discard_granularity
= nullb
->dev
->blocksize
;
1521 nullb
->q
->limits
.discard_alignment
= nullb
->dev
->blocksize
;
1522 blk_queue_max_discard_sectors(nullb
->q
, UINT_MAX
>> 9);
1523 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, nullb
->q
);
1526 static int null_open(struct block_device
*bdev
, fmode_t mode
)
1531 static void null_release(struct gendisk
*disk
, fmode_t mode
)
1535 static const struct block_device_operations null_fops
= {
1536 .owner
= THIS_MODULE
,
1538 .release
= null_release
,
1541 static void null_init_queue(struct nullb
*nullb
, struct nullb_queue
*nq
)
1546 init_waitqueue_head(&nq
->wait
);
1547 nq
->queue_depth
= nullb
->queue_depth
;
1548 nq
->dev
= nullb
->dev
;
1551 static void null_init_queues(struct nullb
*nullb
)
1553 struct request_queue
*q
= nullb
->q
;
1554 struct blk_mq_hw_ctx
*hctx
;
1555 struct nullb_queue
*nq
;
1558 queue_for_each_hw_ctx(q
, hctx
, i
) {
1559 if (!hctx
->nr_ctx
|| !hctx
->tags
)
1561 nq
= &nullb
->queues
[i
];
1562 hctx
->driver_data
= nq
;
1563 null_init_queue(nullb
, nq
);
1568 static int setup_commands(struct nullb_queue
*nq
)
1570 struct nullb_cmd
*cmd
;
1573 nq
->cmds
= kcalloc(nq
->queue_depth
, sizeof(*cmd
), GFP_KERNEL
);
1577 tag_size
= ALIGN(nq
->queue_depth
, BITS_PER_LONG
) / BITS_PER_LONG
;
1578 nq
->tag_map
= kcalloc(tag_size
, sizeof(unsigned long), GFP_KERNEL
);
1584 for (i
= 0; i
< nq
->queue_depth
; i
++) {
1586 INIT_LIST_HEAD(&cmd
->list
);
1587 cmd
->ll_list
.next
= NULL
;
1594 static int setup_queues(struct nullb
*nullb
)
1596 nullb
->queues
= kcalloc(nullb
->dev
->submit_queues
,
1597 sizeof(struct nullb_queue
),
1602 nullb
->nr_queues
= 0;
1603 nullb
->queue_depth
= nullb
->dev
->hw_queue_depth
;
1608 static int init_driver_queues(struct nullb
*nullb
)
1610 struct nullb_queue
*nq
;
1613 for (i
= 0; i
< nullb
->dev
->submit_queues
; i
++) {
1614 nq
= &nullb
->queues
[i
];
1616 null_init_queue(nullb
, nq
);
1618 ret
= setup_commands(nq
);
1626 static int null_gendisk_register(struct nullb
*nullb
)
1628 struct gendisk
*disk
;
1631 disk
= nullb
->disk
= alloc_disk_node(1, nullb
->dev
->home_node
);
1634 size
= (sector_t
)nullb
->dev
->size
* 1024 * 1024ULL;
1635 set_capacity(disk
, size
>> 9);
1637 disk
->flags
|= GENHD_FL_EXT_DEVT
| GENHD_FL_SUPPRESS_PARTITION_INFO
;
1638 disk
->major
= null_major
;
1639 disk
->first_minor
= nullb
->index
;
1640 disk
->fops
= &null_fops
;
1641 disk
->private_data
= nullb
;
1642 disk
->queue
= nullb
->q
;
1643 strncpy(disk
->disk_name
, nullb
->disk_name
, DISK_NAME_LEN
);
1649 static int null_init_tag_set(struct nullb
*nullb
, struct blk_mq_tag_set
*set
)
1651 set
->ops
= &null_mq_ops
;
1652 set
->nr_hw_queues
= nullb
? nullb
->dev
->submit_queues
:
1654 set
->queue_depth
= nullb
? nullb
->dev
->hw_queue_depth
:
1656 set
->numa_node
= nullb
? nullb
->dev
->home_node
: g_home_node
;
1657 set
->cmd_size
= sizeof(struct nullb_cmd
);
1658 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
1660 set
->flags
|= BLK_MQ_F_NO_SCHED
;
1661 set
->driver_data
= NULL
;
1663 if ((nullb
&& nullb
->dev
->blocking
) || g_blocking
)
1664 set
->flags
|= BLK_MQ_F_BLOCKING
;
1666 return blk_mq_alloc_tag_set(set
);
1669 static void null_validate_conf(struct nullb_device
*dev
)
1671 dev
->blocksize
= round_down(dev
->blocksize
, 512);
1672 dev
->blocksize
= clamp_t(unsigned int, dev
->blocksize
, 512, 4096);
1674 if (dev
->queue_mode
== NULL_Q_MQ
&& dev
->use_per_node_hctx
) {
1675 if (dev
->submit_queues
!= nr_online_nodes
)
1676 dev
->submit_queues
= nr_online_nodes
;
1677 } else if (dev
->submit_queues
> nr_cpu_ids
)
1678 dev
->submit_queues
= nr_cpu_ids
;
1679 else if (dev
->submit_queues
== 0)
1680 dev
->submit_queues
= 1;
1682 dev
->queue_mode
= min_t(unsigned int, dev
->queue_mode
, NULL_Q_MQ
);
1683 dev
->irqmode
= min_t(unsigned int, dev
->irqmode
, NULL_IRQ_TIMER
);
1685 /* Do memory allocation, so set blocking */
1686 if (dev
->memory_backed
)
1687 dev
->blocking
= true;
1688 else /* cache is meaningless */
1689 dev
->cache_size
= 0;
1690 dev
->cache_size
= min_t(unsigned long, ULONG_MAX
/ 1024 / 1024,
1692 dev
->mbps
= min_t(unsigned int, 1024 * 40, dev
->mbps
);
1693 /* can not stop a queue */
1694 if (dev
->queue_mode
== NULL_Q_BIO
)
1698 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1699 static bool __null_setup_fault(struct fault_attr
*attr
, char *str
)
1704 if (!setup_fault_attr(attr
, str
))
1712 static bool null_setup_fault(void)
1714 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1715 if (!__null_setup_fault(&null_timeout_attr
, g_timeout_str
))
1717 if (!__null_setup_fault(&null_requeue_attr
, g_requeue_str
))
1723 static int null_add_dev(struct nullb_device
*dev
)
1725 struct nullb
*nullb
;
1728 null_validate_conf(dev
);
1730 nullb
= kzalloc_node(sizeof(*nullb
), GFP_KERNEL
, dev
->home_node
);
1738 spin_lock_init(&nullb
->lock
);
1740 rv
= setup_queues(nullb
);
1742 goto out_free_nullb
;
1744 if (dev
->queue_mode
== NULL_Q_MQ
) {
1746 nullb
->tag_set
= &tag_set
;
1749 nullb
->tag_set
= &nullb
->__tag_set
;
1750 rv
= null_init_tag_set(nullb
, nullb
->tag_set
);
1754 goto out_cleanup_queues
;
1756 if (!null_setup_fault())
1757 goto out_cleanup_queues
;
1759 nullb
->tag_set
->timeout
= 5 * HZ
;
1760 nullb
->q
= blk_mq_init_queue(nullb
->tag_set
);
1761 if (IS_ERR(nullb
->q
)) {
1763 goto out_cleanup_tags
;
1765 null_init_queues(nullb
);
1766 } else if (dev
->queue_mode
== NULL_Q_BIO
) {
1767 nullb
->q
= blk_alloc_queue_node(GFP_KERNEL
, dev
->home_node
,
1771 goto out_cleanup_queues
;
1773 blk_queue_make_request(nullb
->q
, null_queue_bio
);
1774 rv
= init_driver_queues(nullb
);
1776 goto out_cleanup_blk_queue
;
1778 nullb
->q
= blk_init_queue_node(null_request_fn
, &nullb
->lock
,
1782 goto out_cleanup_queues
;
1785 if (!null_setup_fault())
1786 goto out_cleanup_blk_queue
;
1788 blk_queue_prep_rq(nullb
->q
, null_rq_prep_fn
);
1789 blk_queue_softirq_done(nullb
->q
, null_softirq_done_fn
);
1790 blk_queue_rq_timed_out(nullb
->q
, null_rq_timed_out_fn
);
1791 nullb
->q
->rq_timeout
= 5 * HZ
;
1792 rv
= init_driver_queues(nullb
);
1794 goto out_cleanup_blk_queue
;
1798 set_bit(NULLB_DEV_FL_THROTTLED
, &dev
->flags
);
1799 nullb_setup_bwtimer(nullb
);
1802 if (dev
->cache_size
> 0) {
1803 set_bit(NULLB_DEV_FL_CACHE
, &nullb
->dev
->flags
);
1804 blk_queue_write_cache(nullb
->q
, true, true);
1805 blk_queue_flush_queueable(nullb
->q
, true);
1809 rv
= null_zone_init(dev
);
1811 goto out_cleanup_blk_queue
;
1813 blk_queue_chunk_sectors(nullb
->q
, dev
->zone_size_sects
);
1814 nullb
->q
->limits
.zoned
= BLK_ZONED_HM
;
1817 nullb
->q
->queuedata
= nullb
;
1818 blk_queue_flag_set(QUEUE_FLAG_NONROT
, nullb
->q
);
1819 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, nullb
->q
);
1822 nullb
->index
= ida_simple_get(&nullb_indexes
, 0, 0, GFP_KERNEL
);
1823 dev
->index
= nullb
->index
;
1824 mutex_unlock(&lock
);
1826 blk_queue_logical_block_size(nullb
->q
, dev
->blocksize
);
1827 blk_queue_physical_block_size(nullb
->q
, dev
->blocksize
);
1829 null_config_discard(nullb
);
1831 sprintf(nullb
->disk_name
, "nullb%d", nullb
->index
);
1833 rv
= null_gendisk_register(nullb
);
1835 goto out_cleanup_zone
;
1838 list_add_tail(&nullb
->list
, &nullb_list
);
1839 mutex_unlock(&lock
);
1844 null_zone_exit(dev
);
1845 out_cleanup_blk_queue
:
1846 blk_cleanup_queue(nullb
->q
);
1848 if (dev
->queue_mode
== NULL_Q_MQ
&& nullb
->tag_set
== &nullb
->__tag_set
)
1849 blk_mq_free_tag_set(nullb
->tag_set
);
1851 cleanup_queues(nullb
);
1859 static int __init
null_init(void)
1863 struct nullb
*nullb
;
1864 struct nullb_device
*dev
;
1866 if (g_bs
> PAGE_SIZE
) {
1867 pr_warn("null_blk: invalid block size\n");
1868 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE
);
1872 if (!is_power_of_2(g_zone_size
)) {
1873 pr_err("null_blk: zone_size must be power-of-two\n");
1877 if (g_queue_mode
== NULL_Q_MQ
&& g_use_per_node_hctx
) {
1878 if (g_submit_queues
!= nr_online_nodes
) {
1879 pr_warn("null_blk: submit_queues param is set to %u.\n",
1881 g_submit_queues
= nr_online_nodes
;
1883 } else if (g_submit_queues
> nr_cpu_ids
)
1884 g_submit_queues
= nr_cpu_ids
;
1885 else if (g_submit_queues
<= 0)
1886 g_submit_queues
= 1;
1888 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
) {
1889 ret
= null_init_tag_set(NULL
, &tag_set
);
1894 config_group_init(&nullb_subsys
.su_group
);
1895 mutex_init(&nullb_subsys
.su_mutex
);
1897 ret
= configfs_register_subsystem(&nullb_subsys
);
1903 null_major
= register_blkdev(0, "nullb");
1904 if (null_major
< 0) {
1909 for (i
= 0; i
< nr_devices
; i
++) {
1910 dev
= null_alloc_dev();
1915 ret
= null_add_dev(dev
);
1922 pr_info("null: module loaded\n");
1926 while (!list_empty(&nullb_list
)) {
1927 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
1929 null_del_dev(nullb
);
1932 unregister_blkdev(null_major
, "nullb");
1934 configfs_unregister_subsystem(&nullb_subsys
);
1936 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
1937 blk_mq_free_tag_set(&tag_set
);
1941 static void __exit
null_exit(void)
1943 struct nullb
*nullb
;
1945 configfs_unregister_subsystem(&nullb_subsys
);
1947 unregister_blkdev(null_major
, "nullb");
1950 while (!list_empty(&nullb_list
)) {
1951 struct nullb_device
*dev
;
1953 nullb
= list_entry(nullb_list
.next
, struct nullb
, list
);
1955 null_del_dev(nullb
);
1958 mutex_unlock(&lock
);
1960 if (g_queue_mode
== NULL_Q_MQ
&& shared_tags
)
1961 blk_mq_free_tag_set(&tag_set
);
1964 module_init(null_init
);
1965 module_exit(null_exit
);
1967 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
1968 MODULE_LICENSE("GPL");