Skip to content

Commit 8841c66

Browse files
Xiubo LiAshish Samant
Xiubo Li
authored and
Ashish Samant
committed
tcmu: fix crash when removing the tcmu device
Orabug: 27952054 Before the nl REMOVE msg has been sent to the userspace, the ring's and other resources have been released, but the userspace maybe still using them. And then we can see the crash messages like: ring broken, not handling completions BUG: unable to handle kernel paging request at ffffffffffffffd0 IP: tcmu_handle_completions+0x134/0x2f0 [target_core_user] PGD 11bdc0c067 P4D 11bdc0c067 PUD 11bdc0e067 PMD 0 Oops: 0000 [#1] SMP cmd_id not found, ring is broken RIP: 0010:tcmu_handle_completions+0x134/0x2f0 [target_core_user] RSP: 0018:ffffb8a2d8983d88 EFLAGS: 00010296 RAX: 0000000000000000 RBX: ffffb8a2aaa4e000 RCX: 00000000ffffffff RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000220 R10: 0000000076c71401 R11: ffff8d2e76c713f0 R12: ffffb8a2aad56bc0 R13: 000000000000001c R14: ffff8d2e32c90000 R15: ffff8d2e76c713f0 FS: 00007f411ffff700(0000) GS:ffff8d1e7fdc0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffffffffffffd0 CR3: 0000001027070000 CR4: 00000000001406e0 Call Trace: ? tcmu_irqcontrol+0x2a/0x40 [target_core_user] ? uio_write+0x7b/0xc0 [uio] ? __vfs_write+0x37/0x150 ? __getnstimeofday64+0x3b/0xd0 ? vfs_write+0xb2/0x1b0 ? syscall_trace_enter+0x1d0/0x2b0 ? SyS_write+0x55/0xc0 ? do_syscall_64+0x67/0x150 ? entry_SYSCALL64_slow_path+0x25/0x25 Code: 41 5d 41 5e 41 5f 5d c3 83 f8 01 0f 85 cf 01 00 00 48 8b 7d d0 e8 dd 5c 1d f3 41 0f b7 74 24 04 48 8b 7d c8 31 d2 e8 5c c7 1b f3 <48> 8b 7d d0 49 89 c7 c6 07 00 0f 1f 40 00 4d 85 ff 0f 84 82 01 RIP: tcmu_handle_completions+0x134/0x2f0 [target_core_user] RSP: ffffb8a2d8983d88 CR2: ffffffffffffffd0 And the crash also could happen in tcmu_page_fault and other places. Signed-off-by: Zhang Zhuoyu <[email protected]> Signed-off-by: Xiubo Li <[email protected]> Reviewed-by: Mike Christie <[email protected]> Signed-off-by: Nicholas Bellinger <[email protected]> (cherry picked from commit c22adc0) Signed-off-by: Ashish Samant <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]>
1 parent c939f60 commit 8841c66

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

drivers/target/target_core_user.c

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,8 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
11191119
init_waitqueue_head(&udev->nl_cmd_wq);
11201120
spin_lock_init(&udev->nl_cmd_lock);
11211121

1122+
INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1123+
11221124
return &udev->se_dev;
11231125
}
11241126

@@ -1287,10 +1289,54 @@ static void tcmu_dev_call_rcu(struct rcu_head *p)
12871289
kfree(udev);
12881290
}
12891291

1292+
static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1293+
{
1294+
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1295+
kmem_cache_free(tcmu_cmd_cache, cmd);
1296+
return 0;
1297+
}
1298+
return -EINVAL;
1299+
}
1300+
1301+
static void tcmu_blocks_release(struct tcmu_dev *udev)
1302+
{
1303+
int i;
1304+
struct page *page;
1305+
1306+
/* Try to release all block pages */
1307+
mutex_lock(&udev->cmdr_lock);
1308+
for (i = 0; i <= udev->dbi_max; i++) {
1309+
page = radix_tree_delete(&udev->data_blocks, i);
1310+
if (page) {
1311+
__free_page(page);
1312+
atomic_dec(&global_db_count);
1313+
}
1314+
}
1315+
mutex_unlock(&udev->cmdr_lock);
1316+
}
1317+
12901318
static void tcmu_dev_kref_release(struct kref *kref)
12911319
{
12921320
struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
12931321
struct se_device *dev = &udev->se_dev;
1322+
struct tcmu_cmd *cmd;
1323+
bool all_expired = true;
1324+
int i;
1325+
1326+
vfree(udev->mb_addr);
1327+
udev->mb_addr = NULL;
1328+
1329+
/* Upper layer should drain all requests before calling this */
1330+
spin_lock_irq(&udev->commands_lock);
1331+
idr_for_each_entry(&udev->commands, cmd, i) {
1332+
if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1333+
all_expired = false;
1334+
}
1335+
idr_destroy(&udev->commands);
1336+
spin_unlock_irq(&udev->commands_lock);
1337+
WARN_ON(!all_expired);
1338+
1339+
tcmu_blocks_release(udev);
12941340

12951341
call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
12961342
}
@@ -1483,8 +1529,6 @@ static int tcmu_configure_device(struct se_device *dev)
14831529
WARN_ON(udev->data_size % PAGE_SIZE);
14841530
WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
14851531

1486-
INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1487-
14881532
info->version = __stringify(TCMU_MAILBOX_VERSION);
14891533

14901534
info->mem[0].name = "tcm-user command & data buffer";
@@ -1534,44 +1578,19 @@ static int tcmu_configure_device(struct se_device *dev)
15341578
uio_unregister_device(&udev->uio_info);
15351579
err_register:
15361580
vfree(udev->mb_addr);
1581+
udev->mb_addr = NULL;
15371582
err_vzalloc:
15381583
kfree(info->name);
15391584
info->name = NULL;
15401585

15411586
return ret;
15421587
}
15431588

1544-
static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1545-
{
1546-
if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1547-
kmem_cache_free(tcmu_cmd_cache, cmd);
1548-
return 0;
1549-
}
1550-
return -EINVAL;
1551-
}
1552-
15531589
static bool tcmu_dev_configured(struct tcmu_dev *udev)
15541590
{
15551591
return udev->uio_info.uio_dev ? true : false;
15561592
}
15571593

1558-
static void tcmu_blocks_release(struct tcmu_dev *udev)
1559-
{
1560-
int i;
1561-
struct page *page;
1562-
1563-
/* Try to release all block pages */
1564-
mutex_lock(&udev->cmdr_lock);
1565-
for (i = 0; i <= udev->dbi_max; i++) {
1566-
page = radix_tree_delete(&udev->data_blocks, i);
1567-
if (page) {
1568-
__free_page(page);
1569-
atomic_dec(&global_db_count);
1570-
}
1571-
}
1572-
mutex_unlock(&udev->cmdr_lock);
1573-
}
1574-
15751594
static void tcmu_free_device(struct se_device *dev)
15761595
{
15771596
struct tcmu_dev *udev = TCMU_DEV(dev);
@@ -1583,30 +1602,13 @@ static void tcmu_free_device(struct se_device *dev)
15831602
static void tcmu_destroy_device(struct se_device *dev)
15841603
{
15851604
struct tcmu_dev *udev = TCMU_DEV(dev);
1586-
struct tcmu_cmd *cmd;
1587-
bool all_expired = true;
1588-
int i;
15891605

15901606
del_timer_sync(&udev->timeout);
15911607

15921608
mutex_lock(&root_udev_mutex);
15931609
list_del(&udev->node);
15941610
mutex_unlock(&root_udev_mutex);
15951611

1596-
vfree(udev->mb_addr);
1597-
1598-
/* Upper layer should drain all requests before calling this */
1599-
spin_lock_irq(&udev->commands_lock);
1600-
idr_for_each_entry(&udev->commands, cmd, i) {
1601-
if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1602-
all_expired = false;
1603-
}
1604-
idr_destroy(&udev->commands);
1605-
spin_unlock_irq(&udev->commands_lock);
1606-
WARN_ON(!all_expired);
1607-
1608-
tcmu_blocks_release(udev);
1609-
16101612
tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
16111613

16121614
uio_unregister_device(&udev->uio_info);

0 commit comments

Comments
 (0)