@@ -1173,6 +1173,8 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1173
1173
init_waitqueue_head (& udev -> nl_cmd_wq );
1174
1174
spin_lock_init (& udev -> nl_cmd_lock );
1175
1175
1176
+ INIT_RADIX_TREE (& udev -> data_blocks , GFP_KERNEL );
1177
+
1176
1178
return & udev -> se_dev ;
1177
1179
}
1178
1180
@@ -1351,10 +1353,54 @@ static void tcmu_dev_call_rcu(struct rcu_head *p)
1351
1353
kfree (udev );
1352
1354
}
1353
1355
1356
+ static int tcmu_check_and_free_pending_cmd (struct tcmu_cmd * cmd )
1357
+ {
1358
+ if (test_bit (TCMU_CMD_BIT_EXPIRED , & cmd -> flags )) {
1359
+ kmem_cache_free (tcmu_cmd_cache , cmd );
1360
+ return 0 ;
1361
+ }
1362
+ return - EINVAL ;
1363
+ }
1364
+
1365
+ static void tcmu_blocks_release (struct tcmu_dev * udev )
1366
+ {
1367
+ int i ;
1368
+ struct page * page ;
1369
+
1370
+ /* Try to release all block pages */
1371
+ mutex_lock (& udev -> cmdr_lock );
1372
+ for (i = 0 ; i <= udev -> dbi_max ; i ++ ) {
1373
+ page = radix_tree_delete (& udev -> data_blocks , i );
1374
+ if (page ) {
1375
+ __free_page (page );
1376
+ atomic_dec (& global_db_count );
1377
+ }
1378
+ }
1379
+ mutex_unlock (& udev -> cmdr_lock );
1380
+ }
1381
+
1354
1382
static void tcmu_dev_kref_release (struct kref * kref )
1355
1383
{
1356
1384
struct tcmu_dev * udev = container_of (kref , struct tcmu_dev , kref );
1357
1385
struct se_device * dev = & udev -> se_dev ;
1386
+ struct tcmu_cmd * cmd ;
1387
+ bool all_expired = true;
1388
+ int i ;
1389
+
1390
+ vfree (udev -> mb_addr );
1391
+ udev -> mb_addr = NULL ;
1392
+
1393
+ /* Upper layer should drain all requests before calling this */
1394
+ spin_lock_irq (& udev -> commands_lock );
1395
+ idr_for_each_entry (& udev -> commands , cmd , i ) {
1396
+ if (tcmu_check_and_free_pending_cmd (cmd ) != 0 )
1397
+ all_expired = false;
1398
+ }
1399
+ idr_destroy (& udev -> commands );
1400
+ spin_unlock_irq (& udev -> commands_lock );
1401
+ WARN_ON (!all_expired );
1402
+
1403
+ tcmu_blocks_release (udev );
1358
1404
1359
1405
call_rcu (& dev -> rcu_head , tcmu_dev_call_rcu );
1360
1406
}
@@ -1547,8 +1593,6 @@ static int tcmu_configure_device(struct se_device *dev)
1547
1593
WARN_ON (udev -> data_size % PAGE_SIZE );
1548
1594
WARN_ON (udev -> data_size % DATA_BLOCK_SIZE );
1549
1595
1550
- INIT_RADIX_TREE (& udev -> data_blocks , GFP_KERNEL );
1551
-
1552
1596
info -> version = __stringify (TCMU_MAILBOX_VERSION );
1553
1597
1554
1598
info -> mem [0 ].name = "tcm-user command & data buffer" ;
@@ -1598,44 +1642,19 @@ static int tcmu_configure_device(struct se_device *dev)
1598
1642
uio_unregister_device (& udev -> uio_info );
1599
1643
err_register :
1600
1644
vfree (udev -> mb_addr );
1645
+ udev -> mb_addr = NULL ;
1601
1646
err_vzalloc :
1602
1647
kfree (info -> name );
1603
1648
info -> name = NULL ;
1604
1649
1605
1650
return ret ;
1606
1651
}
1607
1652
1608
- static int tcmu_check_and_free_pending_cmd (struct tcmu_cmd * cmd )
1609
- {
1610
- if (test_bit (TCMU_CMD_BIT_EXPIRED , & cmd -> flags )) {
1611
- kmem_cache_free (tcmu_cmd_cache , cmd );
1612
- return 0 ;
1613
- }
1614
- return - EINVAL ;
1615
- }
1616
-
1617
1653
static bool tcmu_dev_configured (struct tcmu_dev * udev )
1618
1654
{
1619
1655
return udev -> uio_info .uio_dev ? true : false;
1620
1656
}
1621
1657
1622
- static void tcmu_blocks_release (struct tcmu_dev * udev )
1623
- {
1624
- int i ;
1625
- struct page * page ;
1626
-
1627
- /* Try to release all block pages */
1628
- mutex_lock (& udev -> cmdr_lock );
1629
- for (i = 0 ; i <= udev -> dbi_max ; i ++ ) {
1630
- page = radix_tree_delete (& udev -> data_blocks , i );
1631
- if (page ) {
1632
- __free_page (page );
1633
- atomic_dec (& global_db_count );
1634
- }
1635
- }
1636
- mutex_unlock (& udev -> cmdr_lock );
1637
- }
1638
-
1639
1658
static void tcmu_free_device (struct se_device * dev )
1640
1659
{
1641
1660
struct tcmu_dev * udev = TCMU_DEV (dev );
@@ -1647,9 +1666,6 @@ static void tcmu_free_device(struct se_device *dev)
1647
1666
static void tcmu_destroy_device (struct se_device * dev )
1648
1667
{
1649
1668
struct tcmu_dev * udev = TCMU_DEV (dev );
1650
- struct tcmu_cmd * cmd ;
1651
- bool all_expired = true;
1652
- int i ;
1653
1669
1654
1670
del_timer_sync (& udev -> timeout );
1655
1671
@@ -1664,20 +1680,6 @@ static void tcmu_destroy_device(struct se_device *dev)
1664
1680
mutex_unlock (& udev -> cmdr_lock );
1665
1681
mutex_unlock (& root_udev_waiter_mutex );
1666
1682
1667
- vfree (udev -> mb_addr );
1668
-
1669
- /* Upper layer should drain all requests before calling this */
1670
- spin_lock_irq (& udev -> commands_lock );
1671
- idr_for_each_entry (& udev -> commands , cmd , i ) {
1672
- if (tcmu_check_and_free_pending_cmd (cmd ) != 0 )
1673
- all_expired = false;
1674
- }
1675
- idr_destroy (& udev -> commands );
1676
- spin_unlock_irq (& udev -> commands_lock );
1677
- WARN_ON (!all_expired );
1678
-
1679
- tcmu_blocks_release (udev );
1680
-
1681
1683
tcmu_netlink_event (udev , TCMU_CMD_REMOVED_DEVICE , 0 , NULL );
1682
1684
1683
1685
uio_unregister_device (& udev -> uio_info );
0 commit comments