From: sashiko-bot@kernel.org
To: "Mahanta Jambigi" <mjambigi@linux.ibm.com>
Cc: linux-modules@vger.kernel.org, linux-s390@vger.kernel.org,
"Petr Pavlu" <petr.pavlu@suse.com>,
"Heiko Carstens" <hca@linux.ibm.com>,
"Vasily Gorbik" <gor@linux.ibm.com>,
"Alexander Gordeev" <agordeev@linux.ibm.com>,
"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH v2] module: drain external refs before freeing a failed-init module
Date: Tue, 01 Dec 2026 11:05:16 +0000 [thread overview]
Message-ID: <sashiko-outbox-243122@kernel.org> (raw)
In-Reply-To: <20261201103922.1480284-1-mjambigi@linux.ibm.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] module: premature MODULE_STATE_GOING teardown before draining
refs in do_init_module()
- [Medium] module: use-after-free in module_put() calling
trace_module_put() after final put
- [Medium] module: premature module_wq wakeup and idempotent_complete()
in do_init_module()
- [Medium] module: invalid wait context in module_put() waking
spinlock_t module_refs_wq
--
commit 519bf26934925ffafd7a4badb1b58d2efcf6b1ce
Author: Mahanta Jambigi <mjambigi@linux.ibm.com>
module: drain external refs before freeing a failed-init module
When module initialization fails in do_init_module(), this commit drops
MODULE_REF_BASE and waits on module_refs_wq for external module references
to drain before calling free_module(), after waking module_wq and completing
idempotent loads.
Link: https://lore.kernel.org/all/5dc1fe2b-289e-4786-b9e9-e181dda8d4e9@linux.ibm.com/
> diff --git a/kernel/module/main.c b/kernel/module/main.c
> index b405ba5fccb6b..02e789f1aef7d 100644
> --- a/kernel/module/main.c
> +++ b/kernel/module/main.c
> @@ -660,6 +660,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)
> {
> @@ -969,6 +975,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_);
[Severity: Medium]
Can trace_module_put() dereference module after free_module() has already
freed it?
CPU0 (failing loader) CPU1 (external caller)
do_init_module()
...
atomic_sub_return(MODULE_REF_BASE,
&mod->refcnt)
module_put(mod)
atomic_dec_if_positive(&mod->refcnt)
<preempted>
wait_event(module_refs_wq,
atomic_read(&mod->refcnt) <= 0)
free_module(mod)
...
free_mod_mem(mod)
trace_module_put(mod, _RET_IP_)
mod->name <-- use-after-free
Once atomic_dec_if_positive() drops the last reference, do_init_module() can
see mod->refcnt == 0 and run free_module() to completion without waiting for
wake_up_all(). If CPU1 is preempted before entering the tracepoint's RCU
read-side section, synchronize_rcu() in free_module() does not wait for it,
and trace_module_put() reads mod->name and mod->refcnt from unmapped module
memory.
> + /*
> + * 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);
[Severity: Medium]
Does calling wake_up_all(&module_refs_wq) from module_put() violate lock
nesting rules when a reference is dropped under a raw_spinlock_t?
module_refs_wq is a regular wait_queue_head_t protected by a spinlock_t,
which is a sleeping lock on PREEMPT_RT. Callers such as
clockevents_exchange_device() and clockevents_replace() invoke
module_put(dev->owner) while holding raw_spin_lock(&clockevents_lock) with
interrupts disabled (including from __clockevents_unbind() in hardirq IPI
context). If the last reference is dropped in that context after
MODULE_REF_BASE has been subtracted, waking module_refs_wq acquires a
spinlock_t inside a raw_spinlock_t, triggering a lockdep
PROVE_RAW_LOCK_NESTING splat and sleeping in atomic context on PREEMPT_RT.
[ ... ]
> @@ -3234,6 +3251,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);
[Severity: Medium]
Does waking module_wq and completing idempotent requests before
free_module() break concurrent symbol resolution and retries?
At this point mod is still linked on the modules list in MODULE_STATE_GOING
and its sysfs directory is still present:
1. If another module load is waiting in resolve_symbol_wait() for a weak
(STB_WEAK) symbol exported by mod, waking module_wq while mod is in
MODULE_STATE_GOING causes find_symbol() to find mod and ref_module() ->
strong_try_module_get() to return -ENOENT. resolve_symbol_wait() then
returns ERR_PTR(-ENOENT) instead of NULL, which bypasses the weak-symbol
fallback in simplify_symbols() and fails the dependent module load with
-ENOENT.
2. Completing idem unhashes and returns concurrent idempotent finit_module()
callers to userspace before free_module() tears down /sys/module/<mod>
and unlinks mod from modules (even when no external references are held
and wait_event() does not sleep). An immediate retry from userspace is
no longer coalesced by idempotent(), enters
module_patient_check_exists(), finds mod still on the list, and fails
with -EBUSY.
> +
> +#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);
[Severity: High]
Should MODULE_REF_BASE be dropped and external references drained before
running the MODULE_STATE_GOING notifier chain, klp_module_going(), and
ftrace_release_mod()?
In delete_module(), try_stop_module() drops MODULE_REF_BASE and ensures no
external references remain before MODULE_STATE_GOING teardown runs, so
GOING handlers assume no task is executing inside the module anymore. In
do_init_module(), running that teardown while callers holding
try_module_get() are still executing module code breaks several consumers:
- kprobes_module_callback() calls kill_kprobe(), which assumes no thread can
reach the probed function and calls arch_remove_kprobe() to free
p->ainsn.insn and set it to NULL without disarming the breakpoint in
module text:
kernel/kprobes.c:kill_kprobe() {
...
/*
* Here, we can remove insn_slot safely, because no thread calls
* the original probed function (which will be freed soon) any more.
*/
arch_remove_kprobe(p);
...
}
If the active try_module_get() caller hits that kprobe before calling
module_put(), single-stepping jumps to NULL or freed slot memory and
crashes the kernel.
- srcu_module_going() calls cleanup_srcu_struct() and free_percpu(ssp->sda)
on the module's static srcu_struct instances while active module code may
still be in an SRCU read-side critical section, either hitting
WARN_ON(srcu_readers_active(ssp)) and leaking ssp->sda or causing a
per-CPU use-after-free.
- klp_module_going() unpatches the module without a consistency transition
and clears livepatch relocations while tasks are still running patched
module functions, and ftrace_release_mod() frees the module's ftrace
records while functions remain active.
In addition, keeping MODULE_REF_BASE alive across synchronize_rcu() and the
GOING teardown means mod->refcnt stays at least 1 even when no external
references were held when mod->init() failed, so a non-RCU try_module_get()
caller that passed module_is_live(mod) just before mod->state became
MODULE_STATE_GOING can still succeed in atomic_inc_not_zero(&mod->refcnt)
after MODULE_STATE_GOING teardown has already run.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201103922.1480284-1-mjambigi@linux.ibm.com?part=1
prev parent reply other threads:[~2026-12-01 11:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-243122@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=agordeev@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=linux-modules@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mjambigi@linux.ibm.com \
--cc=petr.pavlu@suse.com \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.