Skip to content

Commit 3123ac7

Browse files
Li Nanaxboe
authored andcommitted
nbd: factor out a helper to get nbd_config without holding 'config_lock'
There are no functional changes, just to make 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 1b59860 commit 3123ac7

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

drivers/block/nbd.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ static u32 req_to_nbd_cmd_type(struct request *req)
395395
}
396396
}
397397

398+
static struct nbd_config *nbd_get_config_unlocked(struct nbd_device *nbd)
399+
{
400+
if (refcount_inc_not_zero(&nbd->config_refs))
401+
return nbd->config;
402+
403+
return NULL;
404+
}
405+
398406
static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
399407
{
400408
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
@@ -409,13 +417,13 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
409417
return BLK_EH_DONE;
410418
}
411419

412-
if (!refcount_inc_not_zero(&nbd->config_refs)) {
420+
config = nbd_get_config_unlocked(nbd);
421+
if (!config) {
413422
cmd->status = BLK_STS_TIMEOUT;
414423
__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
415424
mutex_unlock(&cmd->lock);
416425
goto done;
417426
}
418-
config = nbd->config;
419427

420428
if (config->num_connections > 1 ||
421429
(config->num_connections == 1 && nbd->tag_set.timeout)) {
@@ -977,12 +985,12 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
977985
struct nbd_sock *nsock;
978986
int ret;
979987

980-
if (!refcount_inc_not_zero(&nbd->config_refs)) {
988+
config = nbd_get_config_unlocked(nbd);
989+
if (!config) {
981990
dev_err_ratelimited(disk_to_dev(nbd->disk),
982991
"Socks array is empty\n");
983992
return -EINVAL;
984993
}
985-
config = nbd->config;
986994

987995
if (index >= config->num_connections) {
988996
dev_err_ratelimited(disk_to_dev(nbd->disk),
@@ -1560,6 +1568,7 @@ static int nbd_alloc_and_init_config(struct nbd_device *nbd)
15601568
static int nbd_open(struct gendisk *disk, blk_mode_t mode)
15611569
{
15621570
struct nbd_device *nbd;
1571+
struct nbd_config *config;
15631572
int ret = 0;
15641573

15651574
mutex_lock(&nbd_index_mutex);
@@ -1572,7 +1581,9 @@ static int nbd_open(struct gendisk *disk, blk_mode_t mode)
15721581
ret = -ENXIO;
15731582
goto out;
15741583
}
1575-
if (!refcount_inc_not_zero(&nbd->config_refs)) {
1584+
1585+
config = nbd_get_config_unlocked(nbd);
1586+
if (!config) {
15761587
mutex_lock(&nbd->config_lock);
15771588
if (refcount_inc_not_zero(&nbd->config_refs)) {
15781589
mutex_unlock(&nbd->config_lock);
@@ -1588,7 +1599,7 @@ static int nbd_open(struct gendisk *disk, blk_mode_t mode)
15881599
mutex_unlock(&nbd->config_lock);
15891600
if (max_part)
15901601
set_bit(GD_NEED_PART_SCAN, &disk->state);
1591-
} else if (nbd_disconnected(nbd->config)) {
1602+
} else if (nbd_disconnected(config)) {
15921603
if (max_part)
15931604
set_bit(GD_NEED_PART_SCAN, &disk->state);
15941605
}
@@ -2205,15 +2216,15 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
22052216
}
22062217
mutex_unlock(&nbd_index_mutex);
22072218

2208-
if (!refcount_inc_not_zero(&nbd->config_refs)) {
2219+
config = nbd_get_config_unlocked(nbd);
2220+
if (!config) {
22092221
dev_err(nbd_to_dev(nbd),
22102222
"not configured, cannot reconfigure\n");
22112223
nbd_put(nbd);
22122224
return -EINVAL;
22132225
}
22142226

22152227
mutex_lock(&nbd->config_lock);
2216-
config = nbd->config;
22172228
if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) ||
22182229
!nbd->pid) {
22192230
dev_err(nbd_to_dev(nbd),

0 commit comments

Comments
 (0)