Linux Modules
 help / color / mirror / Atom feed
* [PATCH v2] module: drain external refs before freeing a failed-init module
@ 2026-12-01 10:39 Mahanta Jambigi
  2026-12-01 11:05 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Mahanta Jambigi @ 2026-12-01 10:39 UTC (permalink / raw)
  To: mcgrof, petr.pavlu, da.gomez, samitolvanen, atomlin
  Cc: linux-modules, linux-kernel, linux-s390, pasic, wintera,
	Mahanta Jambigi

When a module's __init makes it externally reachable and a later __init
step fails, do_init_module() frees the module while callers that entered
through that interface and called try_module_get() may still be running
module code. The normal unload path in try_stop_module() refuses to
proceed while references are held; the init-failure path has no such
check.

Drop the base reference so no new reference can be taken, then wait for
the remaining ones to be put before calling free_module(). Duplicate and
idempotent loads of the failed module are released first, so they are
not blocked behind the wait.

The wait is not interruptible and has no timeout. A reference that is
never put keeps the module in MODULE_STATE_GOING and the loading task
waiting, which the hung task detector reports.

Signed-off-by: Mahanta Jambigi <mjambigi@linux.ibm.com>
---
Link: https://lore.kernel.org/all/5dc1fe2b-289e-4786-b9e9-e181dda8d4e9@linux.ibm.com/
      [RFC] module: init-failure path can free a module with live
      try_module_get() users

Changes in v2:
- Use a global wait queue instead of a completion in struct module. The
  new wake-up path in module_put() touches no module memory, so it
  cannot race with free_module() reached through delete_module().
- Drop the base reference and wait on the raw count. try_module_get()
  then fails once the count reaches zero, so a racing getter cannot
  resurrect the module, and module_put() wakes only on the last
  reference. module_refcount() reports one less than the held
  references until the module is freed.
- Release duplicate and idempotent loads before the wait, and keep the
  trailing module_wq wake-up so a loader that finds the module UNFORMED
  during free_module() is not left asleep.
- Make idempotent_complete() a no-op on the second call for the same
  request, so entries from a newer finit_module() during the wait are
  left alone.
- Run the wait after the GOING notifier chain, so a consumer that a
  notifier tears down drops its reference before the loader blocks on
  it.
- A load of the same name that starts during the wait now fails with
  -EBUSY instead of blocking; callers retry it.
- No change to ref_module(): strong_try_module_get() already refuses
  MODULE_STATE_COMING, so a failed init cannot be pinned through a
  module dependency.
- Without CONFIG_MODULE_UNLOAD there are no reference counts and nothing
  to drain; the wait is compiled out. The wait is not interruptible,
  since after the base reference is gone nothing else can free the
  module.

 kernel/module/main.c |   64 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 57 insertions(+), 7 deletions(-)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 34e0204ea37c..b3c301790d5d 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -659,6 +659,12 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
 /* MODULE_REF_BASE is the base reference count by kmodule loader. */
 #define MODULE_REF_BASE	1
 
+/*
+ * Wait queue for a failed init that is draining the references taken
+ * by other tasks before the module can be freed.
+ */
+static DECLARE_WAIT_QUEUE_HEAD(module_refs_wq);
+
 /* Init the unload section of the module. */
 static int module_unload_init(struct module *mod)
 {
@@ -968,6 +974,13 @@ void module_put(struct module *module)
 		ret = atomic_dec_if_positive(&module->refcnt);
 		WARN_ON(ret < 0);	/* Failed to put refcount */
 		trace_module_put(module, _RET_IP_);
+		/*
+		 * Zero is only reached once the base reference has been
+		 * dropped by a failed init, which then waits for the last
+		 * reference to go.
+		 */
+		if (!ret)
+			wake_up_all(&module_refs_wq);
 	}
 }
 EXPORT_SYMBOL(module_put);
@@ -3095,13 +3108,17 @@ void flush_module_init_free_work(void)
 static bool async_probe;
 module_param(async_probe, bool, 0644);
 
+struct idempotent;
+
+static int idempotent_complete(struct idempotent *u, int ret);
+
 /*
  * This is where the real work happens.
  *
  * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
  * helper command 'lx-symbols'.
  */
-static noinline int do_init_module(struct module *mod)
+static noinline int do_init_module(struct module *mod, struct idempotent *idem)
 {
 	int ret = 0;
 	struct mod_initfree *freeinit;
@@ -3233,6 +3250,28 @@ static noinline int do_init_module(struct module *mod)
 				     MODULE_STATE_GOING, mod);
 	klp_module_going(mod);
 	ftrace_release_mod(mod);
+
+	/*
+	 * Release duplicate and idempotent loads before waiting below, so
+	 * a failed init delays only the task that ran it.
+	 */
+	wake_up_all(&module_wq);
+	if (idem)
+		idempotent_complete(idem, ret);
+
+#ifdef CONFIG_MODULE_UNLOAD
+	/*
+	 * Dropping the base reference gates new references: once the count
+	 * reaches zero, try_module_get() fails. Wait for the remaining
+	 * references to go before the module memory is freed. Until then
+	 * module_refcount() reports one less than the references held.
+	 *
+	 * A negative count means the module dropped a reference it never
+	 * took; puts are no-ops on it, so waiting for it would never end.
+	 */
+	if (atomic_sub_return(MODULE_REF_BASE, &mod->refcnt) > 0)
+		wait_event(module_refs_wq, atomic_read(&mod->refcnt) <= 0);
+#endif
 	free_module(mod);
 	wake_up_all(&module_wq);
 
@@ -3450,7 +3489,7 @@ static int early_mod_check(struct load_info *info, int flags)
  * zero, and we rely on this for optional sections.
  */
 static int load_module(struct load_info *info, const char __user *uargs,
-		       int flags)
+		       int flags, struct idempotent *idem)
 {
 	struct module *mod;
 	bool module_allocated = false;
@@ -3610,7 +3649,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	/* Done! */
 	trace_module_load(mod);
 
-	return do_init_module(mod);
+	return do_init_module(mod, idem);
 
  livepatch_cleanup:
 	if (is_livepatch_module(mod))
@@ -3687,7 +3726,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
 		return err;
 	}
 
-	return load_module(&info, uargs, 0);
+	return load_module(&info, uargs, 0, NULL);
 }
 
 struct idempotent {
@@ -3695,6 +3734,7 @@ struct idempotent {
 	struct hlist_node entry;
 	struct completion complete;
 	int ret;
+	bool completed;
 };
 
 #define IDEM_HASH_BITS 8
@@ -3710,6 +3750,7 @@ static bool idempotent(struct idempotent *u, const void *cookie)
 
 	u->ret = -EINTR;
 	u->cookie = cookie;
+	u->completed = false;
 	init_completion(&u->complete);
 
 	spin_lock(&idem_lock);
@@ -3731,6 +3772,9 @@ static bool idempotent(struct idempotent *u, const void *cookie)
  * up completing the operation. We now need to walk the list,
  * remove everybody - which includes ourselves - fill in the return
  * value, and then complete the operation.
+ *
+ * Only the first call for a request walks the list, so entries of a
+ * newer request for the same file are left alone.
  */
 static int idempotent_complete(struct idempotent *u, int ret)
 {
@@ -3741,6 +3785,10 @@ static int idempotent_complete(struct idempotent *u, int ret)
 	struct idempotent *pos;
 
 	spin_lock(&idem_lock);
+	if (u->completed)
+		goto out;
+	u->completed = true;
+
 	hlist_for_each_entry_safe(pos, next, head, entry) {
 		if (pos->cookie != cookie)
 			continue;
@@ -3748,6 +3796,7 @@ static int idempotent_complete(struct idempotent *u, int ret)
 		pos->ret = ret;
 		complete(&pos->complete);
 	}
+out:
 	spin_unlock(&idem_lock);
 	return ret;
 }
@@ -3774,7 +3823,8 @@ static int idempotent_wait_for_completion(struct idempotent *u)
 	return u->ret;
 }
 
-static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
+static int init_module_from_file(struct file *f, const char __user *uargs, int flags,
+				 struct idempotent *idem)
 {
 	bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
 	struct load_info info = { };
@@ -3810,7 +3860,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 		info.len = len;
 	}
 
-	return load_module(&info, uargs, flags);
+	return load_module(&info, uargs, flags, idem);
 }
 
 static int idempotent_init_module(struct file *f, const char __user * uargs, int flags)
@@ -3822,7 +3872,7 @@ static int idempotent_init_module(struct file *f, const char __user * uargs, int
 
 	/* Are we the winners of the race and get to do this? */
 	if (!idempotent(&idem, file_inode(f))) {
-		int ret = init_module_from_file(f, uargs, flags);
+		int ret = init_module_from_file(f, uargs, flags, &idem);
 		return idempotent_complete(&idem, ret);
 	}
 

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 11:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 10:39 [PATCH v2] module: drain external refs before freeing a failed-init module Mahanta Jambigi
2026-12-01 11:05 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox