Skip to content

Commit 8454d68

Browse files
xiubliaxboe
authored andcommitted
nbd: fix possible page fault for nbd disk
When the NBD_CFLAG_DESTROY_ON_DISCONNECT flag is set and at the same time when the socket is closed due to the server daemon is restarted, just before the last DISCONNET is totally done if we start a new connection by using the old nbd_index, there will be crashing randomly, like: <3>[ 110.151949] block nbd1: Receive control failed (result -32) <1>[ 110.152024] BUG: unable to handle page fault for address: 0000058000000840 <1>[ 110.152063] #PF: supervisor read access in kernel mode <1>[ 110.152083] #PF: error_code(0x0000) - not-present page <6>[ 110.152094] PGD 0 P4D 0 <4>[ 110.152106] Oops: 0000 [raspberrypi#1] SMP PTI <4>[ 110.152120] CPU: 0 PID: 6698 Comm: kworker/u5:1 Kdump: loaded Not tainted 5.3.0-rc4+ raspberrypi#2 <4>[ 110.152136] Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 <4>[ 110.152166] Workqueue: knbd-recv recv_work [nbd] <4>[ 110.152187] RIP: 0010:__dev_printk+0xd/0x67 <4>[ 110.152206] Code: 10 e8 c5 fd ff ff 48 8b 4c 24 18 65 48 33 0c 25 28 00 [...] <4>[ 110.152244] RSP: 0018:ffffa41581f13d18 EFLAGS: 00010206 <4>[ 110.152256] RAX: ffffa41581f13d30 RBX: ffff96dd7374e900 RCX: 0000000000000000 <4>[ 110.152271] RDX: ffffa41581f13d20 RSI: 00000580000007f0 RDI: ffffffff970ec24f <4>[ 110.152285] RBP: ffffa41581f13d80 R08: ffff96dd7fc17908 R09: 0000000000002e56 <4>[ 110.152299] R10: ffffffff970ec24f R11: 0000000000000003 R12: ffff96dd7374e900 <4>[ 110.152313] R13: 0000000000000000 R14: ffff96dd7374e9d8 R15: ffff96dd6e3b02c8 <4>[ 110.152329] FS: 0000000000000000(0000) GS:ffff96dd7fc00000(0000) knlGS:0000000000000000 <4>[ 110.152362] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 <4>[ 110.152383] CR2: 0000058000000840 CR3: 0000000067cc6002 CR4: 00000000001606f0 <4>[ 110.152401] Call Trace: <4>[ 110.152422] _dev_err+0x6c/0x83 <4>[ 110.152435] nbd_read_stat.cold+0xda/0x578 [nbd] <4>[ 110.152448] ? __switch_to_asm+0x34/0x70 <4>[ 110.152468] ? __switch_to_asm+0x40/0x70 <4>[ 110.152478] ? __switch_to_asm+0x34/0x70 <4>[ 110.152491] ? __switch_to_asm+0x40/0x70 <4>[ 110.152501] ? __switch_to_asm+0x34/0x70 <4>[ 110.152511] ? __switch_to_asm+0x40/0x70 <4>[ 110.152522] ? __switch_to_asm+0x34/0x70 <4>[ 110.152533] recv_work+0x35/0x9e [nbd] <4>[ 110.152547] process_one_work+0x19d/0x340 <4>[ 110.152558] worker_thread+0x50/0x3b0 <4>[ 110.152568] kthread+0xfb/0x130 <4>[ 110.152577] ? process_one_work+0x340/0x340 <4>[ 110.152609] ? kthread_park+0x80/0x80 <4>[ 110.152637] ret_from_fork+0x35/0x40 This is very easy to reproduce by running the nbd-runner. Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Xiubo Li <xiubli@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ec76a7b commit 8454d68

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎drivers/block/nbd.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/ioctl.h>
2727
#include <linux/mutex.h>
2828
#include <linux/compiler.h>
29+
#include <linux/completion.h>
2930
#include <linux/err.h>
3031
#include <linux/kernel.h>
3132
#include <linux/slab.h>
@@ -80,6 +81,9 @@ struct link_dead_args {
8081
#define NBD_RT_DESTROY_ON_DISCONNECT 6
8182
#define NBD_RT_DISCONNECT_ON_CLOSE 7
8283

84+
#define NBD_DESTROY_ON_DISCONNECT 0
85+
#define NBD_DISCONNECT_REQUESTED 1
86+
8387
struct nbd_config {
8488
u32 flags;
8589
unsigned long runtime_flags;
@@ -113,6 +117,9 @@ struct nbd_device {
113117
struct list_head list;
114118
struct task_struct *task_recv;
115119
struct task_struct *task_setup;
120+
121+
struct completion *destroy_complete;
122+
unsigned long flags;
116123
};
117124

118125
#define NBD_CMD_REQUEUED 1
@@ -223,6 +230,16 @@ static void nbd_dev_remove(struct nbd_device *nbd)
223230
disk->private_data = NULL;
224231
put_disk(disk);
225232
}
233+
234+
/*
235+
* Place this in the last just before the nbd is freed to
236+
* make sure that the disk and the related kobject are also
237+
* totally removed to avoid duplicate creation of the same
238+
* one.
239+
*/
240+
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) && nbd->destroy_complete)
241+
complete(nbd->destroy_complete);
242+
226243
kfree(nbd);
227244
}
228245

@@ -1125,6 +1142,7 @@ static int nbd_disconnect(struct nbd_device *nbd)
11251142

11261143
dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
11271144
set_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
1145+
set_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags);
11281146
send_disconnects(nbd);
11291147
return 0;
11301148
}
@@ -1636,6 +1654,7 @@ static int nbd_dev_add(int index)
16361654
nbd->tag_set.flags = BLK_MQ_F_SHOULD_MERGE |
16371655
BLK_MQ_F_BLOCKING;
16381656
nbd->tag_set.driver_data = nbd;
1657+
nbd->destroy_complete = NULL;
16391658

16401659
err = blk_mq_alloc_tag_set(&nbd->tag_set);
16411660
if (err)
@@ -1750,6 +1769,7 @@ static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
17501769

17511770
static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
17521771
{
1772+
DECLARE_COMPLETION_ONSTACK(destroy_complete);
17531773
struct nbd_device *nbd = NULL;
17541774
struct nbd_config *config;
17551775
int index = -1;
@@ -1801,6 +1821,17 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
18011821
mutex_unlock(&nbd_index_mutex);
18021822
return -EINVAL;
18031823
}
1824+
1825+
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
1826+
test_bit(NBD_DISCONNECT_REQUESTED, &nbd->flags)) {
1827+
nbd->destroy_complete = &destroy_complete;
1828+
mutex_unlock(&nbd_index_mutex);
1829+
1830+
/* Wait untill the the nbd stuff is totally destroyed */
1831+
wait_for_completion(&destroy_complete);
1832+
goto again;
1833+
}
1834+
18041835
if (!refcount_inc_not_zero(&nbd->refs)) {
18051836
mutex_unlock(&nbd_index_mutex);
18061837
if (index == -1)
@@ -1855,7 +1886,10 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
18551886
if (flags & NBD_CFLAG_DESTROY_ON_DISCONNECT) {
18561887
set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
18571888
&config->runtime_flags);
1889+
set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
18581890
put_dev = true;
1891+
} else {
1892+
clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
18591893
}
18601894
if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {
18611895
set_bit(NBD_RT_DISCONNECT_ON_CLOSE,
@@ -2029,10 +2063,12 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
20292063
if (!test_and_set_bit(NBD_RT_DESTROY_ON_DISCONNECT,
20302064
&config->runtime_flags))
20312065
put_dev = true;
2066+
set_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
20322067
} else {
20332068
if (test_and_clear_bit(NBD_RT_DESTROY_ON_DISCONNECT,
20342069
&config->runtime_flags))
20352070
refcount_inc(&nbd->refs);
2071+
clear_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags);
20362072
}
20372073

20382074
if (flags & NBD_CFLAG_DISCONNECT_ON_CLOSE) {

0 commit comments

Comments
 (0)