scsi: fix refcount leaking for "tagset_refcnt"
PATCH 1/1 scsi: fix refcount leaking for "tagset_refcnt"
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
[130120.652718] scsi_alloc_sdev: Allocation failure during SCSI scanning, some SCSI devices might not be configured
PID: 2528 TASK: ffff9d0408974e00 CPU: 3 COMMAND: "iscsid"
#0 [ffffb5b9c134b9e0] __schedule at ffffffff860657d4
#1 [ffffb5b9c134ba28] schedule at ffffffff86065c6f
#2 [ffffb5b9c134ba40] schedule_timeout at ffffffff86069fb0
#3 [ffffb5b9c134bab0] __wait_for_common at ffffffff8606674f
#4 [ffffb5b9c134bb10] scsi_remove_host at ffffffff85bfe84b
#5 [ffffb5b9c134bb30] iscsi_sw_tcp_session_destroy at ffffffffc03031c4 [iscsi_tcp] #6 [ffffb5b9c134bb48] iscsi_if_recv_msg at ffffffffc0292692 [scsi_transport_iscsi] #7 [ffffb5b9c134bb98] iscsi_if_rx at ffffffffc02929c2 [scsi_transport_iscsi] #8 [ffffb5b9c134bbf0] netlink_unicast at ffffffff85e551d6
#9 [ffffb5b9c134bc38] netlink_sendmsg at ffffffff85e554ef
--- drivers/scsi/scsi_scan.c | 1 + 1 file changed, 1 insertion(+)
drivers/scsi/scsi_scan.c +1 -0
@@ -361,6 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,* since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map (sdev, depth)) { +kref_put (&sdev->host->tagset_refcnt, scsi_mq_free_tags);put_device (&starget->dev);kfree (sdev); goto out;
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 30 lines · Junxiao Bi
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
[130120.652718] scsi_alloc_sdev: Allocation failure during SCSI scanning, some SCSI devices might not be configuredPID: 2528 TASK:
ffff9d0408974e00CPU: 3 COMMAND: "iscsid"
#0 [ffffb5b9c134b9e0]__scheduleatffffffff860657d4
#1 [ffffb5b9c134ba28] schedule atffffffff86065c6f
#2 [ffffb5b9c134ba40]schedule_timeoutatffffffff86069fb0
#3 [ffffb5b9c134bab0]__wait_for_commonatffffffff8606674f
#4 [ffffb5b9c134bb10]scsi_remove_hostatffffffff85bfe84b
#5 [ffffb5b9c134bb30]iscsi_sw_tcp_session_destroyatffffffffc03031c4[iscsi_tcp] #6 [ffffb5b9c134bb48]iscsi_if_recv_msgatffffffffc0292692[scsi_transport_iscsi] #7 [ffffb5b9c134bb98]iscsi_if_rxatffffffffc02929c2[scsi_transport_iscsi] #8 [ffffb5b9c134bbf0]netlink_unicastatffffffff85e551d6
#9 [ffffb5b9c134bc38]netlink_sendmsgatffffffff85e554efFixes 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")Signed-off-by Junxiao Bi <[email protected]>--- drivers/scsi/scsi_scan.c | 1 + 1 file changed, 1 insertion(+)drivers/scsi/scsi_scan.c +0
-0
drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 367
Quoted 7 lines
drivers/scsi/scsi_scan.c +1
-0@@ -361,6 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,* since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map (sdev, depth)) { +kref_put (&sdev->host->tagset_refcnt, scsi_mq_free_tags);put_device (&starget->dev);kfree (sdev); goto out;
drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 367
Quoted 12 lines · Junxiao Bi
drivers/scsi/scsi_scan.c +1
-0@@ -361,6 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,* since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map (sdev, depth)) { +kref_put (&sdev->host->tagset_refcnt, scsi_mq_free_tags);put_device (&starget->dev);kfree (sdev); goto out;
Martin K. Petersen
AppliedJunxiao,
Quoted 3 lines
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
Applied to 7.0/scsi-fixes, thanks!
Signature · 1 line
Martin K. Petersen
Martin K. Petersen
AppliedQuoted 18 lines · Junxiao Bi
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
[130120.652718] scsi_alloc_sdev: Allocation failure during SCSI scanning, some SCSI devices might not be configuredPID: 2528 TASK:
ffff9d0408974e00CPU: 3 COMMAND: "iscsid"
#0 [ffffb5b9c134b9e0]__scheduleatffffffff860657d4
#1 [ffffb5b9c134ba28] schedule atffffffff86065c6f
#2 [ffffb5b9c134ba40]schedule_timeoutatffffffff86069fb0
#3 [ffffb5b9c134bab0]__wait_for_commonatffffffff8606674f
#4 [ffffb5b9c134bb10]scsi_remove_hostatffffffff85bfe84b
#5 [ffffb5b9c134bb30]iscsi_sw_tcp_session_destroyatffffffffc03031c4[iscsi_tcp] #6 [ffffb5b9c134bb48]iscsi_if_recv_msgatffffffffc0292692[scsi_transport_iscsi] #7 [ffffb5b9c134bb98]iscsi_if_rxatffffffffc02929c2[scsi_transport_iscsi] #8 [ffffb5b9c134bbf0]netlink_unicastatffffffff85e551d6
#9 [ffffb5b9c134bc38]netlink_sendmsgatffffffff85e554ef
Quoted 1 line
[...]
Applied to 7.0/scsi-fixes, thanks!
[1/1] scsi: fix refcount leaking for "tagset_refcnt"
https://git.kernel.org/mkp/scsi/c/1ac22c8eae81
Signature · 1 line
Martin K. Petersen
Quoted 30 lines · Junxiao Bi
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
[130120.652718] scsi_alloc_sdev: Allocation failure during SCSI scanning, some SCSI devices might not be configuredPID: 2528 TASK:
ffff9d0408974e00CPU: 3 COMMAND: "iscsid"
#0 [ffffb5b9c134b9e0]__scheduleatffffffff860657d4
#1 [ffffb5b9c134ba28] schedule atffffffff86065c6f
#2 [ffffb5b9c134ba40]schedule_timeoutatffffffff86069fb0
#3 [ffffb5b9c134bab0]__wait_for_commonatffffffff8606674f
#4 [ffffb5b9c134bb10]scsi_remove_hostatffffffff85bfe84b
#5 [ffffb5b9c134bb30]iscsi_sw_tcp_session_destroyatffffffffc03031c4[iscsi_tcp] #6 [ffffb5b9c134bb48]iscsi_if_recv_msgatffffffffc0292692[scsi_transport_iscsi] #7 [ffffb5b9c134bb98]iscsi_if_rxatffffffffc02929c2[scsi_transport_iscsi] #8 [ffffb5b9c134bbf0]netlink_unicastatffffffff85e551d6
#9 [ffffb5b9c134bc38]netlink_sendmsgatffffffff85e554efFixes 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")Signed-off-by Junxiao Bi <[email protected]>--- drivers/scsi/scsi_scan.c | 1 + 1 file changed, 1 insertion(+)drivers/scsi/scsi_scan.c +0
-0
drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, · line 363
Quoted 3 lines
drivers/scsi/scsi_scan.c +0
-0@@ -361,6 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,* since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map (sdev, depth)) {
At this point scsi_sysfs_device_initialize() has been called. Then if you check the comment in __scsi_remove_device():
Paired with kref_get() in scsi_sysfs_device_initialize()*
So I wonder why we don't call __scsi_remove_device() instead, which calls scsi_target_reap().
Indeed, the current error handling in scsi_alloc_sdev() is odd - we only call __scsi_remove_device() for ->sdev_init() failure, but nothing happens between calling ->sdev_init() and after scsi_sysfs_device_initialize() which means that at this point we should only now call __scsi_remove_device().
* I think that should be scsi_sysfs_initialize() and has always been incorrect
Quoted 4 lines
drivers/scsi/scsi_scan.c +1
-0+kref_put (&sdev->host->tagset_refcnt, scsi_mq_free_tags);put_device (&starget->dev);kfree (sdev); goto out;
Quoted 38 lines · John Garry
Quoted 30 lines · Junxiao Bi
This leaking will cause hung when tearing down the scsi host. This is an example with iscsi, iscsid hung with the following call trace after this kernel log.
[130120.652718] scsi_alloc_sdev: Allocation failure during SCSI scanning, some SCSI devices might not be configuredPID: 2528 TASK: ffff9d0408974e00 CPU: 3 COMMAND: "iscsid"
#0 [ffffb5b9c134b9e0]__scheduleatffffffff860657d4
#1 [ffffb5b9c134ba28] schedule atffffffff86065c6f
#2 [ffffb5b9c134ba40]schedule_timeoutatffffffff86069fb0
#3 [ffffb5b9c134bab0]__wait_for_commonatffffffff8606674f
#4 [ffffb5b9c134bb10]scsi_remove_hostatffffffff85bfe84b
#5 [ffffb5b9c134bb30]iscsi_sw_tcp_session_destroyatffffffffc03031c4[iscsi_tcp] #6 [ffffb5b9c134bb48]iscsi_if_recv_msgatffffffffc0292692[scsi_transport_iscsi] #7 [ffffb5b9c134bb98]iscsi_if_rxatffffffffc02929c2[scsi_transport_iscsi] #8 [ffffb5b9c134bbf0]netlink_unicastatffffffff85e551d6
#9 [ffffb5b9c134bc38]netlink_sendmsgatffffffff85e554efFixes 8fe4ce5836e9 ("scsi: core: Fix a use-after-free")Signed-off-by Junxiao Bi <[email protected]>---
drivers/scsi/scsi_scan.c| 1 +1 file changed, 1 insertion(+)drivers/scsi/scsi_scan.c +0
-0drivers/scsi/scsi_scan.c static struct scsi_device *scsi_alloc_sdev(struct · line 364
Quoted 4 lines
drivers/scsi/scsi_scan.c +0
-0@@ -361,6 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(structscsi_target *starget, * since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map(sdev, depth)) {At this point
scsi_sysfs_device_initialize()has been called. Then if you check the comment in__scsi_remove_device():
Quoted 7 lines
Paired with
kref_get()in scsi_sysfs_device_initialize()*So I wonder why we don't call
__scsi_remove_device()instead, which callsscsi_target_reap().Indeed, the current error handling in
scsi_alloc_sdev()is odd - we only call__scsi_remove_device()for ->sdev_init() failure, but nothing happens between calling ->sdev_init() and afterscsi_sysfs_device_initialize()which means that at this point we should only now call__scsi_remove_device().* I think that should be
scsi_sysfs_initialize()and has always been incorrect
Good catch. Thanks John. I will send a v2 with this:
drivers/scsi/scsi_scan.c +0 -0
@@ -361,9 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
* since we use this queue depth most of times. */
if (scsi_realloc_sdev_budget_map(sdev, depth)) { - put_device(&starget->dev);
- kfree(sdev);
- goto out;
+ goto out_device_destroy;
}
scsi_change_queue_depth(sdev, depth);
Thanks,
Junxiao.
Quoted 7 lines
Quoted 6 lines
drivers/scsi/scsi_scan.c +1
-0+kref_put (&sdev->host->tagset_refcnt, scsi_mq_free_tags); put_device(&starget->dev); kfree(sdev);goto out;
Quoted 27 lines · On 02/03/2026 20:36, [email protected] wrote:
Quoted 9 lines
At this point
scsi_sysfs_device_initialize()has been called. Then if you check the comment in__scsi_remove_device():Paired with
kref_get()in scsi_sysfs_device_initialize()*So I wonder why we don't call
__scsi_remove_device()instead, which callsscsi_target_reap().Indeed, the current error handling in
scsi_alloc_sdev()is odd - we only call__scsi_remove_device()for ->sdev_init() failure, but nothing happens between calling ->sdev_init() and afterscsi_sysfs_device_initialize()which means that at this point we should only now call__scsi_remove_device().* I think that should be
scsi_sysfs_initialize()and has always been incorrectGood catch. Thanks John. I will send a v2 with this:
drivers/scsi/scsi_scan.c +1
-3@@ -361,9 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(structscsi_target *starget, * since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map(sdev, depth)) { - put_device(&starget->dev); - kfree(sdev); - goto out; + goto out_device_destroy; }
Quoted 1 line
scsi_change_queue_depth(sdev, depth);
NP and sorry the late review. I think that Martin has already queued your v1 in his fixes branch...
Quoted 28 lines · John Garry
Quoted 27 lines · On 02/03/2026 20:36, [email protected] wrote:
Quoted 9 lines
At this point
scsi_sysfs_device_initialize()has been called. Then if you check the comment in__scsi_remove_device():Paired with
kref_get()in scsi_sysfs_device_initialize()*So I wonder why we don't call
__scsi_remove_device()instead, which callsscsi_target_reap().Indeed, the current error handling in
scsi_alloc_sdev()is odd - we only call__scsi_remove_device()for ->sdev_init() failure, but nothing happens between calling ->sdev_init() and afterscsi_sysfs_device_initialize()which means that at this point we should only now call__scsi_remove_device().* I think that should be
scsi_sysfs_initialize()and has always been incorrectGood catch. Thanks John. I will send a v2 with this:
drivers/scsi/scsi_scan.c +1
-3@@ -361,9 +361,7 @@ static struct scsi_device *scsi_alloc_sdev(structscsi_target *starget, * since we use this queue depth most of times. */ if (scsi_realloc_sdev_budget_map(sdev, depth)) { - put_device(&starget->dev); - kfree(sdev); - goto out; + goto out_device_destroy; }
Quoted 3 lines
Quoted 1 line
scsi_change_queue_depth(sdev, depth);NP and sorry the late review. I think that Martin has already queued your v1 in his fixes branch...
I see. Then i will post a new patch to fix the sysfs reference leak.
Thanks,
Junxiao.