]>
Commit | Line | Data |
---|---|---|
21f93658 GKH |
1 | From a4b732a248d12cbdb46999daf0bf288c011335eb Mon Sep 17 00:00:00 2001 |
2 | From: Liang Chen <liangchen.linux@gmail.com> | |
3 | Date: Thu, 25 Apr 2019 00:48:31 +0800 | |
4 | Subject: bcache: fix a race between cache register and cacheset unregister | |
5 | ||
6 | From: Liang Chen <liangchen.linux@gmail.com> | |
7 | ||
8 | commit a4b732a248d12cbdb46999daf0bf288c011335eb upstream. | |
9 | ||
10 | There is a race between cache device register and cache set unregister. | |
11 | For an already registered cache device, register_bcache will call | |
12 | bch_is_open to iterate through all cachesets and check every cache | |
13 | there. The race occurs if cache_set_free executes at the same time and | |
14 | clears the caches right before ca is dereferenced in bch_is_open_cache. | |
15 | To close the race, let's make sure the clean up work is protected by | |
16 | the bch_register_lock as well. | |
17 | ||
18 | This issue can be reproduced as follows, | |
19 | while true; do echo /dev/XXX> /sys/fs/bcache/register ; done& | |
20 | while true; do echo 1> /sys/block/XXX/bcache/set/unregister ; done & | |
21 | ||
22 | and results in the following oops, | |
23 | ||
24 | [ +0.000053] BUG: unable to handle kernel NULL pointer dereference at 0000000000000998 | |
25 | [ +0.000457] #PF error: [normal kernel read fault] | |
26 | [ +0.000464] PGD 800000003ca9d067 P4D 800000003ca9d067 PUD 3ca9c067 PMD 0 | |
27 | [ +0.000388] Oops: 0000 [#1] SMP PTI | |
28 | [ +0.000269] CPU: 1 PID: 3266 Comm: bash Not tainted 5.0.0+ #6 | |
29 | [ +0.000346] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.fc28 04/01/2014 | |
30 | [ +0.000472] RIP: 0010:register_bcache+0x1829/0x1990 [bcache] | |
31 | [ +0.000344] Code: b0 48 83 e8 50 48 81 fa e0 e1 10 c0 0f 84 a9 00 00 00 48 89 c6 48 89 ca 0f b7 ba 54 04 00 00 4c 8b 82 60 0c 00 00 85 ff 74 2f <49> 3b a8 98 09 00 00 74 4e 44 8d 47 ff 31 ff 49 c1 e0 03 eb 0d | |
32 | [ +0.000839] RSP: 0018:ffff92ee804cbd88 EFLAGS: 00010202 | |
33 | [ +0.000328] RAX: ffffffffc010e190 RBX: ffff918b5c6b5000 RCX: ffff918b7d8e0000 | |
34 | [ +0.000399] RDX: ffff918b7d8e0000 RSI: ffffffffc010e190 RDI: 0000000000000001 | |
35 | [ +0.000398] RBP: ffff918b7d318340 R08: 0000000000000000 R09: ffffffffb9bd2d7a | |
36 | [ +0.000385] R10: ffff918b7eb253c0 R11: ffffb95980f51200 R12: ffffffffc010e1a0 | |
37 | [ +0.000411] R13: fffffffffffffff2 R14: 000000000000000b R15: ffff918b7e232620 | |
38 | [ +0.000384] FS: 00007f955bec2740(0000) GS:ffff918b7eb00000(0000) knlGS:0000000000000000 | |
39 | [ +0.000420] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 | |
40 | [ +0.000801] CR2: 0000000000000998 CR3: 000000003cad6000 CR4: 00000000001406e0 | |
41 | [ +0.000837] Call Trace: | |
42 | [ +0.000682] ? _cond_resched+0x10/0x20 | |
43 | [ +0.000691] ? __kmalloc+0x131/0x1b0 | |
44 | [ +0.000710] kernfs_fop_write+0xfa/0x170 | |
45 | [ +0.000733] __vfs_write+0x2e/0x190 | |
46 | [ +0.000688] ? inode_security+0x10/0x30 | |
47 | [ +0.000698] ? selinux_file_permission+0xd2/0x120 | |
48 | [ +0.000752] ? security_file_permission+0x2b/0x100 | |
49 | [ +0.000753] vfs_write+0xa8/0x1a0 | |
50 | [ +0.000676] ksys_write+0x4d/0xb0 | |
51 | [ +0.000699] do_syscall_64+0x3a/0xf0 | |
52 | [ +0.000692] entry_SYSCALL_64_after_hwframe+0x44/0xa9 | |
53 | ||
54 | Signed-off-by: Liang Chen <liangchen.linux@gmail.com> | |
55 | Cc: stable@vger.kernel.org | |
56 | Signed-off-by: Coly Li <colyli@suse.de> | |
57 | Signed-off-by: Jens Axboe <axboe@kernel.dk> | |
58 | Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> | |
59 | ||
60 | --- | |
61 | drivers/md/bcache/super.c | 2 +- | |
62 | 1 file changed, 1 insertion(+), 1 deletion(-) | |
63 | ||
64 | --- a/drivers/md/bcache/super.c | |
65 | +++ b/drivers/md/bcache/super.c | |
66 | @@ -1516,6 +1516,7 @@ static void cache_set_free(struct closur | |
67 | bch_btree_cache_free(c); | |
68 | bch_journal_free(c); | |
69 | ||
70 | + mutex_lock(&bch_register_lock); | |
71 | for_each_cache(ca, c, i) | |
72 | if (ca) { | |
73 | ca->set = NULL; | |
74 | @@ -1534,7 +1535,6 @@ static void cache_set_free(struct closur | |
75 | mempool_exit(&c->search); | |
76 | kfree(c->devices); | |
77 | ||
78 | - mutex_lock(&bch_register_lock); | |
79 | list_del(&c->list); | |
80 | mutex_unlock(&bch_register_lock); | |
81 |