Commit 6e4df4c6 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

nbd: reduce the nbd_index_mutex scope

nbd_index_mutex is currently held over add_disk and inside ->open, which
leads to lock order reversals.  Refactor the device creation code path
so that nbd_dev_add is called without nbd_index_mutex lock held and
only takes it for the IDR insertation.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20210811124428.2368491-7-hch@lst.de
[axboe: fix whitespace]
Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 6177b56c
...@@ -1683,7 +1683,7 @@ static const struct blk_mq_ops nbd_mq_ops = { ...@@ -1683,7 +1683,7 @@ static const struct blk_mq_ops nbd_mq_ops = {
.timeout = nbd_xmit_timeout, .timeout = nbd_xmit_timeout,
}; };
static struct nbd_device *nbd_dev_add(int index) static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
{ {
struct nbd_device *nbd; struct nbd_device *nbd;
struct gendisk *disk; struct gendisk *disk;
...@@ -1709,6 +1709,7 @@ static struct nbd_device *nbd_dev_add(int index) ...@@ -1709,6 +1709,7 @@ static struct nbd_device *nbd_dev_add(int index)
if (err) if (err)
goto out_free_nbd; goto out_free_nbd;
mutex_lock(&nbd_index_mutex);
if (index >= 0) { if (index >= 0) {
err = idr_alloc(&nbd_index_idr, nbd, index, index + 1, err = idr_alloc(&nbd_index_idr, nbd, index, index + 1,
GFP_KERNEL); GFP_KERNEL);
...@@ -1719,6 +1720,7 @@ static struct nbd_device *nbd_dev_add(int index) ...@@ -1719,6 +1720,7 @@ static struct nbd_device *nbd_dev_add(int index)
if (err >= 0) if (err >= 0)
index = err; index = err;
} }
mutex_unlock(&nbd_index_mutex);
if (err < 0) if (err < 0)
goto out_free_tags; goto out_free_tags;
nbd->index = index; nbd->index = index;
...@@ -1745,7 +1747,7 @@ static struct nbd_device *nbd_dev_add(int index) ...@@ -1745,7 +1747,7 @@ static struct nbd_device *nbd_dev_add(int index)
mutex_init(&nbd->config_lock); mutex_init(&nbd->config_lock);
refcount_set(&nbd->config_refs, 0); refcount_set(&nbd->config_refs, 0);
refcount_set(&nbd->refs, 1); refcount_set(&nbd->refs, refs);
INIT_LIST_HEAD(&nbd->list); INIT_LIST_HEAD(&nbd->list);
disk->major = NBD_MAJOR; disk->major = NBD_MAJOR;
disk->first_minor = index << part_shift; disk->first_minor = index << part_shift;
...@@ -1849,34 +1851,35 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) ...@@ -1849,34 +1851,35 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
nbd = idr_find(&nbd_index_idr, index); nbd = idr_find(&nbd_index_idr, index);
} }
if (!nbd) { if (nbd) {
nbd = nbd_dev_add(index); if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
if (IS_ERR(nbd)) { test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
nbd->destroy_complete = &destroy_complete;
mutex_unlock(&nbd_index_mutex); mutex_unlock(&nbd_index_mutex);
pr_err("nbd: failed to add new device\n");
return PTR_ERR(nbd); /* wait until the nbd device is completely destroyed */
wait_for_completion(&destroy_complete);
goto again;
} }
}
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) && if (!refcount_inc_not_zero(&nbd->refs)) {
test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) { mutex_unlock(&nbd_index_mutex);
nbd->destroy_complete = &destroy_complete; if (index == -1)
goto again;
pr_err("nbd: device at index %d is going down\n",
index);
return -EINVAL;
}
mutex_unlock(&nbd_index_mutex); mutex_unlock(&nbd_index_mutex);
} else {
/* Wait untill the the nbd stuff is totally destroyed */
wait_for_completion(&destroy_complete);
goto again;
}
if (!refcount_inc_not_zero(&nbd->refs)) {
mutex_unlock(&nbd_index_mutex); mutex_unlock(&nbd_index_mutex);
if (index == -1)
goto again; nbd = nbd_dev_add(index, 2);
printk(KERN_ERR "nbd: device at index %d is going down\n", if (IS_ERR(nbd)) {
index); pr_err("nbd: failed to add new device\n");
return -EINVAL; return PTR_ERR(nbd);
}
} }
mutex_unlock(&nbd_index_mutex);
mutex_lock(&nbd->config_lock); mutex_lock(&nbd->config_lock);
if (refcount_read(&nbd->config_refs)) { if (refcount_read(&nbd->config_refs)) {
...@@ -2432,10 +2435,8 @@ static int __init nbd_init(void) ...@@ -2432,10 +2435,8 @@ static int __init nbd_init(void)
} }
nbd_dbg_init(); nbd_dbg_init();
mutex_lock(&nbd_index_mutex);
for (i = 0; i < nbds_max; i++) for (i = 0; i < nbds_max; i++)
nbd_dev_add(i); nbd_dev_add(i, 1);
mutex_unlock(&nbd_index_mutex);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment