2019-05-19 05:08:20 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-08-14 15:04:53 -07:00
|
|
|
/*
|
|
|
|
* Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
|
|
|
|
* Shaohua Li <shli@fb.com>
|
|
|
|
*/
|
2013-10-25 03:52:25 -07:00
|
|
|
#include <linux/module.h>
|
2013-12-20 16:11:01 -07:00
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/init.h>
|
2018-07-06 10:38:38 -07:00
|
|
|
#include "null_blk.h"
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2022-04-19 17:57:17 -07:00
|
|
|
#undef pr_fmt
|
|
|
|
#define pr_fmt(fmt) "null_blk: " fmt
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
#define FREE_BATCH 16
|
|
|
|
|
2017-08-14 15:04:58 -07:00
|
|
|
#define TICKS_PER_SEC 50ULL
|
|
|
|
#define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
|
|
|
|
|
2018-01-11 03:31:25 -07:00
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
2018-01-10 09:06:23 -07:00
|
|
|
static DECLARE_FAULT_ATTR(null_timeout_attr);
|
2018-02-28 09:18:57 -07:00
|
|
|
static DECLARE_FAULT_ATTR(null_requeue_attr);
|
2020-03-09 21:26:23 -07:00
|
|
|
static DECLARE_FAULT_ATTR(null_init_hctx_attr);
|
2018-01-11 03:31:25 -07:00
|
|
|
#endif
|
2018-01-10 09:06:23 -07:00
|
|
|
|
2017-08-14 15:04:58 -07:00
|
|
|
static inline u64 mb_per_tick(int mbps)
|
|
|
|
{
|
|
|
|
return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
|
|
|
|
}
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
/*
|
|
|
|
* Status flags for nullb_device.
|
|
|
|
*
|
|
|
|
* CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
|
|
|
|
* UP: Device is currently on and visible in userspace.
|
2017-08-14 15:04:58 -07:00
|
|
|
* THROTTLED: Device is being throttled.
|
2017-08-14 15:04:59 -07:00
|
|
|
* CACHE: Device is using a write-back cache.
|
2017-08-14 15:04:53 -07:00
|
|
|
*/
|
|
|
|
enum nullb_device_flags {
|
|
|
|
NULLB_DEV_FL_CONFIGURED = 0,
|
|
|
|
NULLB_DEV_FL_UP = 1,
|
2017-08-14 15:04:58 -07:00
|
|
|
NULLB_DEV_FL_THROTTLED = 2,
|
2017-08-14 15:04:59 -07:00
|
|
|
NULLB_DEV_FL_CACHE = 3,
|
2017-08-14 15:04:53 -07:00
|
|
|
};
|
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
#define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
|
2017-08-14 15:04:56 -07:00
|
|
|
/*
|
|
|
|
* nullb_page is a page in memory for nullb devices.
|
|
|
|
*
|
|
|
|
* @page: The page holding the data.
|
|
|
|
* @bitmap: The bitmap represents which sector in the page has data.
|
|
|
|
* Each bit represents one block size. For example, sector 8
|
|
|
|
* will use the 7th bit
|
2017-08-14 15:04:59 -07:00
|
|
|
* The highest 2 bits of bitmap are for special purpose. LOCK means the cache
|
|
|
|
* page is being flushing to storage. FREE means the cache page is freed and
|
|
|
|
* should be skipped from flushing to storage. Please see
|
|
|
|
* null_make_cache_space
|
2017-08-14 15:04:56 -07:00
|
|
|
*/
|
|
|
|
struct nullb_page {
|
|
|
|
struct page *page;
|
2018-03-05 21:07:13 -07:00
|
|
|
DECLARE_BITMAP(bitmap, MAP_SZ);
|
2017-08-14 15:04:56 -07:00
|
|
|
};
|
2018-03-05 21:07:13 -07:00
|
|
|
#define NULLB_PAGE_LOCK (MAP_SZ - 1)
|
|
|
|
#define NULLB_PAGE_FREE (MAP_SZ - 2)
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
static LIST_HEAD(nullb_list);
|
|
|
|
static struct mutex lock;
|
|
|
|
static int null_major;
|
2017-08-14 15:04:55 -07:00
|
|
|
static DEFINE_IDA(nullb_indexes);
|
2017-06-20 13:22:01 -07:00
|
|
|
static struct blk_mq_tag_set tag_set;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
NULL_IRQ_NONE = 0,
|
|
|
|
NULL_IRQ_SOFTIRQ = 1,
|
|
|
|
NULL_IRQ_TIMER = 2,
|
2014-02-10 04:24:40 -07:00
|
|
|
};
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2024-07-03 18:06:38 -07:00
|
|
|
static bool g_virt_boundary;
|
2021-04-12 02:55:23 -07:00
|
|
|
module_param_named(virt_boundary, g_virt_boundary, bool, 0444);
|
|
|
|
MODULE_PARM_DESC(virt_boundary, "Require a virtual boundary for the device. Default: False");
|
|
|
|
|
2017-09-29 18:49:21 -07:00
|
|
|
static int g_no_sched;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(no_sched, g_no_sched, int, 0444);
|
2017-09-29 18:49:21 -07:00
|
|
|
MODULE_PARM_DESC(no_sched, "No io scheduler");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_submit_queues = 1;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(submit_queues, g_submit_queues, int, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(submit_queues, "Number of submission queues");
|
|
|
|
|
2021-04-17 08:29:49 -07:00
|
|
|
static int g_poll_queues = 1;
|
|
|
|
module_param_named(poll_queues, g_poll_queues, int, 0444);
|
|
|
|
MODULE_PARM_DESC(poll_queues, "Number of IOPOLL submission queues");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_home_node = NUMA_NO_NODE;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(home_node, g_home_node, int, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(home_node, "Home node for the device");
|
|
|
|
|
2018-01-11 03:31:25 -07:00
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
2020-03-12 15:01:40 -07:00
|
|
|
/*
|
|
|
|
* For more details about fault injection, please refer to
|
|
|
|
* Documentation/fault-injection/fault-injection.rst.
|
|
|
|
*/
|
2018-01-10 09:06:23 -07:00
|
|
|
static char g_timeout_str[80];
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
|
2020-03-12 15:01:40 -07:00
|
|
|
MODULE_PARM_DESC(timeout, "Fault injection. timeout=<interval>,<probability>,<space>,<times>");
|
2018-02-28 09:18:57 -07:00
|
|
|
|
|
|
|
static char g_requeue_str[80];
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
|
2020-03-12 15:01:40 -07:00
|
|
|
MODULE_PARM_DESC(requeue, "Fault injection. requeue=<interval>,<probability>,<space>,<times>");
|
2020-03-09 21:26:23 -07:00
|
|
|
|
|
|
|
static char g_init_hctx_str[80];
|
|
|
|
module_param_string(init_hctx, g_init_hctx_str, sizeof(g_init_hctx_str), 0444);
|
2020-03-12 15:01:40 -07:00
|
|
|
MODULE_PARM_DESC(init_hctx, "Fault injection to fail hctx init. init_hctx=<interval>,<probability>,<space>,<times>");
|
2018-01-11 03:31:25 -07:00
|
|
|
#endif
|
2018-01-10 09:06:23 -07:00
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
/*
|
|
|
|
* Historic queue modes.
|
|
|
|
*
|
|
|
|
* These days nothing but NULL_Q_MQ is actually supported, but we keep it the
|
|
|
|
* enum for error reporting.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
NULL_Q_BIO = 0,
|
|
|
|
NULL_Q_RQ = 1,
|
|
|
|
NULL_Q_MQ = 2,
|
|
|
|
};
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_queue_mode = NULL_Q_MQ;
|
2014-11-26 14:45:48 -07:00
|
|
|
|
|
|
|
static int null_param_store_val(const char *str, int *val, int min, int max)
|
|
|
|
{
|
|
|
|
int ret, new_val;
|
|
|
|
|
|
|
|
ret = kstrtoint(str, 10, &new_val);
|
|
|
|
if (ret)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (new_val < min || new_val > max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*val = new_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
|
|
|
|
{
|
2017-08-14 15:04:52 -07:00
|
|
|
return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
|
2014-11-26 14:45:48 -07:00
|
|
|
}
|
|
|
|
|
2015-05-26 18:39:38 -07:00
|
|
|
static const struct kernel_param_ops null_queue_mode_param_ops = {
|
2014-11-26 14:45:48 -07:00
|
|
|
.set = null_set_queue_mode,
|
|
|
|
.get = param_get_int,
|
|
|
|
};
|
|
|
|
|
2018-05-24 12:38:59 -07:00
|
|
|
device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
|
2014-06-11 14:13:50 -07:00
|
|
|
MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_gb = 250;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(gb, g_gb, int, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(gb, "Size in GB");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_bs = 512;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(bs, g_bs, int, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(bs, "Block size (in bytes)");
|
|
|
|
|
2020-11-19 18:55:18 -07:00
|
|
|
static int g_max_sectors;
|
|
|
|
module_param_named(max_sectors, g_max_sectors, int, 0444);
|
|
|
|
MODULE_PARM_DESC(max_sectors, "Maximum size of a command (in 512B sectors)");
|
|
|
|
|
2019-09-11 07:46:36 -07:00
|
|
|
static unsigned int nr_devices = 1;
|
2019-09-16 07:07:58 -07:00
|
|
|
module_param(nr_devices, uint, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(nr_devices, "Number of devices to register");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static bool g_blocking;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(blocking, g_blocking, bool, 0444);
|
2017-03-30 12:44:26 -07:00
|
|
|
MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
|
|
|
|
|
2024-01-29 21:21:34 -07:00
|
|
|
static bool g_shared_tags;
|
|
|
|
module_param_named(shared_tags, g_shared_tags, bool, 0444);
|
2017-06-20 13:22:01 -07:00
|
|
|
MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
|
|
|
|
|
2020-08-19 08:20:29 -07:00
|
|
|
static bool g_shared_tag_bitmap;
|
|
|
|
module_param_named(shared_tag_bitmap, g_shared_tag_bitmap, bool, 0444);
|
|
|
|
MODULE_PARM_DESC(shared_tag_bitmap, "Use shared tag bitmap for all submission queues for blk-mq");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_irqmode = NULL_IRQ_SOFTIRQ;
|
2014-11-26 14:45:48 -07:00
|
|
|
|
|
|
|
static int null_set_irqmode(const char *str, const struct kernel_param *kp)
|
|
|
|
{
|
2017-08-14 15:04:52 -07:00
|
|
|
return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
|
2014-11-26 14:45:48 -07:00
|
|
|
NULL_IRQ_TIMER);
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:39:38 -07:00
|
|
|
static const struct kernel_param_ops null_irqmode_param_ops = {
|
2014-11-26 14:45:48 -07:00
|
|
|
.set = null_set_irqmode,
|
|
|
|
.get = param_get_int,
|
|
|
|
};
|
|
|
|
|
2018-05-24 12:38:59 -07:00
|
|
|
device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static unsigned long g_completion_nsec = 10000;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int g_hw_queue_depth = 64;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static bool g_use_per_node_hctx;
|
2018-05-24 12:38:59 -07:00
|
|
|
module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
|
2013-12-20 16:11:00 -07:00
|
|
|
MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2022-07-08 10:49:49 -07:00
|
|
|
static bool g_memory_backed;
|
|
|
|
module_param_named(memory_backed, g_memory_backed, bool, 0444);
|
|
|
|
MODULE_PARM_DESC(memory_backed, "Create a memory-backed block device. Default: false");
|
|
|
|
|
|
|
|
static bool g_discard;
|
|
|
|
module_param_named(discard, g_discard, bool, 0444);
|
|
|
|
MODULE_PARM_DESC(discard, "Support discard operations (requires memory-backed null_blk device). Default: false");
|
|
|
|
|
|
|
|
static unsigned long g_cache_size;
|
|
|
|
module_param_named(cache_size, g_cache_size, ulong, 0444);
|
|
|
|
MODULE_PARM_DESC(mbps, "Cache size in MiB for memory-backed device. Default: 0 (none)");
|
|
|
|
|
2024-04-07 18:41:17 -07:00
|
|
|
static bool g_fua = true;
|
|
|
|
module_param_named(fua, g_fua, bool, 0444);
|
2024-07-02 00:32:34 -07:00
|
|
|
MODULE_PARM_DESC(fua, "Enable/disable FUA support when cache_size is used. Default: true");
|
2024-04-07 18:41:17 -07:00
|
|
|
|
2022-07-08 10:49:49 -07:00
|
|
|
static unsigned int g_mbps;
|
|
|
|
module_param_named(mbps, g_mbps, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(mbps, "Limit maximum bandwidth (in MiB/s). Default: 0 (no limit)");
|
|
|
|
|
2018-07-06 10:38:39 -07:00
|
|
|
static bool g_zoned;
|
|
|
|
module_param_named(zoned, g_zoned, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
|
|
|
|
|
|
|
|
static unsigned long g_zone_size = 256;
|
|
|
|
module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
|
|
|
|
|
2020-06-29 12:06:38 -07:00
|
|
|
static unsigned long g_zone_capacity;
|
|
|
|
module_param_named(zone_capacity, g_zone_capacity, ulong, 0444);
|
|
|
|
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");
|
|
|
|
|
2018-10-30 00:14:05 -07:00
|
|
|
static unsigned int g_zone_nr_conv;
|
|
|
|
module_param_named(zone_nr_conv, g_zone_nr_conv, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones when block device is zoned. Default: 0");
|
|
|
|
|
2020-08-28 03:54:00 -07:00
|
|
|
static unsigned int g_zone_max_open;
|
|
|
|
module_param_named(zone_max_open, g_zone_max_open, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones when block device is zoned. Default: 0 (no limit)");
|
|
|
|
|
|
|
|
static unsigned int g_zone_max_active;
|
|
|
|
module_param_named(zone_max_active, g_zone_max_active, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(zone_max_active, "Maximum number of active zones when block device is zoned. Default: 0 (no limit)");
|
|
|
|
|
2024-04-07 18:41:16 -07:00
|
|
|
static int g_zone_append_max_sectors = INT_MAX;
|
|
|
|
module_param_named(zone_append_max_sectors, g_zone_append_max_sectors, int, 0444);
|
|
|
|
MODULE_PARM_DESC(zone_append_max_sectors,
|
|
|
|
"Maximum size of a zone append command (in 512B sectors). Specify 0 for zone append emulation");
|
|
|
|
|
2024-07-03 22:28:12 -07:00
|
|
|
static bool g_zone_full;
|
|
|
|
module_param_named(zone_full, g_zone_full, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(zone_full, "Initialize the sequential write required zones of a zoned device to be full. Default: false");
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
static struct nullb_device *null_alloc_dev(void);
|
|
|
|
static void null_free_dev(struct nullb_device *dev);
|
2017-08-14 15:04:54 -07:00
|
|
|
static void null_del_dev(struct nullb *nullb);
|
|
|
|
static int null_add_dev(struct nullb_device *dev);
|
2022-04-19 17:57:18 -07:00
|
|
|
static struct nullb *null_find_dev_by_name(const char *name);
|
2017-08-14 15:04:59 -07:00
|
|
|
static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
|
2017-08-14 15:04:53 -07:00
|
|
|
|
|
|
|
static inline struct nullb_device *to_nullb_device(struct config_item *item)
|
|
|
|
{
|
2023-03-27 07:37:33 -07:00
|
|
|
return item ? container_of(to_config_group(item), struct nullb_device, group) : NULL;
|
2017-08-14 15:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
|
|
|
|
{
|
|
|
|
return snprintf(page, PAGE_SIZE, "%u\n", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
|
|
|
|
char *page)
|
|
|
|
{
|
|
|
|
return snprintf(page, PAGE_SIZE, "%lu\n", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
|
|
|
|
{
|
|
|
|
return snprintf(page, PAGE_SIZE, "%u\n", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nullb_device_uint_attr_store(unsigned int *val,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = kstrtouint(page, 0, &tmp);
|
2019-09-30 16:00:47 -07:00
|
|
|
if (result < 0)
|
2017-08-14 15:04:53 -07:00
|
|
|
return result;
|
|
|
|
|
|
|
|
*val = tmp;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
result = kstrtoul(page, 0, &tmp);
|
2019-09-30 16:00:47 -07:00
|
|
|
if (result < 0)
|
2017-08-14 15:04:53 -07:00
|
|
|
return result;
|
|
|
|
|
|
|
|
*val = tmp;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
|
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
bool tmp;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = kstrtobool(page, &tmp);
|
2019-09-30 16:00:47 -07:00
|
|
|
if (result < 0)
|
2017-08-14 15:04:53 -07:00
|
|
|
return result;
|
|
|
|
|
|
|
|
*val = tmp;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
|
2020-02-03 18:34:49 -07:00
|
|
|
#define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY) \
|
|
|
|
static ssize_t \
|
|
|
|
nullb_device_##NAME##_show(struct config_item *item, char *page) \
|
|
|
|
{ \
|
|
|
|
return nullb_device_##TYPE##_attr_show( \
|
|
|
|
to_nullb_device(item)->NAME, page); \
|
|
|
|
} \
|
|
|
|
static ssize_t \
|
|
|
|
nullb_device_##NAME##_store(struct config_item *item, const char *page, \
|
|
|
|
size_t count) \
|
|
|
|
{ \
|
|
|
|
int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
|
|
|
|
struct nullb_device *dev = to_nullb_device(item); \
|
2020-03-09 21:26:19 -07:00
|
|
|
TYPE new_value = 0; \
|
2020-02-03 18:34:49 -07:00
|
|
|
int ret; \
|
|
|
|
\
|
|
|
|
ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
|
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
if (apply_fn) \
|
|
|
|
ret = apply_fn(dev, new_value); \
|
|
|
|
else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) \
|
|
|
|
ret = -EBUSY; \
|
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
dev->NAME = new_value; \
|
|
|
|
return count; \
|
|
|
|
} \
|
2017-08-14 15:04:53 -07:00
|
|
|
CONFIGFS_ATTR(nullb_device_, NAME);
|
|
|
|
|
2021-10-29 03:39:26 -07:00
|
|
|
static int nullb_update_nr_hw_queues(struct nullb_device *dev,
|
|
|
|
unsigned int submit_queues,
|
|
|
|
unsigned int poll_queues)
|
|
|
|
|
2019-09-30 16:00:47 -07:00
|
|
|
{
|
|
|
|
struct blk_mq_tag_set *set;
|
2021-10-29 03:39:26 -07:00
|
|
|
int ret, nr_hw_queues;
|
2019-09-30 16:00:47 -07:00
|
|
|
|
2021-10-29 03:39:26 -07:00
|
|
|
if (!dev->nullb)
|
2019-09-30 16:00:47 -07:00
|
|
|
return 0;
|
|
|
|
|
2021-10-29 03:39:26 -07:00
|
|
|
/*
|
2021-12-02 19:39:35 -07:00
|
|
|
* Make sure at least one submit queue exists.
|
2021-10-29 03:39:26 -07:00
|
|
|
*/
|
2021-12-02 19:39:35 -07:00
|
|
|
if (!submit_queues)
|
2021-10-29 03:39:26 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2020-03-09 21:26:20 -07:00
|
|
|
/*
|
|
|
|
* Make sure that null_init_hctx() does not access nullb->queues[] past
|
|
|
|
* the end of that array.
|
|
|
|
*/
|
2021-10-29 03:39:26 -07:00
|
|
|
if (submit_queues > nr_cpu_ids || poll_queues > g_poll_queues)
|
2020-03-09 21:26:20 -07:00
|
|
|
return -EINVAL;
|
2021-10-29 03:39:26 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep previous and new queue numbers in nullb_device for reference in
|
|
|
|
* the call back function null_map_queues().
|
|
|
|
*/
|
|
|
|
dev->prev_submit_queues = dev->submit_queues;
|
|
|
|
dev->prev_poll_queues = dev->poll_queues;
|
|
|
|
dev->submit_queues = submit_queues;
|
|
|
|
dev->poll_queues = poll_queues;
|
|
|
|
|
|
|
|
set = dev->nullb->tag_set;
|
|
|
|
nr_hw_queues = submit_queues + poll_queues;
|
|
|
|
blk_mq_update_nr_hw_queues(set, nr_hw_queues);
|
|
|
|
ret = set->nr_hw_queues == nr_hw_queues ? 0 : -ENOMEM;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
/* on error, revert the queue numbers */
|
|
|
|
dev->submit_queues = dev->prev_submit_queues;
|
|
|
|
dev->poll_queues = dev->prev_poll_queues;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nullb_apply_submit_queues(struct nullb_device *dev,
|
|
|
|
unsigned int submit_queues)
|
|
|
|
{
|
2024-05-23 08:39:34 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&lock);
|
|
|
|
ret = nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
|
|
|
|
return ret;
|
2021-10-29 03:39:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nullb_apply_poll_queues(struct nullb_device *dev,
|
|
|
|
unsigned int poll_queues)
|
|
|
|
{
|
2024-05-23 08:39:34 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&lock);
|
|
|
|
ret = nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
|
|
|
|
return ret;
|
2019-09-30 16:00:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NULLB_DEVICE_ATTR(size, ulong, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(completion_nsec, ulong, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(submit_queues, uint, nullb_apply_submit_queues);
|
2021-10-29 03:39:26 -07:00
|
|
|
NULLB_DEVICE_ATTR(poll_queues, uint, nullb_apply_poll_queues);
|
2019-09-30 16:00:47 -07:00
|
|
|
NULLB_DEVICE_ATTR(home_node, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(queue_mode, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(blocksize, uint, NULL);
|
2020-11-19 18:55:18 -07:00
|
|
|
NULLB_DEVICE_ATTR(max_sectors, uint, NULL);
|
2019-09-30 16:00:47 -07:00
|
|
|
NULLB_DEVICE_ATTR(irqmode, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(hw_queue_depth, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(index, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(blocking, bool, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(use_per_node_hctx, bool, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(memory_backed, bool, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(discard, bool, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(mbps, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(cache_size, ulong, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(zoned, bool, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(zone_size, ulong, NULL);
|
2020-06-29 12:06:38 -07:00
|
|
|
NULLB_DEVICE_ATTR(zone_capacity, ulong, NULL);
|
2019-09-30 16:00:47 -07:00
|
|
|
NULLB_DEVICE_ATTR(zone_nr_conv, uint, NULL);
|
2020-08-28 03:54:00 -07:00
|
|
|
NULLB_DEVICE_ATTR(zone_max_open, uint, NULL);
|
|
|
|
NULLB_DEVICE_ATTR(zone_max_active, uint, NULL);
|
2024-04-07 18:41:16 -07:00
|
|
|
NULLB_DEVICE_ATTR(zone_append_max_sectors, uint, NULL);
|
2024-07-03 22:28:12 -07:00
|
|
|
NULLB_DEVICE_ATTR(zone_full, bool, NULL);
|
2021-04-12 02:55:23 -07:00
|
|
|
NULLB_DEVICE_ATTR(virt_boundary, bool, NULL);
|
2022-07-08 10:49:49 -07:00
|
|
|
NULLB_DEVICE_ATTR(no_sched, bool, NULL);
|
2024-01-29 21:21:34 -07:00
|
|
|
NULLB_DEVICE_ATTR(shared_tags, bool, NULL);
|
2022-07-08 10:49:49 -07:00
|
|
|
NULLB_DEVICE_ATTR(shared_tag_bitmap, bool, NULL);
|
2024-04-07 18:41:17 -07:00
|
|
|
NULLB_DEVICE_ATTR(fua, bool, NULL);
|
2017-08-14 15:04:53 -07:00
|
|
|
|
2017-08-14 15:04:54 -07:00
|
|
|
static ssize_t nullb_device_power_show(struct config_item *item, char *page)
|
|
|
|
{
|
|
|
|
return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nullb_device_power_store(struct config_item *item,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev = to_nullb_device(item);
|
|
|
|
bool newp = false;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = nullb_device_bool_attr_store(&newp, page, count);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2024-05-23 08:39:34 -07:00
|
|
|
ret = count;
|
|
|
|
mutex_lock(&lock);
|
2017-08-14 15:04:54 -07:00
|
|
|
if (!dev->power && newp) {
|
|
|
|
if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
|
2024-05-23 08:39:34 -07:00
|
|
|
goto out;
|
|
|
|
|
2022-02-15 04:59:51 -07:00
|
|
|
ret = null_add_dev(dev);
|
|
|
|
if (ret) {
|
2017-08-14 15:04:54 -07:00
|
|
|
clear_bit(NULLB_DEV_FL_UP, &dev->flags);
|
2024-05-23 08:39:34 -07:00
|
|
|
goto out;
|
2017-08-14 15:04:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
|
|
|
|
dev->power = newp;
|
2024-05-26 21:34:45 -07:00
|
|
|
ret = count;
|
2017-08-28 14:06:31 -07:00
|
|
|
} else if (dev->power && !newp) {
|
2019-06-15 00:43:48 -07:00
|
|
|
if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
|
|
|
|
dev->power = newp;
|
|
|
|
null_del_dev(dev->nullb);
|
|
|
|
}
|
2018-07-05 12:07:13 -07:00
|
|
|
clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
|
2017-08-14 15:04:54 -07:00
|
|
|
}
|
|
|
|
|
2024-05-23 08:39:34 -07:00
|
|
|
out:
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
return ret;
|
2017-08-14 15:04:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CONFIGFS_ATTR(nullb_device_, power);
|
|
|
|
|
2017-08-14 15:05:00 -07:00
|
|
|
static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
|
|
|
|
{
|
|
|
|
struct nullb_device *t_dev = to_nullb_device(item);
|
|
|
|
|
|
|
|
return badblocks_show(&t_dev->badblocks, page, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t nullb_device_badblocks_store(struct config_item *item,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
struct nullb_device *t_dev = to_nullb_device(item);
|
|
|
|
char *orig, *buf, *tmp;
|
|
|
|
u64 start, end;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
orig = kstrndup(page, count, GFP_KERNEL);
|
|
|
|
if (!orig)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
buf = strstrip(orig);
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (buf[0] != '+' && buf[0] != '-')
|
|
|
|
goto out;
|
|
|
|
tmp = strchr(&buf[1], '-');
|
|
|
|
if (!tmp)
|
|
|
|
goto out;
|
|
|
|
*tmp = '\0';
|
|
|
|
ret = kstrtoull(buf + 1, 0, &start);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
ret = kstrtoull(tmp + 1, 0, &end);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (start > end)
|
|
|
|
goto out;
|
|
|
|
/* enable badblocks */
|
|
|
|
cmpxchg(&t_dev->badblocks.shift, -1, 0);
|
|
|
|
if (buf[0] == '+')
|
|
|
|
ret = badblocks_set(&t_dev->badblocks, start,
|
|
|
|
end - start + 1, 1);
|
|
|
|
else
|
|
|
|
ret = badblocks_clear(&t_dev->badblocks, start,
|
|
|
|
end - start + 1);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = count;
|
|
|
|
out:
|
|
|
|
kfree(orig);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CONFIGFS_ATTR(nullb_device_, badblocks);
|
|
|
|
|
2022-11-30 23:10:36 -07:00
|
|
|
static ssize_t nullb_device_zone_readonly_store(struct config_item *item,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev = to_nullb_device(item);
|
|
|
|
|
|
|
|
return zone_cond_store(dev, page, count, BLK_ZONE_COND_READONLY);
|
|
|
|
}
|
|
|
|
CONFIGFS_ATTR_WO(nullb_device_, zone_readonly);
|
|
|
|
|
|
|
|
static ssize_t nullb_device_zone_offline_store(struct config_item *item,
|
|
|
|
const char *page, size_t count)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev = to_nullb_device(item);
|
|
|
|
|
|
|
|
return zone_cond_store(dev, page, count, BLK_ZONE_COND_OFFLINE);
|
|
|
|
}
|
|
|
|
CONFIGFS_ATTR_WO(nullb_device_, zone_offline);
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
static struct configfs_attribute *nullb_device_attrs[] = {
|
|
|
|
&nullb_device_attr_size,
|
|
|
|
&nullb_device_attr_completion_nsec,
|
|
|
|
&nullb_device_attr_submit_queues,
|
2021-04-17 08:29:49 -07:00
|
|
|
&nullb_device_attr_poll_queues,
|
2017-08-14 15:04:53 -07:00
|
|
|
&nullb_device_attr_home_node,
|
|
|
|
&nullb_device_attr_queue_mode,
|
|
|
|
&nullb_device_attr_blocksize,
|
2020-11-19 18:55:18 -07:00
|
|
|
&nullb_device_attr_max_sectors,
|
2017-08-14 15:04:53 -07:00
|
|
|
&nullb_device_attr_irqmode,
|
|
|
|
&nullb_device_attr_hw_queue_depth,
|
2017-08-14 15:04:54 -07:00
|
|
|
&nullb_device_attr_index,
|
2017-08-14 15:04:53 -07:00
|
|
|
&nullb_device_attr_blocking,
|
|
|
|
&nullb_device_attr_use_per_node_hctx,
|
2017-08-14 15:04:54 -07:00
|
|
|
&nullb_device_attr_power,
|
2017-08-14 15:04:56 -07:00
|
|
|
&nullb_device_attr_memory_backed,
|
2017-08-14 15:04:57 -07:00
|
|
|
&nullb_device_attr_discard,
|
2017-08-14 15:04:58 -07:00
|
|
|
&nullb_device_attr_mbps,
|
2017-08-14 15:04:59 -07:00
|
|
|
&nullb_device_attr_cache_size,
|
2017-08-14 15:05:00 -07:00
|
|
|
&nullb_device_attr_badblocks,
|
2018-07-06 10:38:39 -07:00
|
|
|
&nullb_device_attr_zoned,
|
|
|
|
&nullb_device_attr_zone_size,
|
2020-06-29 12:06:38 -07:00
|
|
|
&nullb_device_attr_zone_capacity,
|
2018-10-30 00:14:05 -07:00
|
|
|
&nullb_device_attr_zone_nr_conv,
|
2020-08-28 03:54:00 -07:00
|
|
|
&nullb_device_attr_zone_max_open,
|
|
|
|
&nullb_device_attr_zone_max_active,
|
2024-04-07 18:41:16 -07:00
|
|
|
&nullb_device_attr_zone_append_max_sectors,
|
2022-11-30 23:10:36 -07:00
|
|
|
&nullb_device_attr_zone_readonly,
|
|
|
|
&nullb_device_attr_zone_offline,
|
2024-07-03 22:28:12 -07:00
|
|
|
&nullb_device_attr_zone_full,
|
2021-04-12 02:55:23 -07:00
|
|
|
&nullb_device_attr_virt_boundary,
|
2022-07-08 10:49:49 -07:00
|
|
|
&nullb_device_attr_no_sched,
|
2024-01-29 21:21:34 -07:00
|
|
|
&nullb_device_attr_shared_tags,
|
2022-07-08 10:49:49 -07:00
|
|
|
&nullb_device_attr_shared_tag_bitmap,
|
2024-04-07 18:41:17 -07:00
|
|
|
&nullb_device_attr_fua,
|
2017-08-14 15:04:53 -07:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void nullb_device_release(struct config_item *item)
|
|
|
|
{
|
2017-08-14 15:04:56 -07:00
|
|
|
struct nullb_device *dev = to_nullb_device(item);
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
null_free_device_storage(dev, false);
|
2017-08-14 15:04:56 -07:00
|
|
|
null_free_dev(dev);
|
2017-08-14 15:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct configfs_item_operations nullb_device_ops = {
|
|
|
|
.release = nullb_device_release,
|
|
|
|
};
|
|
|
|
|
2017-10-16 08:18:49 -07:00
|
|
|
static const struct config_item_type nullb_device_type = {
|
2017-08-14 15:04:53 -07:00
|
|
|
.ct_item_ops = &nullb_device_ops,
|
|
|
|
.ct_attrs = nullb_device_attrs,
|
|
|
|
.ct_owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
|
|
|
|
|
|
|
static void nullb_add_fault_config(struct nullb_device *dev)
|
|
|
|
{
|
|
|
|
fault_config_init(&dev->timeout_config, "timeout_inject");
|
|
|
|
fault_config_init(&dev->requeue_config, "requeue_inject");
|
|
|
|
fault_config_init(&dev->init_hctx_fault_config, "init_hctx_fault_inject");
|
|
|
|
|
|
|
|
configfs_add_default_group(&dev->timeout_config.group, &dev->group);
|
|
|
|
configfs_add_default_group(&dev->requeue_config.group, &dev->group);
|
|
|
|
configfs_add_default_group(&dev->init_hctx_fault_config.group, &dev->group);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void nullb_add_fault_config(struct nullb_device *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
static struct
|
2023-03-27 07:37:33 -07:00
|
|
|
config_group *nullb_group_make_group(struct config_group *group, const char *name)
|
2017-08-14 15:04:53 -07:00
|
|
|
{
|
|
|
|
struct nullb_device *dev;
|
|
|
|
|
2022-04-19 17:57:18 -07:00
|
|
|
if (null_find_dev_by_name(name))
|
|
|
|
return ERR_PTR(-EEXIST);
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
dev = null_alloc_dev();
|
|
|
|
if (!dev)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
config_group_init_type_name(&dev->group, name, &nullb_device_type);
|
|
|
|
nullb_add_fault_config(dev);
|
2017-08-14 15:04:53 -07:00
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
return &dev->group;
|
2017-08-14 15:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nullb_group_drop_item(struct config_group *group, struct config_item *item)
|
|
|
|
{
|
2017-08-14 15:04:54 -07:00
|
|
|
struct nullb_device *dev = to_nullb_device(item);
|
|
|
|
|
|
|
|
if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
|
|
|
|
mutex_lock(&lock);
|
|
|
|
dev->power = false;
|
|
|
|
null_del_dev(dev->nullb);
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
config_item_put(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t memb_group_features_show(struct config_item *item, char *page)
|
|
|
|
{
|
2020-06-29 12:06:38 -07:00
|
|
|
return snprintf(page, PAGE_SIZE,
|
2024-04-07 18:41:17 -07:00
|
|
|
"badblocks,blocking,blocksize,cache_size,fua,"
|
2022-07-08 10:49:49 -07:00
|
|
|
"completion_nsec,discard,home_node,hw_queue_depth,"
|
|
|
|
"irqmode,max_sectors,mbps,memory_backed,no_sched,"
|
2024-01-29 21:21:34 -07:00
|
|
|
"poll_queues,power,queue_mode,shared_tag_bitmap,"
|
|
|
|
"shared_tags,size,submit_queues,use_per_node_hctx,"
|
|
|
|
"virt_boundary,zoned,zone_capacity,zone_max_active,"
|
|
|
|
"zone_max_open,zone_nr_conv,zone_offline,zone_readonly,"
|
2024-07-03 22:28:12 -07:00
|
|
|
"zone_size,zone_append_max_sectors,zone_full\n");
|
2017-08-14 15:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CONFIGFS_ATTR_RO(memb_group_, features);
|
|
|
|
|
|
|
|
static struct configfs_attribute *nullb_group_attrs[] = {
|
|
|
|
&memb_group_attr_features,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct configfs_group_operations nullb_group_ops = {
|
2023-03-27 07:37:33 -07:00
|
|
|
.make_group = nullb_group_make_group,
|
2017-08-14 15:04:53 -07:00
|
|
|
.drop_item = nullb_group_drop_item,
|
|
|
|
};
|
|
|
|
|
2017-10-16 08:18:49 -07:00
|
|
|
static const struct config_item_type nullb_group_type = {
|
2017-08-14 15:04:53 -07:00
|
|
|
.ct_group_ops = &nullb_group_ops,
|
|
|
|
.ct_attrs = nullb_group_attrs,
|
|
|
|
.ct_owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct configfs_subsystem nullb_subsys = {
|
|
|
|
.su_group = {
|
|
|
|
.cg_item = {
|
|
|
|
.ci_namebuf = "nullb",
|
|
|
|
.ci_type = &nullb_group_type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
static inline int null_cache_active(struct nullb *nullb)
|
|
|
|
{
|
|
|
|
return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static struct nullb_device *null_alloc_dev(void)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev;
|
|
|
|
|
|
|
|
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
|
|
|
|
if (!dev)
|
|
|
|
return NULL;
|
2023-03-27 07:37:33 -07:00
|
|
|
|
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
|
|
|
dev->timeout_config.attr = null_timeout_attr;
|
|
|
|
dev->requeue_config.attr = null_requeue_attr;
|
|
|
|
dev->init_hctx_fault_config.attr = null_init_hctx_attr;
|
|
|
|
#endif
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
|
2017-08-14 15:04:59 -07:00
|
|
|
INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
|
2017-08-14 15:05:00 -07:00
|
|
|
if (badblocks_init(&dev->badblocks, 0)) {
|
|
|
|
kfree(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
dev->size = g_gb * 1024;
|
|
|
|
dev->completion_nsec = g_completion_nsec;
|
|
|
|
dev->submit_queues = g_submit_queues;
|
2021-10-29 03:39:26 -07:00
|
|
|
dev->prev_submit_queues = g_submit_queues;
|
2021-04-17 08:29:49 -07:00
|
|
|
dev->poll_queues = g_poll_queues;
|
2021-10-29 03:39:26 -07:00
|
|
|
dev->prev_poll_queues = g_poll_queues;
|
2017-08-14 15:04:52 -07:00
|
|
|
dev->home_node = g_home_node;
|
|
|
|
dev->queue_mode = g_queue_mode;
|
|
|
|
dev->blocksize = g_bs;
|
2020-11-19 18:55:18 -07:00
|
|
|
dev->max_sectors = g_max_sectors;
|
2017-08-14 15:04:52 -07:00
|
|
|
dev->irqmode = g_irqmode;
|
|
|
|
dev->hw_queue_depth = g_hw_queue_depth;
|
|
|
|
dev->blocking = g_blocking;
|
2022-07-08 10:49:49 -07:00
|
|
|
dev->memory_backed = g_memory_backed;
|
|
|
|
dev->discard = g_discard;
|
|
|
|
dev->cache_size = g_cache_size;
|
|
|
|
dev->mbps = g_mbps;
|
2017-08-14 15:04:52 -07:00
|
|
|
dev->use_per_node_hctx = g_use_per_node_hctx;
|
2018-07-06 10:38:39 -07:00
|
|
|
dev->zoned = g_zoned;
|
|
|
|
dev->zone_size = g_zone_size;
|
2020-06-29 12:06:38 -07:00
|
|
|
dev->zone_capacity = g_zone_capacity;
|
2018-10-30 00:14:05 -07:00
|
|
|
dev->zone_nr_conv = g_zone_nr_conv;
|
2020-08-28 03:54:00 -07:00
|
|
|
dev->zone_max_open = g_zone_max_open;
|
|
|
|
dev->zone_max_active = g_zone_max_active;
|
2024-04-07 18:41:16 -07:00
|
|
|
dev->zone_append_max_sectors = g_zone_append_max_sectors;
|
2024-07-03 22:28:12 -07:00
|
|
|
dev->zone_full = g_zone_full;
|
2021-04-12 02:55:23 -07:00
|
|
|
dev->virt_boundary = g_virt_boundary;
|
2022-07-08 10:49:49 -07:00
|
|
|
dev->no_sched = g_no_sched;
|
2024-01-29 21:21:34 -07:00
|
|
|
dev->shared_tags = g_shared_tags;
|
2022-07-08 10:49:49 -07:00
|
|
|
dev->shared_tag_bitmap = g_shared_tag_bitmap;
|
2024-04-07 18:41:17 -07:00
|
|
|
dev->fua = g_fua;
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_free_dev(struct nullb_device *dev)
|
|
|
|
{
|
2017-11-08 09:29:44 -07:00
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
2020-04-22 20:02:38 -07:00
|
|
|
null_free_zoned_dev(dev);
|
2017-11-08 09:29:44 -07:00
|
|
|
badblocks_exit(&dev->badblocks);
|
2017-08-14 15:04:52 -07:00
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
null_blk: guarantee device restart in all irq modes
In single-queue (block layer) mode,the function null_rq_prep_fn stops
the device if alloc_cmd fails. Then, once stopped, the device must be
restarted on the next command completion, so that the request(s) for
which alloc_cmd failed can be requeued. Otherwise the device hangs.
Unfortunately, device restart is currently performed only for delayed
completions, i.e., in irqmode==2. This fact causes hangs, for the
above reasons, with the other irqmodes in combination with single-queue
block layer.
This commits addresses this issue by making sure that, if stopped, the
device is properly restarted for all irqmodes on completions.
Signed-off-by: Paolo Valente <paolo.valente@unimore.it>
Signed-off-by: Arianna AVanzini <avanzini@google.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
2015-12-01 03:48:18 -07:00
|
|
|
static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
|
|
|
|
{
|
2024-02-20 02:32:44 -07:00
|
|
|
struct nullb_cmd *cmd = container_of(timer, struct nullb_cmd, timer);
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_end_request(blk_mq_rq_from_pdu(cmd), cmd->error);
|
2013-10-25 03:52:25 -07:00
|
|
|
return HRTIMER_NORESTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_cmd_end_timer(struct nullb_cmd *cmd)
|
|
|
|
{
|
2017-08-14 15:04:52 -07:00
|
|
|
ktime_t kt = cmd->nq->dev->completion_nsec;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
null_blk: set a separate timer for each command
For the Timer IRQ mode (i.e., when command completions are delayed),
there is one timer for each CPU. Each of these timers
. has a completion queue associated with it, containing all the
command completions to be executed when the timer fires;
. is set, and a new completion-to-execute is inserted into its
completion queue, every time the dispatch code for a new command
happens to be executed on the CPU related to the timer.
This implies that, if the dispatch of a new command happens to be
executed on a CPU whose timer has already been set, but has not yet
fired, then the timer is set again, to the completion time of the
newly arrived command. When the timer eventually fires, all its queued
completions are executed.
This way of handling delayed command completions entails the following
problem: if more than one command completion is inserted into the
queue of a timer before the timer fires, then the expiration time for
the timer is moved forward every time each of these completions is
enqueued. As a consequence, only the last completion enqueued enjoys a
correct execution time, while all previous completions are unjustly
delayed until the last completion is executed (and at that time they
are executed all together).
Specifically, if all the above completions are enqueued almost at the
same time, then the problem is negligible. On the opposite end, if
every completion is enqueued a while after the previous completion was
enqueued (in the extreme case, it is enqueued only right before the
timer would have expired), then every enqueued completion, except for
the last one, experiences an inflated delay, proportional to the number
of completions enqueued after it. In the end, commands, and thus I/O
requests, may be completed at an arbitrarily lower rate than the
desired one.
This commit addresses this issue by replacing per-CPU timers with
per-command timers, i.e., by associating an individual timer with each
command.
Signed-off-by: Paolo Valente <paolo.valente@unimore.it>
Signed-off-by: Arianna Avanzini <avanzini@google.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
2015-12-01 03:48:17 -07:00
|
|
|
hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2018-11-10 01:30:45 -07:00
|
|
|
static void null_complete_rq(struct request *rq)
|
2013-10-25 03:52:25 -07:00
|
|
|
{
|
2024-02-20 02:32:44 -07:00
|
|
|
struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
|
|
|
|
|
|
|
|
blk_mq_end_request(rq, cmd->error);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:28:51 -07:00
|
|
|
static struct nullb_page *null_alloc_page(void)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
struct nullb_page *t_page;
|
|
|
|
|
2022-02-22 08:28:51 -07:00
|
|
|
t_page = kmalloc(sizeof(struct nullb_page), GFP_NOIO);
|
2017-08-14 15:04:56 -07:00
|
|
|
if (!t_page)
|
2022-02-22 08:28:52 -07:00
|
|
|
return NULL;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2022-02-22 08:28:51 -07:00
|
|
|
t_page->page = alloc_pages(GFP_NOIO, 0);
|
2022-02-22 08:28:52 -07:00
|
|
|
if (!t_page->page) {
|
|
|
|
kfree(t_page);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
|
2017-08-14 15:04:56 -07:00
|
|
|
return t_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_free_page(struct nullb_page *t_page)
|
|
|
|
{
|
2018-03-05 21:07:13 -07:00
|
|
|
__set_bit(NULLB_PAGE_FREE, t_page->bitmap);
|
|
|
|
if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
|
2017-08-14 15:04:59 -07:00
|
|
|
return;
|
2017-08-14 15:04:56 -07:00
|
|
|
__free_page(t_page->page);
|
|
|
|
kfree(t_page);
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
static bool null_page_empty(struct nullb_page *page)
|
|
|
|
{
|
|
|
|
int size = MAP_SZ - 2;
|
|
|
|
|
|
|
|
return find_first_bit(page->bitmap, size) == size;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
static void null_free_sector(struct nullb *nullb, sector_t sector,
|
|
|
|
bool is_cache)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
unsigned int sector_bit;
|
|
|
|
u64 idx;
|
|
|
|
struct nullb_page *t_page, *ret;
|
|
|
|
struct radix_tree_root *root;
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
|
2017-08-14 15:04:56 -07:00
|
|
|
idx = sector >> PAGE_SECTORS_SHIFT;
|
|
|
|
sector_bit = (sector & SECTOR_MASK);
|
|
|
|
|
|
|
|
t_page = radix_tree_lookup(root, idx);
|
|
|
|
if (t_page) {
|
2018-03-05 21:07:13 -07:00
|
|
|
__clear_bit(sector_bit, t_page->bitmap);
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
if (null_page_empty(t_page)) {
|
2017-08-14 15:04:56 -07:00
|
|
|
ret = radix_tree_delete_item(root, idx, t_page);
|
|
|
|
WARN_ON(ret != t_page);
|
|
|
|
null_free_page(ret);
|
2017-08-14 15:04:59 -07:00
|
|
|
if (is_cache)
|
|
|
|
nullb->dev->curr_cache -= PAGE_SIZE;
|
2017-08-14 15:04:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
|
2017-08-14 15:04:59 -07:00
|
|
|
struct nullb_page *t_page, bool is_cache)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
struct radix_tree_root *root;
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
if (radix_tree_insert(root, idx, t_page)) {
|
|
|
|
null_free_page(t_page);
|
|
|
|
t_page = radix_tree_lookup(root, idx);
|
|
|
|
WARN_ON(!t_page || t_page->page->index != idx);
|
2017-08-14 15:04:59 -07:00
|
|
|
} else if (is_cache)
|
|
|
|
nullb->dev->curr_cache += PAGE_SIZE;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
return t_page;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
unsigned long pos = 0;
|
|
|
|
int nr_pages;
|
|
|
|
struct nullb_page *ret, *t_pages[FREE_BATCH];
|
|
|
|
struct radix_tree_root *root;
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
root = is_cache ? &dev->cache : &dev->data;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
nr_pages = radix_tree_gang_lookup(root,
|
|
|
|
(void **)t_pages, pos, FREE_BATCH);
|
|
|
|
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
pos = t_pages[i]->page->index;
|
|
|
|
ret = radix_tree_delete_item(root, pos, t_pages[i]);
|
|
|
|
WARN_ON(ret != t_pages[i]);
|
|
|
|
null_free_page(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
} while (nr_pages == FREE_BATCH);
|
2017-08-14 15:04:59 -07:00
|
|
|
|
|
|
|
if (is_cache)
|
|
|
|
dev->curr_cache = 0;
|
2017-08-14 15:04:56 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
static struct nullb_page *__null_lookup_page(struct nullb *nullb,
|
|
|
|
sector_t sector, bool for_write, bool is_cache)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
unsigned int sector_bit;
|
|
|
|
u64 idx;
|
|
|
|
struct nullb_page *t_page;
|
2017-08-14 15:04:59 -07:00
|
|
|
struct radix_tree_root *root;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
idx = sector >> PAGE_SECTORS_SHIFT;
|
|
|
|
sector_bit = (sector & SECTOR_MASK);
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
|
|
|
|
t_page = radix_tree_lookup(root, idx);
|
2017-08-14 15:04:56 -07:00
|
|
|
WARN_ON(t_page && t_page->page->index != idx);
|
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
|
2017-08-14 15:04:56 -07:00
|
|
|
return t_page;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
static struct nullb_page *null_lookup_page(struct nullb *nullb,
|
|
|
|
sector_t sector, bool for_write, bool ignore_cache)
|
|
|
|
{
|
|
|
|
struct nullb_page *page = NULL;
|
|
|
|
|
|
|
|
if (!ignore_cache)
|
|
|
|
page = __null_lookup_page(nullb, sector, for_write, true);
|
|
|
|
if (page)
|
|
|
|
return page;
|
|
|
|
return __null_lookup_page(nullb, sector, for_write, false);
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
static struct nullb_page *null_insert_page(struct nullb *nullb,
|
2018-08-09 13:22:41 -07:00
|
|
|
sector_t sector, bool ignore_cache)
|
|
|
|
__releases(&nullb->lock)
|
|
|
|
__acquires(&nullb->lock)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
u64 idx;
|
|
|
|
struct nullb_page *t_page;
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
t_page = null_lookup_page(nullb, sector, true, ignore_cache);
|
2017-08-14 15:04:56 -07:00
|
|
|
if (t_page)
|
|
|
|
return t_page;
|
|
|
|
|
|
|
|
spin_unlock_irq(&nullb->lock);
|
|
|
|
|
2022-02-22 08:28:51 -07:00
|
|
|
t_page = null_alloc_page();
|
2017-08-14 15:04:56 -07:00
|
|
|
if (!t_page)
|
|
|
|
goto out_lock;
|
|
|
|
|
|
|
|
if (radix_tree_preload(GFP_NOIO))
|
|
|
|
goto out_freepage;
|
|
|
|
|
|
|
|
spin_lock_irq(&nullb->lock);
|
|
|
|
idx = sector >> PAGE_SECTORS_SHIFT;
|
|
|
|
t_page->page->index = idx;
|
2017-08-14 15:04:59 -07:00
|
|
|
t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
|
2017-08-14 15:04:56 -07:00
|
|
|
radix_tree_preload_end();
|
|
|
|
|
|
|
|
return t_page;
|
|
|
|
out_freepage:
|
|
|
|
null_free_page(t_page);
|
|
|
|
out_lock:
|
|
|
|
spin_lock_irq(&nullb->lock);
|
2017-08-14 15:04:59 -07:00
|
|
|
return null_lookup_page(nullb, sector, true, ignore_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int offset;
|
|
|
|
u64 idx;
|
|
|
|
struct nullb_page *t_page, *ret;
|
|
|
|
void *dst, *src;
|
|
|
|
|
|
|
|
idx = c_page->page->index;
|
|
|
|
|
|
|
|
t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
|
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
|
|
|
|
if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
|
2017-08-14 15:04:59 -07:00
|
|
|
null_free_page(c_page);
|
2018-03-05 21:07:13 -07:00
|
|
|
if (t_page && null_page_empty(t_page)) {
|
2017-08-14 15:04:59 -07:00
|
|
|
ret = radix_tree_delete_item(&nullb->dev->data,
|
|
|
|
idx, t_page);
|
|
|
|
null_free_page(t_page);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t_page)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2023-03-30 11:49:26 -07:00
|
|
|
src = kmap_local_page(c_page->page);
|
|
|
|
dst = kmap_local_page(t_page->page);
|
2017-08-14 15:04:59 -07:00
|
|
|
|
|
|
|
for (i = 0; i < PAGE_SECTORS;
|
|
|
|
i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
|
2018-03-05 21:07:13 -07:00
|
|
|
if (test_bit(i, c_page->bitmap)) {
|
2017-08-14 15:04:59 -07:00
|
|
|
offset = (i << SECTOR_SHIFT);
|
|
|
|
memcpy(dst + offset, src + offset,
|
|
|
|
nullb->dev->blocksize);
|
2018-03-05 21:07:13 -07:00
|
|
|
__set_bit(i, t_page->bitmap);
|
2017-08-14 15:04:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:49:26 -07:00
|
|
|
kunmap_local(dst);
|
|
|
|
kunmap_local(src);
|
2017-08-14 15:04:59 -07:00
|
|
|
|
|
|
|
ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
|
|
|
|
null_free_page(ret);
|
|
|
|
nullb->dev->curr_cache -= PAGE_SIZE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int null_make_cache_space(struct nullb *nullb, unsigned long n)
|
2013-10-25 03:52:25 -07:00
|
|
|
{
|
2017-08-14 15:04:59 -07:00
|
|
|
int i, err, nr_pages;
|
|
|
|
struct nullb_page *c_pages[FREE_BATCH];
|
|
|
|
unsigned long flushed = 0, one_round;
|
|
|
|
|
|
|
|
again:
|
|
|
|
if ((nullb->dev->cache_size * 1024 * 1024) >
|
|
|
|
nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
|
|
|
|
(void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
|
|
|
|
/*
|
|
|
|
* nullb_flush_cache_page could unlock before using the c_pages. To
|
|
|
|
* avoid race, we don't allow page free
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
nullb->cache_flush_pos = c_pages[i]->page->index;
|
|
|
|
/*
|
|
|
|
* We found the page which is being flushed to disk by other
|
|
|
|
* threads
|
|
|
|
*/
|
2018-03-05 21:07:13 -07:00
|
|
|
if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
|
2017-08-14 15:04:59 -07:00
|
|
|
c_pages[i] = NULL;
|
|
|
|
else
|
2018-03-05 21:07:13 -07:00
|
|
|
__set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
|
2017-08-14 15:04:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
one_round = 0;
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
if (c_pages[i] == NULL)
|
|
|
|
continue;
|
|
|
|
err = null_flush_cache_page(nullb, c_pages[i]);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
one_round++;
|
|
|
|
}
|
|
|
|
flushed += one_round << PAGE_SHIFT;
|
|
|
|
|
|
|
|
if (n > flushed) {
|
|
|
|
if (nr_pages == 0)
|
|
|
|
nullb->cache_flush_pos = 0;
|
|
|
|
if (one_round == 0) {
|
|
|
|
/* give other threads a chance */
|
|
|
|
spin_unlock_irq(&nullb->lock);
|
|
|
|
spin_lock_irq(&nullb->lock);
|
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-08-14 15:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_to_nullb(struct nullb *nullb, struct page *source,
|
2017-08-14 15:04:59 -07:00
|
|
|
unsigned int off, sector_t sector, size_t n, bool is_fua)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
size_t temp, count = 0;
|
|
|
|
unsigned int offset;
|
|
|
|
struct nullb_page *t_page;
|
|
|
|
|
|
|
|
while (count < n) {
|
|
|
|
temp = min_t(size_t, nullb->dev->blocksize, n - count);
|
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
if (null_cache_active(nullb) && !is_fua)
|
|
|
|
null_make_cache_space(nullb, PAGE_SIZE);
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
|
2017-08-14 15:04:59 -07:00
|
|
|
t_page = null_insert_page(nullb, sector,
|
|
|
|
!null_cache_active(nullb) || is_fua);
|
2017-08-14 15:04:56 -07:00
|
|
|
if (!t_page)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2023-03-30 11:49:25 -07:00
|
|
|
memcpy_page(t_page->page, offset, source, off + count, temp);
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2018-03-05 21:07:13 -07:00
|
|
|
__set_bit(sector & SECTOR_MASK, t_page->bitmap);
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2017-08-14 15:04:59 -07:00
|
|
|
if (is_fua)
|
|
|
|
null_free_sector(nullb, sector, true);
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
count += temp;
|
|
|
|
sector += temp >> SECTOR_SHIFT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_from_nullb(struct nullb *nullb, struct page *dest,
|
|
|
|
unsigned int off, sector_t sector, size_t n)
|
|
|
|
{
|
|
|
|
size_t temp, count = 0;
|
|
|
|
unsigned int offset;
|
|
|
|
struct nullb_page *t_page;
|
|
|
|
|
|
|
|
while (count < n) {
|
|
|
|
temp = min_t(size_t, nullb->dev->blocksize, n - count);
|
|
|
|
|
|
|
|
offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
|
2017-08-14 15:04:59 -07:00
|
|
|
t_page = null_lookup_page(nullb, sector, false,
|
|
|
|
!null_cache_active(nullb));
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2023-03-30 11:49:25 -07:00
|
|
|
if (t_page)
|
|
|
|
memcpy_page(dest, off + count, t_page->page, offset,
|
|
|
|
temp);
|
|
|
|
else
|
|
|
|
zero_user(dest, off + count, temp);
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
count += temp;
|
|
|
|
sector += temp >> SECTOR_SHIFT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:19:43 -07:00
|
|
|
static void nullb_fill_pattern(struct nullb *nullb, struct page *page,
|
|
|
|
unsigned int len, unsigned int off)
|
|
|
|
{
|
2023-03-30 11:49:25 -07:00
|
|
|
memset_page(page, off, 0xff, len);
|
2019-10-17 14:19:43 -07:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:55:17 -07:00
|
|
|
blk_status_t null_handle_discard(struct nullb_device *dev,
|
|
|
|
sector_t sector, sector_t nr_sectors)
|
2017-08-14 15:04:57 -07:00
|
|
|
{
|
2020-11-19 18:55:16 -07:00
|
|
|
struct nullb *nullb = dev->nullb;
|
|
|
|
size_t n = nr_sectors << SECTOR_SHIFT;
|
2017-08-14 15:04:57 -07:00
|
|
|
size_t temp;
|
|
|
|
|
|
|
|
spin_lock_irq(&nullb->lock);
|
|
|
|
while (n > 0) {
|
2020-11-19 18:55:16 -07:00
|
|
|
temp = min_t(size_t, n, dev->blocksize);
|
2017-08-14 15:04:59 -07:00
|
|
|
null_free_sector(nullb, sector, false);
|
|
|
|
if (null_cache_active(nullb))
|
|
|
|
null_free_sector(nullb, sector, true);
|
2017-08-14 15:04:57 -07:00
|
|
|
sector += temp >> SECTOR_SHIFT;
|
|
|
|
n -= temp;
|
|
|
|
}
|
|
|
|
spin_unlock_irq(&nullb->lock);
|
2020-11-19 18:55:16 -07:00
|
|
|
|
|
|
|
return BLK_STS_OK;
|
2017-08-14 15:04:57 -07:00
|
|
|
}
|
|
|
|
|
2024-04-11 01:55:00 -07:00
|
|
|
static blk_status_t null_handle_flush(struct nullb *nullb)
|
2017-08-14 15:04:59 -07:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!null_cache_active(nullb))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
spin_lock_irq(&nullb->lock);
|
|
|
|
while (true) {
|
|
|
|
err = null_make_cache_space(nullb,
|
|
|
|
nullb->dev->cache_size * 1024 * 1024);
|
|
|
|
if (err || nullb->dev->curr_cache == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN_ON(!radix_tree_empty(&nullb->dev->cache));
|
|
|
|
spin_unlock_irq(&nullb->lock);
|
2024-04-11 01:55:00 -07:00
|
|
|
return errno_to_blk_status(err);
|
2017-08-14 15:04:59 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:56 -07:00
|
|
|
static int null_transfer(struct nullb *nullb, struct page *page,
|
2017-08-14 15:04:59 -07:00
|
|
|
unsigned int len, unsigned int off, bool is_write, sector_t sector,
|
|
|
|
bool is_fua)
|
2013-10-25 03:52:25 -07:00
|
|
|
{
|
2019-10-17 14:19:43 -07:00
|
|
|
struct nullb_device *dev = nullb->dev;
|
|
|
|
unsigned int valid_len = len;
|
2017-08-14 15:04:56 -07:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (!is_write) {
|
2019-10-17 14:19:43 -07:00
|
|
|
if (dev->zoned)
|
|
|
|
valid_len = null_zone_valid_read_len(nullb,
|
|
|
|
sector, len);
|
|
|
|
|
|
|
|
if (valid_len) {
|
|
|
|
err = copy_from_nullb(nullb, page, off,
|
|
|
|
sector, valid_len);
|
|
|
|
off += valid_len;
|
|
|
|
len -= valid_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
nullb_fill_pattern(nullb, page, len, off);
|
2017-08-14 15:04:56 -07:00
|
|
|
flush_dcache_page(page);
|
|
|
|
} else {
|
|
|
|
flush_dcache_page(page);
|
2017-08-14 15:04:59 -07:00
|
|
|
err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
|
2017-08-14 15:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-05-10 13:18:16 -07:00
|
|
|
static blk_status_t null_handle_rq(struct nullb_cmd *cmd)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
2024-02-20 02:32:44 -07:00
|
|
|
struct request *rq = blk_mq_rq_from_pdu(cmd);
|
2017-08-14 15:04:56 -07:00
|
|
|
struct nullb *nullb = cmd->nq->dev->nullb;
|
2024-04-11 01:55:00 -07:00
|
|
|
int err = 0;
|
2017-08-14 15:04:56 -07:00
|
|
|
unsigned int len;
|
2020-11-19 18:55:16 -07:00
|
|
|
sector_t sector = blk_rq_pos(rq);
|
2017-08-14 15:04:56 -07:00
|
|
|
struct req_iterator iter;
|
|
|
|
struct bio_vec bvec;
|
|
|
|
|
|
|
|
spin_lock_irq(&nullb->lock);
|
|
|
|
rq_for_each_segment(bvec, rq, iter) {
|
|
|
|
len = bvec.bv_len;
|
|
|
|
err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
|
2017-08-14 15:04:59 -07:00
|
|
|
op_is_write(req_op(rq)), sector,
|
2020-08-21 01:34:42 -07:00
|
|
|
rq->cmd_flags & REQ_FUA);
|
2024-04-11 01:55:00 -07:00
|
|
|
if (err)
|
|
|
|
break;
|
2017-08-14 15:04:56 -07:00
|
|
|
sector += len >> SECTOR_SHIFT;
|
|
|
|
}
|
|
|
|
spin_unlock_irq(&nullb->lock);
|
|
|
|
|
2024-04-11 01:55:00 -07:00
|
|
|
return errno_to_blk_status(err);
|
2017-08-14 15:04:56 -07:00
|
|
|
}
|
|
|
|
|
2019-08-22 21:45:15 -07:00
|
|
|
static inline blk_status_t null_handle_throttled(struct nullb_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
struct nullb *nullb = dev->nullb;
|
|
|
|
blk_status_t sts = BLK_STS_OK;
|
2024-02-20 02:32:44 -07:00
|
|
|
struct request *rq = blk_mq_rq_from_pdu(cmd);
|
2019-08-22 21:45:15 -07:00
|
|
|
|
|
|
|
if (!hrtimer_active(&nullb->bw_timer))
|
|
|
|
hrtimer_restart(&nullb->bw_timer);
|
|
|
|
|
|
|
|
if (atomic_long_sub_return(blk_rq_bytes(rq), &nullb->cur_bytes) < 0) {
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_stop_hw_queues(nullb->q);
|
2019-08-22 21:45:15 -07:00
|
|
|
/* race with timer */
|
|
|
|
if (atomic_long_read(&nullb->cur_bytes) > 0)
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_start_stopped_hw_queues(nullb->q, true);
|
2019-08-22 21:45:15 -07:00
|
|
|
/* requeue request */
|
|
|
|
sts = BLK_STS_DEV_RESOURCE;
|
|
|
|
}
|
|
|
|
return sts;
|
|
|
|
}
|
|
|
|
|
2019-08-22 21:45:16 -07:00
|
|
|
static inline blk_status_t null_handle_badblocks(struct nullb_cmd *cmd,
|
|
|
|
sector_t sector,
|
|
|
|
sector_t nr_sectors)
|
|
|
|
{
|
|
|
|
struct badblocks *bb = &cmd->nq->dev->badblocks;
|
|
|
|
sector_t first_bad;
|
|
|
|
int bad_sectors;
|
|
|
|
|
|
|
|
if (badblocks_check(bb, sector, nr_sectors, &first_bad, &bad_sectors))
|
|
|
|
return BLK_STS_IOERR;
|
|
|
|
|
|
|
|
return BLK_STS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-22 21:45:17 -07:00
|
|
|
static inline blk_status_t null_handle_memory_backed(struct nullb_cmd *cmd,
|
2022-07-14 11:06:27 -07:00
|
|
|
enum req_op op,
|
2020-11-19 18:55:16 -07:00
|
|
|
sector_t sector,
|
|
|
|
sector_t nr_sectors)
|
2019-08-22 21:45:17 -07:00
|
|
|
{
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
|
2020-11-19 18:55:16 -07:00
|
|
|
if (op == REQ_OP_DISCARD)
|
|
|
|
return null_handle_discard(dev, sector, nr_sectors);
|
|
|
|
|
2024-04-11 01:55:00 -07:00
|
|
|
return null_handle_rq(cmd);
|
2019-08-22 21:45:17 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 21:07:37 -07:00
|
|
|
static void nullb_zero_read_cmd_buffer(struct nullb_cmd *cmd)
|
|
|
|
{
|
2024-02-20 02:32:44 -07:00
|
|
|
struct request *rq = blk_mq_rq_from_pdu(cmd);
|
2020-05-18 21:07:37 -07:00
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
struct bio *bio;
|
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
if (!dev->memory_backed && req_op(rq) == REQ_OP_READ) {
|
|
|
|
__rq_for_each_bio(bio, rq)
|
2020-05-18 21:07:37 -07:00
|
|
|
zero_fill_bio(bio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 21:45:19 -07:00
|
|
|
static inline void nullb_complete_cmd(struct nullb_cmd *cmd)
|
|
|
|
{
|
2024-02-20 02:32:44 -07:00
|
|
|
struct request *rq = blk_mq_rq_from_pdu(cmd);
|
|
|
|
|
2020-05-18 21:07:37 -07:00
|
|
|
/*
|
|
|
|
* Since root privileges are required to configure the null_blk
|
|
|
|
* driver, it is fine that this driver does not initialize the
|
|
|
|
* data buffers of read commands. Zero-initialize these buffers
|
|
|
|
* anyway if KMSAN is enabled to prevent that KMSAN complains
|
|
|
|
* about null_blk not initializing read data buffers.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_KMSAN))
|
|
|
|
nullb_zero_read_cmd_buffer(cmd);
|
|
|
|
|
2019-08-22 21:45:19 -07:00
|
|
|
/* Complete IO by inline, softirq or timer */
|
|
|
|
switch (cmd->nq->dev->irqmode) {
|
|
|
|
case NULL_IRQ_SOFTIRQ:
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_complete_request(rq);
|
2019-08-22 21:45:19 -07:00
|
|
|
break;
|
|
|
|
case NULL_IRQ_NONE:
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_end_request(rq, cmd->error);
|
2019-08-22 21:45:19 -07:00
|
|
|
break;
|
|
|
|
case NULL_IRQ_TIMER:
|
|
|
|
null_cmd_end_timer(cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:06:27 -07:00
|
|
|
blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op,
|
|
|
|
sector_t sector, unsigned int nr_sectors)
|
2020-04-22 20:02:37 -07:00
|
|
|
{
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
blk_status_t ret;
|
|
|
|
|
|
|
|
if (dev->badblocks.shift != -1) {
|
|
|
|
ret = null_handle_badblocks(cmd, sector, nr_sectors);
|
|
|
|
if (ret != BLK_STS_OK)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->memory_backed)
|
2020-11-19 18:55:16 -07:00
|
|
|
return null_handle_memory_backed(cmd, op, sector, nr_sectors);
|
2020-04-22 20:02:37 -07:00
|
|
|
|
|
|
|
return BLK_STS_OK;
|
|
|
|
}
|
|
|
|
|
2023-11-19 20:25:21 -07:00
|
|
|
static void null_handle_cmd(struct nullb_cmd *cmd, sector_t sector,
|
|
|
|
sector_t nr_sectors, enum req_op op)
|
2017-08-14 15:04:56 -07:00
|
|
|
{
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
2017-08-14 15:04:58 -07:00
|
|
|
struct nullb *nullb = dev->nullb;
|
2019-08-22 21:45:15 -07:00
|
|
|
blk_status_t sts;
|
2017-08-14 15:04:56 -07:00
|
|
|
|
2019-08-22 21:45:14 -07:00
|
|
|
if (op == REQ_OP_FLUSH) {
|
2024-04-11 01:55:00 -07:00
|
|
|
cmd->error = null_handle_flush(nullb);
|
2019-08-22 21:45:14 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:02:37 -07:00
|
|
|
if (dev->zoned)
|
2021-03-31 15:52:44 -07:00
|
|
|
sts = null_process_zoned_cmd(cmd, op, sector, nr_sectors);
|
2020-04-22 20:02:37 -07:00
|
|
|
else
|
2021-03-31 15:52:44 -07:00
|
|
|
sts = null_process_cmd(cmd, op, sector, nr_sectors);
|
|
|
|
|
|
|
|
/* Do not overwrite errors (e.g. timeout errors) */
|
|
|
|
if (cmd->error == BLK_STS_OK)
|
|
|
|
cmd->error = sts;
|
2019-08-22 21:45:18 -07:00
|
|
|
|
2017-08-14 15:05:00 -07:00
|
|
|
out:
|
2019-08-22 21:45:19 -07:00
|
|
|
nullb_complete_cmd(cmd);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:58 -07:00
|
|
|
static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
|
|
|
|
{
|
|
|
|
struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
|
|
|
|
ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
|
|
|
|
unsigned int mbps = nullb->dev->mbps;
|
|
|
|
|
|
|
|
if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
|
|
|
|
return HRTIMER_NORESTART;
|
|
|
|
|
|
|
|
atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_start_stopped_hw_queues(nullb->q, true);
|
2017-08-14 15:04:58 -07:00
|
|
|
|
|
|
|
hrtimer_forward_now(&nullb->bw_timer, timer_interval);
|
|
|
|
|
|
|
|
return HRTIMER_RESTART;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nullb_setup_bwtimer(struct nullb *nullb)
|
|
|
|
{
|
|
|
|
ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
|
|
|
|
|
|
|
|
hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
|
|
nullb->bw_timer.function = nullb_bwtimer_fn;
|
|
|
|
atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
|
|
|
|
hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
|
|
|
|
|
|
|
static bool should_timeout_request(struct request *rq)
|
|
|
|
{
|
|
|
|
struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
|
|
|
|
return should_fail(&dev->timeout_config.attr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool should_requeue_request(struct request *rq)
|
|
|
|
{
|
|
|
|
struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
|
|
|
|
struct nullb_device *dev = cmd->nq->dev;
|
|
|
|
|
|
|
|
return should_fail(&dev->requeue_config.attr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool should_init_hctx_fail(struct nullb_device *dev)
|
|
|
|
{
|
|
|
|
return should_fail(&dev->init_hctx_fault_config.attr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-01-10 09:06:23 -07:00
|
|
|
static bool should_timeout_request(struct request *rq)
|
|
|
|
{
|
2018-02-28 09:18:57 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-10 09:06:23 -07:00
|
|
|
|
2018-02-28 09:18:57 -07:00
|
|
|
static bool should_requeue_request(struct request *rq)
|
|
|
|
{
|
2018-01-10 09:06:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
static bool should_init_hctx_fail(struct nullb_device *dev)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-08-15 10:00:43 -07:00
|
|
|
static void null_map_queues(struct blk_mq_tag_set *set)
|
2021-04-17 08:29:49 -07:00
|
|
|
{
|
|
|
|
struct nullb *nullb = set->driver_data;
|
|
|
|
int i, qoff;
|
2021-10-29 03:39:26 -07:00
|
|
|
unsigned int submit_queues = g_submit_queues;
|
|
|
|
unsigned int poll_queues = g_poll_queues;
|
|
|
|
|
|
|
|
if (nullb) {
|
|
|
|
struct nullb_device *dev = nullb->dev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Refer nr_hw_queues of the tag set to check if the expected
|
|
|
|
* number of hardware queues are prepared. If block layer failed
|
|
|
|
* to prepare them, use previous numbers of submit queues and
|
|
|
|
* poll queues to map queues.
|
|
|
|
*/
|
|
|
|
if (set->nr_hw_queues ==
|
|
|
|
dev->submit_queues + dev->poll_queues) {
|
|
|
|
submit_queues = dev->submit_queues;
|
|
|
|
poll_queues = dev->poll_queues;
|
|
|
|
} else if (set->nr_hw_queues ==
|
|
|
|
dev->prev_submit_queues + dev->prev_poll_queues) {
|
|
|
|
submit_queues = dev->prev_submit_queues;
|
|
|
|
poll_queues = dev->prev_poll_queues;
|
|
|
|
} else {
|
|
|
|
pr_warn("tag set has unexpected nr_hw_queues: %d\n",
|
|
|
|
set->nr_hw_queues);
|
2022-08-15 10:00:42 -07:00
|
|
|
WARN_ON_ONCE(true);
|
|
|
|
submit_queues = 1;
|
|
|
|
poll_queues = 0;
|
2021-10-29 03:39:26 -07:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 08:29:49 -07:00
|
|
|
|
|
|
|
for (i = 0, qoff = 0; i < set->nr_maps; i++) {
|
|
|
|
struct blk_mq_queue_map *map = &set->map[i];
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case HCTX_TYPE_DEFAULT:
|
2021-10-29 03:39:26 -07:00
|
|
|
map->nr_queues = submit_queues;
|
2021-04-17 08:29:49 -07:00
|
|
|
break;
|
|
|
|
case HCTX_TYPE_READ:
|
|
|
|
map->nr_queues = 0;
|
|
|
|
continue;
|
|
|
|
case HCTX_TYPE_POLL:
|
2021-10-29 03:39:26 -07:00
|
|
|
map->nr_queues = poll_queues;
|
2021-04-17 08:29:49 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
map->queue_offset = qoff;
|
|
|
|
qoff += map->nr_queues;
|
|
|
|
blk_mq_map_queues(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int null_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
|
|
|
|
{
|
|
|
|
struct nullb_queue *nq = hctx->driver_data;
|
|
|
|
LIST_HEAD(list);
|
|
|
|
int nr = 0;
|
2023-09-01 05:03:06 -07:00
|
|
|
struct request *rq;
|
2021-04-17 08:29:49 -07:00
|
|
|
|
|
|
|
spin_lock(&nq->poll_lock);
|
|
|
|
list_splice_init(&nq->poll_list, &list);
|
2023-09-01 05:03:06 -07:00
|
|
|
list_for_each_entry(rq, &list, queuelist)
|
|
|
|
blk_mq_set_request_complete(rq);
|
2021-04-17 08:29:49 -07:00
|
|
|
spin_unlock(&nq->poll_lock);
|
|
|
|
|
|
|
|
while (!list_empty(&list)) {
|
|
|
|
struct nullb_cmd *cmd;
|
|
|
|
struct request *req;
|
|
|
|
|
|
|
|
req = list_first_entry(&list, struct request, queuelist);
|
|
|
|
list_del_init(&req->queuelist);
|
|
|
|
cmd = blk_mq_rq_to_pdu(req);
|
|
|
|
cmd->error = null_process_cmd(cmd, req_op(req), blk_rq_pos(req),
|
|
|
|
blk_rq_sectors(req));
|
2021-12-10 16:32:44 -07:00
|
|
|
if (!blk_mq_add_to_batch(req, iob, (__force int) cmd->error,
|
2021-12-03 01:17:03 -07:00
|
|
|
blk_mq_end_request_batch))
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_end_request(req, cmd->error);
|
2021-04-17 08:29:49 -07:00
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2022-07-06 05:03:51 -07:00
|
|
|
static enum blk_eh_timer_return null_timeout_rq(struct request *rq)
|
2018-01-09 12:47:24 -07:00
|
|
|
{
|
2021-04-17 08:29:49 -07:00
|
|
|
struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
|
2021-03-31 15:52:44 -07:00
|
|
|
struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
|
|
|
|
|
2021-04-17 08:29:49 -07:00
|
|
|
if (hctx->type == HCTX_TYPE_POLL) {
|
|
|
|
struct nullb_queue *nq = hctx->driver_data;
|
|
|
|
|
|
|
|
spin_lock(&nq->poll_lock);
|
2023-09-01 05:03:06 -07:00
|
|
|
/* The request may have completed meanwhile. */
|
|
|
|
if (blk_mq_request_completed(rq)) {
|
|
|
|
spin_unlock(&nq->poll_lock);
|
|
|
|
return BLK_EH_DONE;
|
|
|
|
}
|
2021-04-17 08:29:49 -07:00
|
|
|
list_del_init(&rq->queuelist);
|
|
|
|
spin_unlock(&nq->poll_lock);
|
|
|
|
}
|
|
|
|
|
2023-09-01 05:03:06 -07:00
|
|
|
pr_info("rq %p timed out\n", rq);
|
|
|
|
|
2021-03-31 15:52:44 -07:00
|
|
|
/*
|
|
|
|
* If the device is marked as blocking (i.e. memory backed or zoned
|
|
|
|
* device), the submission path may be blocked waiting for resources
|
|
|
|
* and cause real timeouts. For these real timeouts, the submission
|
|
|
|
* path will complete the request using blk_mq_complete_request().
|
|
|
|
* Only fake timeouts need to execute blk_mq_complete_request() here.
|
|
|
|
*/
|
|
|
|
cmd->error = BLK_STS_TIMEOUT;
|
2022-04-13 01:48:36 -07:00
|
|
|
if (cmd->fake_timeout || hctx->type == HCTX_TYPE_POLL)
|
2021-03-31 15:52:44 -07:00
|
|
|
blk_mq_complete_request(rq);
|
2018-05-29 06:52:33 -07:00
|
|
|
return BLK_EH_DONE;
|
2018-01-09 12:47:24 -07:00
|
|
|
}
|
|
|
|
|
2017-06-03 00:38:05 -07:00
|
|
|
static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
|
2023-03-13 21:11:06 -07:00
|
|
|
const struct blk_mq_queue_data *bd)
|
2013-10-25 03:52:25 -07:00
|
|
|
{
|
2023-03-13 21:11:06 -07:00
|
|
|
struct request *rq = bd->rq;
|
|
|
|
struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
|
2017-08-14 15:04:52 -07:00
|
|
|
struct nullb_queue *nq = hctx->driver_data;
|
2023-03-13 21:11:06 -07:00
|
|
|
sector_t nr_sectors = blk_rq_sectors(rq);
|
|
|
|
sector_t sector = blk_rq_pos(rq);
|
2021-04-17 08:29:49 -07:00
|
|
|
const bool is_poll = hctx->type == HCTX_TYPE_POLL;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-03-30 12:44:26 -07:00
|
|
|
might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
|
|
|
|
|
2021-04-17 08:29:49 -07:00
|
|
|
if (!is_poll && nq->dev->irqmode == NULL_IRQ_TIMER) {
|
null_blk: set a separate timer for each command
For the Timer IRQ mode (i.e., when command completions are delayed),
there is one timer for each CPU. Each of these timers
. has a completion queue associated with it, containing all the
command completions to be executed when the timer fires;
. is set, and a new completion-to-execute is inserted into its
completion queue, every time the dispatch code for a new command
happens to be executed on the CPU related to the timer.
This implies that, if the dispatch of a new command happens to be
executed on a CPU whose timer has already been set, but has not yet
fired, then the timer is set again, to the completion time of the
newly arrived command. When the timer eventually fires, all its queued
completions are executed.
This way of handling delayed command completions entails the following
problem: if more than one command completion is inserted into the
queue of a timer before the timer fires, then the expiration time for
the timer is moved forward every time each of these completions is
enqueued. As a consequence, only the last completion enqueued enjoys a
correct execution time, while all previous completions are unjustly
delayed until the last completion is executed (and at that time they
are executed all together).
Specifically, if all the above completions are enqueued almost at the
same time, then the problem is negligible. On the opposite end, if
every completion is enqueued a while after the previous completion was
enqueued (in the extreme case, it is enqueued only right before the
timer would have expired), then every enqueued completion, except for
the last one, experiences an inflated delay, proportional to the number
of completions enqueued after it. In the end, commands, and thus I/O
requests, may be completed at an arbitrarily lower rate than the
desired one.
This commit addresses this issue by replacing per-CPU timers with
per-command timers, i.e., by associating an individual timer with each
command.
Signed-off-by: Paolo Valente <paolo.valente@unimore.it>
Signed-off-by: Arianna Avanzini <avanzini@google.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
2015-12-01 03:48:17 -07:00
|
|
|
hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
|
|
cmd->timer.function = null_cmd_timer_expired;
|
|
|
|
}
|
2020-02-12 13:23:20 -07:00
|
|
|
cmd->error = BLK_STS_OK;
|
2017-08-14 15:04:52 -07:00
|
|
|
cmd->nq = nq;
|
2023-03-13 21:11:06 -07:00
|
|
|
cmd->fake_timeout = should_timeout_request(rq) ||
|
|
|
|
blk_should_fake_timeout(rq->q);
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2023-03-13 21:11:06 -07:00
|
|
|
if (should_requeue_request(rq)) {
|
2018-02-28 09:18:57 -07:00
|
|
|
/*
|
|
|
|
* Alternate between hitting the core BUSY path, and the
|
|
|
|
* driver driven requeue path
|
|
|
|
*/
|
|
|
|
nq->requeue_selection++;
|
|
|
|
if (nq->requeue_selection & 1)
|
|
|
|
return BLK_STS_RESOURCE;
|
2023-03-13 21:11:06 -07:00
|
|
|
blk_mq_requeue_request(rq, true);
|
|
|
|
return BLK_STS_OK;
|
2018-02-28 09:18:57 -07:00
|
|
|
}
|
2021-04-17 08:29:49 -07:00
|
|
|
|
2023-11-19 20:25:21 -07:00
|
|
|
if (test_bit(NULLB_DEV_FL_THROTTLED, &nq->dev->flags)) {
|
|
|
|
blk_status_t sts = null_handle_throttled(cmd);
|
|
|
|
|
|
|
|
if (sts != BLK_STS_OK)
|
|
|
|
return sts;
|
|
|
|
}
|
|
|
|
|
|
|
|
blk_mq_start_request(rq);
|
|
|
|
|
2021-04-17 08:29:49 -07:00
|
|
|
if (is_poll) {
|
|
|
|
spin_lock(&nq->poll_lock);
|
2023-03-13 21:11:06 -07:00
|
|
|
list_add_tail(&rq->queuelist, &nq->poll_list);
|
2021-04-17 08:29:49 -07:00
|
|
|
spin_unlock(&nq->poll_lock);
|
|
|
|
return BLK_STS_OK;
|
|
|
|
}
|
2021-03-31 15:52:44 -07:00
|
|
|
if (cmd->fake_timeout)
|
2018-02-28 09:18:57 -07:00
|
|
|
return BLK_STS_OK;
|
2018-01-10 09:06:23 -07:00
|
|
|
|
2023-11-19 20:25:21 -07:00
|
|
|
null_handle_cmd(cmd, sector, nr_sectors, req_op(rq));
|
|
|
|
return BLK_STS_OK;
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2023-09-13 08:16:16 -07:00
|
|
|
static void null_queue_rqs(struct request **rqlist)
|
|
|
|
{
|
|
|
|
struct request *requeue_list = NULL;
|
|
|
|
struct request **requeue_lastp = &requeue_list;
|
|
|
|
struct blk_mq_queue_data bd = { };
|
|
|
|
blk_status_t ret;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct request *rq = rq_list_pop(rqlist);
|
|
|
|
|
|
|
|
bd.rq = rq;
|
|
|
|
ret = null_queue_rq(rq->mq_hctx, &bd);
|
|
|
|
if (ret != BLK_STS_OK)
|
|
|
|
rq_list_add_tail(&requeue_lastp, rq);
|
|
|
|
} while (!rq_list_empty(*rqlist));
|
|
|
|
|
|
|
|
*rqlist = requeue_list;
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:26:20 -07:00
|
|
|
static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
|
|
|
|
{
|
|
|
|
nq->dev = nullb->dev;
|
2021-04-17 08:29:49 -07:00
|
|
|
INIT_LIST_HEAD(&nq->poll_list);
|
|
|
|
spin_lock_init(&nq->poll_lock);
|
2020-03-09 21:26:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
|
|
|
|
unsigned int hctx_idx)
|
|
|
|
{
|
|
|
|
struct nullb *nullb = hctx->queue->queuedata;
|
|
|
|
struct nullb_queue *nq;
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
if (should_init_hctx_fail(nullb->dev))
|
2020-03-09 21:26:23 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
2020-03-09 21:26:20 -07:00
|
|
|
nq = &nullb->queues[hctx_idx];
|
|
|
|
hctx->driver_data = nq;
|
|
|
|
null_init_queue(nullb, nq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct blk_mq_ops null_mq_ops = {
|
|
|
|
.queue_rq = null_queue_rq,
|
2023-09-13 08:16:16 -07:00
|
|
|
.queue_rqs = null_queue_rqs,
|
2020-03-09 21:26:20 -07:00
|
|
|
.complete = null_complete_rq,
|
|
|
|
.timeout = null_timeout_rq,
|
2021-04-17 08:29:49 -07:00
|
|
|
.poll = null_poll,
|
|
|
|
.map_queues = null_map_queues,
|
2020-03-09 21:26:20 -07:00
|
|
|
.init_hctx = null_init_hctx,
|
|
|
|
};
|
|
|
|
|
2016-09-16 05:25:05 -07:00
|
|
|
static void null_del_dev(struct nullb *nullb)
|
|
|
|
{
|
2020-03-09 21:26:22 -07:00
|
|
|
struct nullb_device *dev;
|
|
|
|
|
|
|
|
if (!nullb)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dev = nullb->dev;
|
2017-08-14 15:04:52 -07:00
|
|
|
|
2024-01-14 02:00:59 -07:00
|
|
|
ida_free(&nullb_indexes, nullb->index);
|
2017-08-14 15:04:55 -07:00
|
|
|
|
2016-09-16 05:25:05 -07:00
|
|
|
list_del_init(&nullb->list);
|
|
|
|
|
2018-01-05 06:15:57 -07:00
|
|
|
del_gendisk(nullb->disk);
|
2017-08-14 15:04:58 -07:00
|
|
|
|
|
|
|
if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
|
|
|
|
hrtimer_cancel(&nullb->bw_timer);
|
|
|
|
atomic_long_set(&nullb->cur_bytes, LONG_MAX);
|
2024-02-20 02:32:44 -07:00
|
|
|
blk_mq_start_stopped_hw_queues(nullb->q, true);
|
2017-08-14 15:04:58 -07:00
|
|
|
}
|
|
|
|
|
2022-06-18 23:05:52 -07:00
|
|
|
put_disk(nullb->disk);
|
2024-02-20 02:32:44 -07:00
|
|
|
if (nullb->tag_set == &nullb->__tag_set)
|
2017-06-20 13:22:01 -07:00
|
|
|
blk_mq_free_tag_set(nullb->tag_set);
|
2024-02-20 02:32:44 -07:00
|
|
|
kfree(nullb->queues);
|
2017-08-14 15:04:59 -07:00
|
|
|
if (null_cache_active(nullb))
|
|
|
|
null_free_device_storage(nullb->dev, true);
|
2016-09-16 05:25:05 -07:00
|
|
|
kfree(nullb);
|
2017-08-14 15:04:52 -07:00
|
|
|
dev->nullb = NULL;
|
2016-09-16 05:25:05 -07:00
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:48 -07:00
|
|
|
static void null_config_discard(struct nullb *nullb, struct queue_limits *lim)
|
2017-08-14 15:04:57 -07:00
|
|
|
{
|
|
|
|
if (nullb->dev->discard == false)
|
|
|
|
return;
|
2020-05-20 16:01:52 -07:00
|
|
|
|
2020-11-19 18:55:16 -07:00
|
|
|
if (!nullb->dev->memory_backed) {
|
|
|
|
nullb->dev->discard = false;
|
|
|
|
pr_info("discard option is ignored without memory backing\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:01:52 -07:00
|
|
|
if (nullb->dev->zoned) {
|
|
|
|
nullb->dev->discard = false;
|
|
|
|
pr_info("discard option is ignored in zoned mode\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:48 -07:00
|
|
|
lim->max_hw_discard_sectors = UINT_MAX >> 9;
|
2016-09-16 05:25:05 -07:00
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
static const struct block_device_operations null_ops = {
|
2020-07-01 01:59:43 -07:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.report_zones = null_report_zones,
|
|
|
|
};
|
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
static int setup_queues(struct nullb *nullb)
|
|
|
|
{
|
2021-04-17 08:29:49 -07:00
|
|
|
int nqueues = nr_cpu_ids;
|
|
|
|
|
|
|
|
if (g_poll_queues)
|
|
|
|
nqueues += g_poll_queues;
|
|
|
|
|
|
|
|
nullb->queues = kcalloc(nqueues, sizeof(struct nullb_queue),
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 14:03:40 -07:00
|
|
|
GFP_KERNEL);
|
2013-10-25 03:52:25 -07:00
|
|
|
if (!nullb->queues)
|
2013-12-18 05:41:43 -07:00
|
|
|
return -ENOMEM;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2013-12-18 05:41:43 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:46 -07:00
|
|
|
static int null_init_tag_set(struct blk_mq_tag_set *set, int poll_queues)
|
2017-06-20 13:22:01 -07:00
|
|
|
{
|
|
|
|
set->ops = &null_mq_ops;
|
2024-02-20 02:32:46 -07:00
|
|
|
set->cmd_size = sizeof(struct nullb_cmd);
|
2024-02-20 02:32:45 -07:00
|
|
|
set->timeout = 5 * HZ;
|
2024-02-20 02:32:46 -07:00
|
|
|
set->nr_maps = 1;
|
2022-07-08 10:49:49 -07:00
|
|
|
if (poll_queues) {
|
|
|
|
set->nr_hw_queues += poll_queues;
|
2024-02-20 02:32:46 -07:00
|
|
|
set->nr_maps += 2;
|
2022-07-08 10:49:49 -07:00
|
|
|
}
|
2017-06-20 13:22:01 -07:00
|
|
|
return blk_mq_alloc_tag_set(set);
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:46 -07:00
|
|
|
static int null_init_global_tag_set(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (tag_set.ops)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tag_set.nr_hw_queues = g_submit_queues;
|
|
|
|
tag_set.queue_depth = g_hw_queue_depth;
|
|
|
|
tag_set.numa_node = g_home_node;
|
|
|
|
tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
|
|
|
|
if (g_no_sched)
|
|
|
|
tag_set.flags |= BLK_MQ_F_NO_SCHED;
|
|
|
|
if (g_shared_tag_bitmap)
|
|
|
|
tag_set.flags |= BLK_MQ_F_TAG_HCTX_SHARED;
|
|
|
|
if (g_blocking)
|
|
|
|
tag_set.flags |= BLK_MQ_F_BLOCKING;
|
|
|
|
|
|
|
|
error = null_init_tag_set(&tag_set, g_poll_queues);
|
|
|
|
if (error)
|
|
|
|
tag_set.ops = NULL;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int null_setup_tagset(struct nullb *nullb)
|
|
|
|
{
|
|
|
|
if (nullb->dev->shared_tags) {
|
|
|
|
nullb->tag_set = &tag_set;
|
|
|
|
return null_init_global_tag_set();
|
|
|
|
}
|
|
|
|
|
|
|
|
nullb->tag_set = &nullb->__tag_set;
|
|
|
|
nullb->tag_set->driver_data = nullb;
|
|
|
|
nullb->tag_set->nr_hw_queues = nullb->dev->submit_queues;
|
|
|
|
nullb->tag_set->queue_depth = nullb->dev->hw_queue_depth;
|
|
|
|
nullb->tag_set->numa_node = nullb->dev->home_node;
|
|
|
|
nullb->tag_set->flags = BLK_MQ_F_SHOULD_MERGE;
|
|
|
|
if (nullb->dev->no_sched)
|
|
|
|
nullb->tag_set->flags |= BLK_MQ_F_NO_SCHED;
|
|
|
|
if (nullb->dev->shared_tag_bitmap)
|
|
|
|
nullb->tag_set->flags |= BLK_MQ_F_TAG_HCTX_SHARED;
|
|
|
|
if (nullb->dev->blocking)
|
|
|
|
nullb->tag_set->flags |= BLK_MQ_F_BLOCKING;
|
|
|
|
return null_init_tag_set(nullb->tag_set, nullb->dev->poll_queues);
|
|
|
|
}
|
|
|
|
|
2019-12-03 02:39:01 -07:00
|
|
|
static int null_validate_conf(struct nullb_device *dev)
|
2017-08-14 15:04:54 -07:00
|
|
|
{
|
2023-04-16 15:03:39 -07:00
|
|
|
if (dev->queue_mode == NULL_Q_RQ) {
|
|
|
|
pr_err("legacy IO path is no longer available\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2024-02-20 02:32:44 -07:00
|
|
|
if (dev->queue_mode == NULL_Q_BIO) {
|
|
|
|
pr_err("BIO-based IO path is no longer available, using blk-mq instead.\n");
|
|
|
|
dev->queue_mode = NULL_Q_MQ;
|
|
|
|
}
|
2023-04-16 15:03:39 -07:00
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
if (dev->use_per_node_hctx) {
|
2017-08-14 15:04:54 -07:00
|
|
|
if (dev->submit_queues != nr_online_nodes)
|
|
|
|
dev->submit_queues = nr_online_nodes;
|
|
|
|
} else if (dev->submit_queues > nr_cpu_ids)
|
|
|
|
dev->submit_queues = nr_cpu_ids;
|
|
|
|
else if (dev->submit_queues == 0)
|
|
|
|
dev->submit_queues = 1;
|
2021-10-29 03:39:26 -07:00
|
|
|
dev->prev_submit_queues = dev->submit_queues;
|
|
|
|
|
|
|
|
if (dev->poll_queues > g_poll_queues)
|
|
|
|
dev->poll_queues = g_poll_queues;
|
|
|
|
dev->prev_poll_queues = dev->poll_queues;
|
2017-08-14 15:04:54 -07:00
|
|
|
dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
|
2017-08-14 15:04:56 -07:00
|
|
|
|
|
|
|
/* Do memory allocation, so set blocking */
|
|
|
|
if (dev->memory_backed)
|
|
|
|
dev->blocking = true;
|
2017-08-14 15:04:59 -07:00
|
|
|
else /* cache is meaningless */
|
|
|
|
dev->cache_size = 0;
|
|
|
|
dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
|
|
|
|
dev->cache_size);
|
2017-08-14 15:04:58 -07:00
|
|
|
dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
|
2019-12-03 02:39:01 -07:00
|
|
|
|
|
|
|
if (dev->zoned &&
|
|
|
|
(!dev->zone_size || !is_power_of_2(dev->zone_size))) {
|
|
|
|
pr_err("zone_size must be power-of-two\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-08-14 15:04:54 -07:00
|
|
|
}
|
|
|
|
|
2018-01-11 03:31:25 -07:00
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
2018-02-28 09:18:57 -07:00
|
|
|
static bool __null_setup_fault(struct fault_attr *attr, char *str)
|
|
|
|
{
|
|
|
|
if (!str[0])
|
2018-01-10 09:06:23 -07:00
|
|
|
return true;
|
|
|
|
|
2018-02-28 09:18:57 -07:00
|
|
|
if (!setup_fault_attr(attr, str))
|
2018-01-10 09:06:23 -07:00
|
|
|
return false;
|
|
|
|
|
2018-02-28 09:18:57 -07:00
|
|
|
attr->verbose = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool null_setup_fault(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
|
|
|
|
if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
|
|
|
|
return false;
|
|
|
|
if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
|
|
|
|
return false;
|
2020-03-09 21:26:23 -07:00
|
|
|
if (!__null_setup_fault(&null_init_hctx_attr, g_init_hctx_str))
|
|
|
|
return false;
|
2018-01-11 03:31:25 -07:00
|
|
|
#endif
|
2018-01-10 09:06:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
static int null_add_dev(struct nullb_device *dev)
|
2016-09-16 05:25:05 -07:00
|
|
|
{
|
2024-02-20 02:32:48 -07:00
|
|
|
struct queue_limits lim = {
|
|
|
|
.logical_block_size = dev->blocksize,
|
|
|
|
.physical_block_size = dev->blocksize,
|
|
|
|
.max_hw_sectors = dev->max_sectors,
|
|
|
|
};
|
|
|
|
|
2016-09-16 05:25:05 -07:00
|
|
|
struct nullb *nullb;
|
2014-09-02 09:38:49 -07:00
|
|
|
int rv;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2019-12-03 02:39:01 -07:00
|
|
|
rv = null_validate_conf(dev);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
2017-08-14 15:04:54 -07:00
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
|
2014-09-02 09:38:49 -07:00
|
|
|
if (!nullb) {
|
|
|
|
rv = -ENOMEM;
|
2014-04-15 13:14:00 -07:00
|
|
|
goto out;
|
2014-09-02 09:38:49 -07:00
|
|
|
}
|
2017-08-14 15:04:52 -07:00
|
|
|
nullb->dev = dev;
|
|
|
|
dev->nullb = nullb;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
|
|
|
spin_lock_init(&nullb->lock);
|
|
|
|
|
2014-09-02 09:38:49 -07:00
|
|
|
rv = setup_queues(nullb);
|
|
|
|
if (rv)
|
2014-04-15 13:14:00 -07:00
|
|
|
goto out_free_nullb;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2024-02-20 02:32:46 -07:00
|
|
|
rv = null_setup_tagset(nullb);
|
2024-02-20 02:32:44 -07:00
|
|
|
if (rv)
|
|
|
|
goto out_cleanup_queues;
|
2021-05-20 22:51:15 -07:00
|
|
|
|
2024-02-20 02:32:48 -07:00
|
|
|
if (dev->virt_boundary)
|
|
|
|
lim.virt_boundary_mask = PAGE_SIZE - 1;
|
|
|
|
null_config_discard(nullb, &lim);
|
|
|
|
if (dev->zoned) {
|
|
|
|
rv = null_init_zoned_dev(dev, &lim);
|
|
|
|
if (rv)
|
|
|
|
goto out_cleanup_tags;
|
|
|
|
}
|
|
|
|
|
2024-06-16 23:04:40 -07:00
|
|
|
if (dev->cache_size > 0) {
|
|
|
|
set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
|
|
|
|
lim.features |= BLK_FEAT_WRITE_CACHE;
|
|
|
|
if (dev->fua)
|
|
|
|
lim.features |= BLK_FEAT_FUA;
|
|
|
|
}
|
|
|
|
|
2024-02-20 02:32:48 -07:00
|
|
|
nullb->disk = blk_mq_alloc_disk(nullb->tag_set, &lim, nullb);
|
2024-02-20 02:32:44 -07:00
|
|
|
if (IS_ERR(nullb->disk)) {
|
|
|
|
rv = PTR_ERR(nullb->disk);
|
2024-02-20 02:32:48 -07:00
|
|
|
goto out_cleanup_zone;
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
2024-02-20 02:32:44 -07:00
|
|
|
nullb->q = nullb->disk->queue;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-08-14 15:04:58 -07:00
|
|
|
if (dev->mbps) {
|
|
|
|
set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
|
|
|
|
nullb_setup_bwtimer(nullb);
|
|
|
|
}
|
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
nullb->q->queuedata = nullb;
|
|
|
|
|
2024-01-14 02:00:59 -07:00
|
|
|
rv = ida_alloc(&nullb_indexes, GFP_KERNEL);
|
2024-05-23 08:39:34 -07:00
|
|
|
if (rv < 0)
|
2024-02-20 02:32:48 -07:00
|
|
|
goto out_cleanup_disk;
|
2024-05-23 08:39:34 -07:00
|
|
|
|
2022-07-15 01:12:14 -07:00
|
|
|
nullb->index = rv;
|
|
|
|
dev->index = rv;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
if (config_item_name(&dev->group.cg_item)) {
|
2022-04-19 17:57:18 -07:00
|
|
|
/* Use configfs dir name as the device name */
|
|
|
|
snprintf(nullb->disk_name, sizeof(nullb->disk_name),
|
2023-03-27 07:37:33 -07:00
|
|
|
"%s", config_item_name(&dev->group.cg_item));
|
2022-04-19 17:57:18 -07:00
|
|
|
} else {
|
|
|
|
sprintf(nullb->disk_name, "nullb%d", nullb->index);
|
|
|
|
}
|
2015-11-12 12:25:10 -07:00
|
|
|
|
2024-02-20 02:32:47 -07:00
|
|
|
set_capacity(nullb->disk,
|
|
|
|
((sector_t)nullb->dev->size * SZ_1M) >> SECTOR_SHIFT);
|
|
|
|
nullb->disk->major = null_major;
|
|
|
|
nullb->disk->first_minor = nullb->index;
|
|
|
|
nullb->disk->minors = 1;
|
|
|
|
nullb->disk->fops = &null_ops;
|
|
|
|
nullb->disk->private_data = nullb;
|
|
|
|
strscpy_pad(nullb->disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
|
|
|
|
|
|
|
|
if (nullb->dev->zoned) {
|
|
|
|
rv = null_register_zoned_dev(nullb);
|
|
|
|
if (rv)
|
|
|
|
goto out_ida_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = add_disk(nullb->disk);
|
2016-09-16 05:25:05 -07:00
|
|
|
if (rv)
|
2022-07-15 01:12:14 -07:00
|
|
|
goto out_ida_free;
|
2016-02-11 06:49:13 -07:00
|
|
|
|
|
|
|
list_add_tail(&nullb->list, &nullb_list);
|
2016-03-04 09:27:04 -07:00
|
|
|
|
2022-04-19 17:57:17 -07:00
|
|
|
pr_info("disk %s created\n", nullb->disk_name);
|
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
return 0;
|
2022-07-15 01:12:14 -07:00
|
|
|
|
|
|
|
out_ida_free:
|
|
|
|
ida_free(&nullb_indexes, nullb->index);
|
2021-05-20 22:51:15 -07:00
|
|
|
out_cleanup_disk:
|
2022-06-18 23:05:52 -07:00
|
|
|
put_disk(nullb->disk);
|
2024-03-29 17:53:00 -07:00
|
|
|
out_cleanup_zone:
|
|
|
|
null_free_zoned_dev(dev);
|
2014-04-15 13:14:00 -07:00
|
|
|
out_cleanup_tags:
|
2024-02-20 02:32:44 -07:00
|
|
|
if (nullb->tag_set == &nullb->__tag_set)
|
2017-06-20 13:22:01 -07:00
|
|
|
blk_mq_free_tag_set(nullb->tag_set);
|
2014-04-15 13:14:00 -07:00
|
|
|
out_cleanup_queues:
|
2024-02-20 02:32:44 -07:00
|
|
|
kfree(nullb->queues);
|
2014-04-15 13:14:00 -07:00
|
|
|
out_free_nullb:
|
|
|
|
kfree(nullb);
|
2020-03-09 21:26:21 -07:00
|
|
|
dev->nullb = NULL;
|
2014-04-15 13:14:00 -07:00
|
|
|
out:
|
2014-09-02 09:38:49 -07:00
|
|
|
return rv;
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:57:18 -07:00
|
|
|
static struct nullb *null_find_dev_by_name(const char *name)
|
|
|
|
{
|
|
|
|
struct nullb *nullb = NULL, *nb;
|
|
|
|
|
|
|
|
mutex_lock(&lock);
|
|
|
|
list_for_each_entry(nb, &nullb_list, list) {
|
|
|
|
if (strcmp(nb->disk_name, name) == 0) {
|
|
|
|
nullb = nb;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&lock);
|
|
|
|
|
|
|
|
return nullb;
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:57:16 -07:00
|
|
|
static int null_create_dev(void)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dev = null_alloc_dev();
|
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2024-05-23 08:39:34 -07:00
|
|
|
mutex_lock(&lock);
|
2022-04-19 17:57:16 -07:00
|
|
|
ret = null_add_dev(dev);
|
2024-05-23 08:39:34 -07:00
|
|
|
mutex_unlock(&lock);
|
2022-04-19 17:57:16 -07:00
|
|
|
if (ret) {
|
|
|
|
null_free_dev(dev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_destroy_dev(struct nullb *nullb)
|
|
|
|
{
|
|
|
|
struct nullb_device *dev = nullb->dev;
|
|
|
|
|
|
|
|
null_del_dev(nullb);
|
2023-06-04 23:23:53 -07:00
|
|
|
null_free_device_storage(dev, false);
|
2022-04-19 17:57:16 -07:00
|
|
|
null_free_dev(dev);
|
|
|
|
}
|
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
static int __init null_init(void)
|
|
|
|
{
|
2015-12-08 13:47:34 -07:00
|
|
|
int ret = 0;
|
2013-10-25 03:52:25 -07:00
|
|
|
unsigned int i;
|
2015-12-08 13:47:34 -07:00
|
|
|
struct nullb *nullb;
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-08-14 15:04:52 -07:00
|
|
|
if (g_bs > PAGE_SIZE) {
|
2019-09-16 07:07:59 -07:00
|
|
|
pr_warn("invalid block size\n");
|
|
|
|
pr_warn("defaults block size to %lu\n", PAGE_SIZE);
|
2017-08-14 15:04:52 -07:00
|
|
|
g_bs = PAGE_SIZE;
|
2014-01-21 04:29:59 -07:00
|
|
|
}
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2019-04-05 14:42:45 -07:00
|
|
|
if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) {
|
2019-09-16 07:07:59 -07:00
|
|
|
pr_err("invalid home_node value\n");
|
2019-04-05 14:42:45 -07:00
|
|
|
g_home_node = NUMA_NO_NODE;
|
|
|
|
}
|
|
|
|
|
2023-03-27 07:37:33 -07:00
|
|
|
if (!null_setup_fault())
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-10-11 16:58:17 -07:00
|
|
|
if (g_queue_mode == NULL_Q_RQ) {
|
2022-04-19 17:57:15 -07:00
|
|
|
pr_err("legacy IO path is no longer available\n");
|
2018-10-11 16:58:17 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2022-04-19 17:57:15 -07:00
|
|
|
|
2024-02-20 02:32:44 -07:00
|
|
|
if (g_use_per_node_hctx) {
|
2017-08-14 15:04:52 -07:00
|
|
|
if (g_submit_queues != nr_online_nodes) {
|
2019-09-16 07:07:59 -07:00
|
|
|
pr_warn("submit_queues param is set to %u.\n",
|
2022-04-19 17:57:15 -07:00
|
|
|
nr_online_nodes);
|
2017-08-14 15:04:52 -07:00
|
|
|
g_submit_queues = nr_online_nodes;
|
2013-12-20 16:11:01 -07:00
|
|
|
}
|
2022-04-19 17:57:15 -07:00
|
|
|
} else if (g_submit_queues > nr_cpu_ids) {
|
2017-08-14 15:04:52 -07:00
|
|
|
g_submit_queues = nr_cpu_ids;
|
2022-04-19 17:57:15 -07:00
|
|
|
} else if (g_submit_queues <= 0) {
|
2017-08-14 15:04:52 -07:00
|
|
|
g_submit_queues = 1;
|
2022-04-19 17:57:15 -07:00
|
|
|
}
|
2013-10-25 03:52:25 -07:00
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
config_group_init(&nullb_subsys.su_group);
|
|
|
|
mutex_init(&nullb_subsys.su_mutex);
|
|
|
|
|
|
|
|
ret = configfs_register_subsystem(&nullb_subsys);
|
|
|
|
if (ret)
|
2024-01-29 21:21:34 -07:00
|
|
|
return ret;
|
2017-08-14 15:04:53 -07:00
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
mutex_init(&lock);
|
|
|
|
|
|
|
|
null_major = register_blkdev(0, "nullb");
|
2017-07-06 08:00:07 -07:00
|
|
|
if (null_major < 0) {
|
|
|
|
ret = null_major;
|
2017-08-14 15:04:53 -07:00
|
|
|
goto err_conf;
|
2017-07-06 08:00:07 -07:00
|
|
|
}
|
2013-10-25 03:52:25 -07:00
|
|
|
|
|
|
|
for (i = 0; i < nr_devices; i++) {
|
2022-04-19 17:57:16 -07:00
|
|
|
ret = null_create_dev();
|
|
|
|
if (ret)
|
2015-12-08 13:47:34 -07:00
|
|
|
goto err_dev;
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
2019-09-16 07:07:59 -07:00
|
|
|
pr_info("module loaded\n");
|
2013-10-25 03:52:25 -07:00
|
|
|
return 0;
|
2015-12-08 13:47:34 -07:00
|
|
|
|
|
|
|
err_dev:
|
|
|
|
while (!list_empty(&nullb_list)) {
|
|
|
|
nullb = list_entry(nullb_list.next, struct nullb, list);
|
2022-04-19 17:57:16 -07:00
|
|
|
null_destroy_dev(nullb);
|
2015-12-08 13:47:34 -07:00
|
|
|
}
|
|
|
|
unregister_blkdev(null_major, "nullb");
|
2017-08-14 15:04:53 -07:00
|
|
|
err_conf:
|
|
|
|
configfs_unregister_subsystem(&nullb_subsys);
|
2015-12-08 13:47:34 -07:00
|
|
|
return ret;
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit null_exit(void)
|
|
|
|
{
|
|
|
|
struct nullb *nullb;
|
|
|
|
|
2017-08-14 15:04:53 -07:00
|
|
|
configfs_unregister_subsystem(&nullb_subsys);
|
|
|
|
|
2013-10-25 03:52:25 -07:00
|
|
|
unregister_blkdev(null_major, "nullb");
|
|
|
|
|
|
|
|
mutex_lock(&lock);
|
|
|
|
while (!list_empty(&nullb_list)) {
|
|
|
|
nullb = list_entry(nullb_list.next, struct nullb, list);
|
2022-04-19 17:57:16 -07:00
|
|
|
null_destroy_dev(nullb);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
mutex_unlock(&lock);
|
2015-11-19 04:50:08 -07:00
|
|
|
|
2024-01-29 21:21:34 -07:00
|
|
|
if (tag_set.ops)
|
2017-06-20 13:22:01 -07:00
|
|
|
blk_mq_free_tag_set(&tag_set);
|
2024-04-25 10:16:35 -07:00
|
|
|
|
|
|
|
mutex_destroy(&lock);
|
2013-10-25 03:52:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(null_init);
|
|
|
|
module_exit(null_exit);
|
|
|
|
|
2017-08-25 11:53:15 -07:00
|
|
|
MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
|
2024-05-06 00:55:38 -07:00
|
|
|
MODULE_DESCRIPTION("multi queue aware block test driver");
|
2013-10-25 03:52:25 -07:00
|
|
|
MODULE_LICENSE("GPL");
|