Skip to content

Commit 1b59860

Browse files
Li Nanaxboe
authored andcommitted
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 <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 8a554c6 commit 1b59860

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

drivers/block/nbd.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,25 +1530,31 @@ static int nbd_ioctl(struct block_device *bdev, blk_mode_t mode,
15301530
return error;
15311531
}
15321532

1533-
static struct nbd_config *nbd_alloc_config(void)
1533+
static int nbd_alloc_and_init_config(struct nbd_device *nbd)
15341534
{
15351535
struct nbd_config *config;
15361536

1537+
if (WARN_ON(nbd->config))
1538+
return -EINVAL;
1539+
15371540
if (!try_module_get(THIS_MODULE))
1538-
return ERR_PTR(-ENODEV);
1541+
return -ENODEV;
15391542

15401543
config = kzalloc(sizeof(struct nbd_config), GFP_NOFS);
15411544
if (!config) {
15421545
module_put(THIS_MODULE);
1543-
return ERR_PTR(-ENOMEM);
1546+
return -ENOMEM;
15441547
}
15451548

15461549
atomic_set(&config->recv_threads, 0);
15471550
init_waitqueue_head(&config->recv_wq);
15481551
init_waitqueue_head(&config->conn_wait);
15491552
config->blksize_bits = NBD_DEF_BLKSIZE_BITS;
15501553
atomic_set(&config->live_connections, 0);
1551-
return config;
1554+
nbd->config = config;
1555+
refcount_set(&nbd->config_refs, 1);
1556+
1557+
return 0;
15521558
}
15531559

15541560
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)
15671573
goto out;
15681574
}
15691575
if (!refcount_inc_not_zero(&nbd->config_refs)) {
1570-
struct nbd_config *config;
1571-
15721576
mutex_lock(&nbd->config_lock);
15731577
if (refcount_inc_not_zero(&nbd->config_refs)) {
15741578
mutex_unlock(&nbd->config_lock);
15751579
goto out;
15761580
}
1577-
config = nbd_alloc_config();
1578-
if (IS_ERR(config)) {
1579-
ret = PTR_ERR(config);
1581+
ret = nbd_alloc_and_init_config(nbd);
1582+
if (ret) {
15801583
mutex_unlock(&nbd->config_lock);
15811584
goto out;
15821585
}
1583-
nbd->config = config;
1584-
refcount_set(&nbd->config_refs, 1);
1586+
15851587
refcount_inc(&nbd->refs);
15861588
mutex_unlock(&nbd->config_lock);
15871589
if (max_part)
@@ -1990,22 +1992,17 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
19901992
pr_err("nbd%d already in use\n", index);
19911993
return -EBUSY;
19921994
}
1993-
if (WARN_ON(nbd->config)) {
1994-
mutex_unlock(&nbd->config_lock);
1995-
nbd_put(nbd);
1996-
return -EINVAL;
1997-
}
1998-
config = nbd_alloc_config();
1999-
if (IS_ERR(config)) {
1995+
1996+
ret = nbd_alloc_and_init_config(nbd);
1997+
if (ret) {
20001998
mutex_unlock(&nbd->config_lock);
20011999
nbd_put(nbd);
20022000
pr_err("couldn't allocate config\n");
2003-
return PTR_ERR(config);
2001+
return ret;
20042002
}
2005-
nbd->config = config;
2006-
refcount_set(&nbd->config_refs, 1);
2007-
set_bit(NBD_RT_BOUND, &config->runtime_flags);
20082003

2004+
config = nbd->config;
2005+
set_bit(NBD_RT_BOUND, &config->runtime_flags);
20092006
ret = nbd_genl_size_set(info, nbd);
20102007
if (ret)
20112008
goto out;

0 commit comments

Comments
 (0)