[PATCH 2/3] usb: gadget: f_tcm: drop dependent-module support

From: Slawomir Stepien

Date: Thu Dec 03 2026 - 03:15:44 EST


The dependent module was only used by the legacy g_target driver, which
had no configfs item that a TPG could depend on. Instead, f_tcm pinned
the g_target module with try_module_get() while a TPG existed, so the
function instance and the attach/detach callbacks it provided could not
go away underneath the TPG.

With g_target gone, every function instance is a configfs item, and the
TPG keeps it alive with configfs_depend_item_unlocked(). Remove
has_dep, dependent and the try_module_get()/module_put() calls.

usbg_drop_tpg() took dep_lock only to read has_dep, as
configfs_undepend_item_unlocked() just takes configfs_dirent_lock, so
drop dep_lock there. Update the kerneldoc of dep_lock and ready, which
still described the dependent module.

Signed-off-by: Slawomir Stepien <sst@xxxxxxxxx>
---
drivers/usb/gadget/function/f_tcm.c | 40 +++++++++--------------------
drivers/usb/gadget/function/u_tcm.h | 10 ++------
2 files changed, 14 insertions(+), 36 deletions(-)

diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 469939a3329c..8f405f0a5cbd 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -1678,23 +1678,16 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
if (!READ_ONCE(opts->ready))
goto unlock_inst;

- if (opts->has_dep) {
- if (!try_module_get(opts->dependent))
- goto unlock_inst;
- } else {
- /*
- * configfs_depend_item_unlocked() may acquire the configfs
- * root inode lock when the target belongs to a different
- * subsystem. Calling it under dep_lock would create a
- * circular dependency:
- * dep_lock -> configfs inode lock -> su_mutex -> dep_lock
- */
- ret = configfs_depend_item_unlocked(
- wwn->wwn_group.cg_subsys,
- &opts->func_inst.group.cg_item);
- if (ret)
- goto unlock_inst;
- }
+ /*
+ * configfs_depend_item_unlocked() may acquire the configfs root inode
+ * lock when the target belongs to a different subsystem. Calling it
+ * under dep_lock would create a circular dependency:
+ * dep_lock -> configfs inode lock -> su_mutex -> dep_lock
+ */
+ ret = configfs_depend_item_unlocked(wwn->wwn_group.cg_subsys,
+ &opts->func_inst.group.cg_item);
+ if (ret)
+ goto unlock_inst;

tpg = kzalloc_obj(struct usbg_tpg);
ret = -ENOMEM;
@@ -1729,10 +1722,7 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
free_tpg:
kfree(tpg);
unref_dep:
- if (opts->has_dep)
- module_put(opts->dependent);
- else
- configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
+ configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
unlock_inst:
mutex_unlock(&tpg_instances_lock);

@@ -1765,13 +1755,7 @@ static void usbg_drop_tpg(struct se_portal_group *se_tpg)
opts = container_of(tpg_instances[i].func_inst,
struct f_tcm_opts, func_inst);
opts->tpg = NULL;
- mutex_lock(&opts->dep_lock);
- if (opts->has_dep)
- module_put(opts->dependent);
- else
- configfs_undepend_item_unlocked(
- &opts->func_inst.group.cg_item);
- mutex_unlock(&opts->dep_lock);
+ configfs_undepend_item_unlocked(&opts->func_inst.group.cg_item);
}
mutex_unlock(&tpg_instances_lock);

diff --git a/drivers/usb/gadget/function/u_tcm.h b/drivers/usb/gadget/function/u_tcm.h
index 68ce03d3fb6f..93d3deb9a408 100644
--- a/drivers/usb/gadget/function/u_tcm.h
+++ b/drivers/usb/gadget/function/u_tcm.h
@@ -18,13 +18,9 @@
struct usbg_tpg;

/**
- * @dependent: optional dependent module. Meant for legacy gadget.
- * If non-null its refcount will be increased when a tpg is created and
- * decreased when tpg is dropped.
- * @dep_lock: lock for dependent module operations.
- * @ready: true if the dependent module information is set.
+ * @dep_lock: protects @can_attach.
+ * @ready: true once the function instance has been given a name.
* @can_attach: true a function can be bound to gadget
- * @has_dep: true if there is a dependent module
* @tpg: target portal group associated with this function instance. Set
* when the TPG is created, before it can be enabled, and cleared after it
* has been detached, so it never changes while the function is bound.
@@ -33,11 +29,9 @@ struct usbg_tpg;
struct f_tcm_opts {
struct usb_function_instance func_inst;
struct usbg_tpg *tpg;
- struct module *dependent;
struct mutex dep_lock;
bool ready;
bool can_attach;
- bool has_dep;

/*
* Callbacks to be removed when legacy tcm gadget disappears.
--
2.56.1