Skip to content

Commit 85aa61f

Browse files
shuangpeng-kernelgregkh
authored andcommitted
usb: gadget: f_tcm: keep port count until LUN teardown completes
commit c39d091 upstream. tcm_usbg_drop_nexus() permits session removal once tpg_port_count reaches zero. However, usbg_port_unlink() currently decrements that count from the fabric_pre_unlink() callback, before core_dev_del_lun() waits for active se_lun references to drain. If removal of the last LUN races a nexus removal, the latter can observe a zero port count and call target_remove_session(). This frees sess_cmd_map while an in-flight struct usbg_cmd, including its work item, can still be accessed. Overlapping the last-LUN unlink with nexus removal reproduces this lifetime violation as a DEBUG_OBJECTS "free active" warning for usbg_cmd_work, followed by a target-core BUG/Oops. The generic target-core unlink path has no callback after core_dev_del_lun() completes. Add an optional fabric_post_unlink() callback and use it for the f_tcm port count. The count now remains nonzero until core_dev_del_lun() has finished draining active LUN references, preventing nexus removal from freeing the session during command completion. Fixes: c52661d ("usb-gadget: Initial merge of target module for UASP + BOT") Cc: stable@vger.kernel.org Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com> Link: https://patch.msgid.link/20260807060733.3186624-1-shuangpeng.kernel@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3f6face commit 85aa61f

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

drivers/target/target_core_fabric_configfs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,14 @@ static void target_fabric_port_unlink(
690690
}
691691

692692
core_dev_del_lun(se_tpg, lun);
693+
694+
if (tf->tf_ops->fabric_post_unlink) {
695+
/*
696+
* Allow fabrics to release state that must remain valid until
697+
* core_dev_del_lun() has drained all active LUN references.
698+
*/
699+
tf->tf_ops->fabric_post_unlink(se_tpg, lun);
700+
}
693701
}
694702

695703
static void target_fabric_port_release(struct config_item *item)

drivers/usb/gadget/function/f_tcm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ static const struct target_core_fabric_ops usbg_ops = {
20232023
.fabric_enable_tpg = usbg_enable_tpg,
20242024
.fabric_drop_tpg = usbg_drop_tpg,
20252025
.fabric_post_link = usbg_port_link,
2026-
.fabric_pre_unlink = usbg_port_unlink,
2026+
.fabric_post_unlink = usbg_port_unlink,
20272027
.fabric_init_nodeacl = usbg_init_nodeacl,
20282028

20292029
.tfc_wwn_attrs = usbg_wwn_attrs,

include/target/target_core_fabric.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ struct target_core_fabric_ops {
9595
struct se_lun *);
9696
void (*fabric_pre_unlink)(struct se_portal_group *,
9797
struct se_lun *);
98+
void (*fabric_post_unlink)(struct se_portal_group *se_tpg,
99+
struct se_lun *lun);
98100
struct se_tpg_np *(*fabric_make_np)(struct se_portal_group *,
99101
struct config_group *, const char *);
100102
void (*fabric_drop_np)(struct se_tpg_np *);

0 commit comments

Comments
 (0)