mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-01-04 04:04:19 +00:00
nbd: fold nbd config initialization into nbd_alloc_config()
There are no functional changes, make the code cleaner and prepare to fix null-ptr-dereference while accessing 'nbd->config'. Signed-off-by: Li Nan <linan122@huawei.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20231116162316.1740402-2-linan666@huaweicloud.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
8a554c6234
commit
1b59860540
@ -1530,17 +1530,20 @@ static int nbd_ioctl(struct block_device *bdev, blk_mode_t mode,
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct nbd_config *nbd_alloc_config(void)
|
static int nbd_alloc_and_init_config(struct nbd_device *nbd)
|
||||||
{
|
{
|
||||||
struct nbd_config *config;
|
struct nbd_config *config;
|
||||||
|
|
||||||
|
if (WARN_ON(nbd->config))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
if (!try_module_get(THIS_MODULE))
|
if (!try_module_get(THIS_MODULE))
|
||||||
return ERR_PTR(-ENODEV);
|
return -ENODEV;
|
||||||
|
|
||||||
config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
|
config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
module_put(THIS_MODULE);
|
module_put(THIS_MODULE);
|
||||||
return ERR_PTR(-ENOMEM);
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
atomic_set(&config->recv_threads, 0);
|
atomic_set(&config->recv_threads, 0);
|
||||||
@ -1548,7 +1551,10 @@ static struct nbd_config *nbd_alloc_config(void)
|
|||||||
init_waitqueue_head(&config->conn_wait);
|
init_waitqueue_head(&config->conn_wait);
|
||||||
config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
|
config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
|
||||||
atomic_set(&config->live_connections, 0);
|
atomic_set(&config->live_connections, 0);
|
||||||
return config;
|
nbd->config = config;
|
||||||
|
refcount_set(&nbd->config_refs, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_open(struct gendisk *disk, blk_mode_t mode)
|
static int nbd_open(struct gendisk *disk, blk_mode_t mode)
|
||||||
@ -1567,21 +1573,17 @@ static int nbd_open(struct gendisk *disk, blk_mode_t mode)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
if (!refcount_inc_not_zero(&nbd->config_refs)) {
|
if (!refcount_inc_not_zero(&nbd->config_refs)) {
|
||||||
struct nbd_config *config;
|
|
||||||
|
|
||||||
mutex_lock(&nbd->config_lock);
|
mutex_lock(&nbd->config_lock);
|
||||||
if (refcount_inc_not_zero(&nbd->config_refs)) {
|
if (refcount_inc_not_zero(&nbd->config_refs)) {
|
||||||
mutex_unlock(&nbd->config_lock);
|
mutex_unlock(&nbd->config_lock);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
config = nbd_alloc_config();
|
ret = nbd_alloc_and_init_config(nbd);
|
||||||
if (IS_ERR(config)) {
|
if (ret) {
|
||||||
ret = PTR_ERR(config);
|
|
||||||
mutex_unlock(&nbd->config_lock);
|
mutex_unlock(&nbd->config_lock);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
nbd->config = config;
|
|
||||||
refcount_set(&nbd->config_refs, 1);
|
|
||||||
refcount_inc(&nbd->refs);
|
refcount_inc(&nbd->refs);
|
||||||
mutex_unlock(&nbd->config_lock);
|
mutex_unlock(&nbd->config_lock);
|
||||||
if (max_part)
|
if (max_part)
|
||||||
@ -1990,22 +1992,17 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
|
|||||||
pr_err("nbd%d already in use\n", index);
|
pr_err("nbd%d already in use\n", index);
|
||||||
return -EBUSY;
|
return -EBUSY;
|
||||||
}
|
}
|
||||||
if (WARN_ON(nbd->config)) {
|
|
||||||
mutex_unlock(&nbd->config_lock);
|
ret = nbd_alloc_and_init_config(nbd);
|
||||||
nbd_put(nbd);
|
if (ret) {
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
config = nbd_alloc_config();
|
|
||||||
if (IS_ERR(config)) {
|
|
||||||
mutex_unlock(&nbd->config_lock);
|
mutex_unlock(&nbd->config_lock);
|
||||||
nbd_put(nbd);
|
nbd_put(nbd);
|
||||||
pr_err("couldn't allocate config\n");
|
pr_err("couldn't allocate config\n");
|
||||||
return PTR_ERR(config);
|
return ret;
|
||||||
}
|
}
|
||||||
nbd->config = config;
|
|
||||||
refcount_set(&nbd->config_refs, 1);
|
|
||||||
set_bit(NBD_RT_BOUND, &config->runtime_flags);
|
|
||||||
|
|
||||||
|
config = nbd->config;
|
||||||
|
set_bit(NBD_RT_BOUND, &config->runtime_flags);
|
||||||
ret = nbd_genl_size_set(info, nbd);
|
ret = nbd_genl_size_set(info, nbd);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto out;
|
goto out;
|
||||||
|
Loading…
Reference in New Issue
Block a user