@@ -1125,6 +1125,8 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1125
1125
init_waitqueue_head (& udev -> nl_cmd_wq );
1126
1126
spin_lock_init (& udev -> nl_cmd_lock );
1127
1127
1128
+ INIT_RADIX_TREE (& udev -> data_blocks , GFP_KERNEL );
1129
+
1128
1130
return & udev -> se_dev ;
1129
1131
}
1130
1132
@@ -1293,10 +1295,54 @@ static void tcmu_dev_call_rcu(struct rcu_head *p)
1293
1295
kfree (udev );
1294
1296
}
1295
1297
1298
+ static int tcmu_check_and_free_pending_cmd (struct tcmu_cmd * cmd )
1299
+ {
1300
+ if (test_bit (TCMU_CMD_BIT_EXPIRED , & cmd -> flags )) {
1301
+ kmem_cache_free (tcmu_cmd_cache , cmd );
1302
+ return 0 ;
1303
+ }
1304
+ return - EINVAL ;
1305
+ }
1306
+
1307
+ static void tcmu_blocks_release (struct tcmu_dev * udev )
1308
+ {
1309
+ int i ;
1310
+ struct page * page ;
1311
+
1312
+ /* Try to release all block pages */
1313
+ mutex_lock (& udev -> cmdr_lock );
1314
+ for (i = 0 ; i <= udev -> dbi_max ; i ++ ) {
1315
+ page = radix_tree_delete (& udev -> data_blocks , i );
1316
+ if (page ) {
1317
+ __free_page (page );
1318
+ atomic_dec (& global_db_count );
1319
+ }
1320
+ }
1321
+ mutex_unlock (& udev -> cmdr_lock );
1322
+ }
1323
+
1296
1324
static void tcmu_dev_kref_release (struct kref * kref )
1297
1325
{
1298
1326
struct tcmu_dev * udev = container_of (kref , struct tcmu_dev , kref );
1299
1327
struct se_device * dev = & udev -> se_dev ;
1328
+ struct tcmu_cmd * cmd ;
1329
+ bool all_expired = true;
1330
+ int i ;
1331
+
1332
+ vfree (udev -> mb_addr );
1333
+ udev -> mb_addr = NULL ;
1334
+
1335
+ /* Upper layer should drain all requests before calling this */
1336
+ spin_lock_irq (& udev -> commands_lock );
1337
+ idr_for_each_entry (& udev -> commands , cmd , i ) {
1338
+ if (tcmu_check_and_free_pending_cmd (cmd ) != 0 )
1339
+ all_expired = false;
1340
+ }
1341
+ idr_destroy (& udev -> commands );
1342
+ spin_unlock_irq (& udev -> commands_lock );
1343
+ WARN_ON (!all_expired );
1344
+
1345
+ tcmu_blocks_release (udev );
1300
1346
1301
1347
call_rcu (& dev -> rcu_head , tcmu_dev_call_rcu );
1302
1348
}
@@ -1495,8 +1541,6 @@ static int tcmu_configure_device(struct se_device *dev)
1495
1541
WARN_ON (udev -> data_size % PAGE_SIZE );
1496
1542
WARN_ON (udev -> data_size % DATA_BLOCK_SIZE );
1497
1543
1498
- INIT_RADIX_TREE (& udev -> data_blocks , GFP_KERNEL );
1499
-
1500
1544
info -> version = __stringify (TCMU_MAILBOX_VERSION );
1501
1545
1502
1546
info -> mem [0 ].name = "tcm-user command & data buffer" ;
@@ -1552,44 +1596,19 @@ static int tcmu_configure_device(struct se_device *dev)
1552
1596
uio_unregister_device (& udev -> uio_info );
1553
1597
err_register :
1554
1598
vfree (udev -> mb_addr );
1599
+ udev -> mb_addr = NULL ;
1555
1600
err_vzalloc :
1556
1601
kfree (info -> name );
1557
1602
info -> name = NULL ;
1558
1603
1559
1604
return ret ;
1560
1605
}
1561
1606
1562
- static int tcmu_check_and_free_pending_cmd (struct tcmu_cmd * cmd )
1563
- {
1564
- if (test_bit (TCMU_CMD_BIT_EXPIRED , & cmd -> flags )) {
1565
- kmem_cache_free (tcmu_cmd_cache , cmd );
1566
- return 0 ;
1567
- }
1568
- return - EINVAL ;
1569
- }
1570
-
1571
1607
static bool tcmu_dev_configured (struct tcmu_dev * udev )
1572
1608
{
1573
1609
return udev -> uio_info .uio_dev ? true : false;
1574
1610
}
1575
1611
1576
- static void tcmu_blocks_release (struct tcmu_dev * udev )
1577
- {
1578
- int i ;
1579
- struct page * page ;
1580
-
1581
- /* Try to release all block pages */
1582
- mutex_lock (& udev -> cmdr_lock );
1583
- for (i = 0 ; i <= udev -> dbi_max ; i ++ ) {
1584
- page = radix_tree_delete (& udev -> data_blocks , i );
1585
- if (page ) {
1586
- __free_page (page );
1587
- atomic_dec (& global_db_count );
1588
- }
1589
- }
1590
- mutex_unlock (& udev -> cmdr_lock );
1591
- }
1592
-
1593
1612
static void tcmu_free_device (struct se_device * dev )
1594
1613
{
1595
1614
struct tcmu_dev * udev = TCMU_DEV (dev );
@@ -1601,30 +1620,13 @@ static void tcmu_free_device(struct se_device *dev)
1601
1620
static void tcmu_destroy_device (struct se_device * dev )
1602
1621
{
1603
1622
struct tcmu_dev * udev = TCMU_DEV (dev );
1604
- struct tcmu_cmd * cmd ;
1605
- bool all_expired = true;
1606
- int i ;
1607
1623
1608
1624
del_timer_sync (& udev -> timeout );
1609
1625
1610
1626
mutex_lock (& root_udev_mutex );
1611
1627
list_del (& udev -> node );
1612
1628
mutex_unlock (& root_udev_mutex );
1613
1629
1614
- vfree (udev -> mb_addr );
1615
-
1616
- /* Upper layer should drain all requests before calling this */
1617
- spin_lock_irq (& udev -> commands_lock );
1618
- idr_for_each_entry (& udev -> commands , cmd , i ) {
1619
- if (tcmu_check_and_free_pending_cmd (cmd ) != 0 )
1620
- all_expired = false;
1621
- }
1622
- idr_destroy (& udev -> commands );
1623
- spin_unlock_irq (& udev -> commands_lock );
1624
- WARN_ON (!all_expired );
1625
-
1626
- tcmu_blocks_release (udev );
1627
-
1628
1630
tcmu_netlink_event (udev , TCMU_CMD_REMOVED_DEVICE , 0 , NULL );
1629
1631
1630
1632
uio_unregister_device (& udev -> uio_info );
0 commit comments