From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796123118; b=dHF0d2o1zcuur/ixwPbsbHPKYFHSbRzDcOqhTDjjbhkutHqntMrcDLOxINUpPK1OOg9/ 6ihgqf4dB6VGs/VJqe+aBYa/9zRaaCk+NFWvLREpx3/FWNuarA9Mu3ni86DpR+f9G59oL tK0cQcZrc4xzCO3MDNd5AziZ2Wqy55WGb5WY8RPwuEsm75pSJuAYqf1fShEwFWiUW9KFd Lvlhfn4rRisMARx2QTed84nYOVY6lwhkafhuNAW0vGUksB43FtwQBnlZhaEzamLF+ygKT pgkK5YKOTpATIIOWlYNW31ZZNZyBiB1+c/GH91joXyFjH6+6woxjpjG/nsjriIx56bw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796123118; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=e/MsVkHXlPD6wJ6b07jv11V4Y1MxBw2qy2EwRb4fU/Q=; b=QY17M3snfEarv31dGu6YjqIkFLbcuE81C54wIRQ+HkC+JYTfzKeOMRRC5N5vCYuWD7Hk yCai1f/lzoGdQJABH5w8TSVrUWr5k36phszFGpzF4eB4lyjTqv9wK7vUq5v7BY0V5wikf ykNSEONjS0tPP/z3qn53G1UWzc8WECcNOj4S19g9rdDUbmYOUdhMAo6+4Tv5YVpljTbNc JKTDRtzDN1XUhRCqurNlBpmnUZtljHVINpAgpZXSwL/m8njdztQrIAE/nT5UQUM/Ui/Ok /09wQPQfnOLxAIznSUkX6DpvqIYv5ZofYHv5vofJJIrpqlf7/dj/UYkJgazXfUGIY1w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lN1JKzC3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D6F8606A31A; Tue, 1 Dec 2026 11:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lN1JKzC3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F29F1F0089C; Tue, 1 Dec 2026 11:05:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123117; bh=e/MsVkHXlPD6wJ6b07jv11V4Y1MxBw2qy2EwRb4fU/Q=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=lN1JKzC3yvYpvBB8I+rXVqjIMNRTJ59KIDIwRHXsh9GSFiG10Sd6eZbLS7eH+0AY0 EJqrJRi5n5ksAZBgK2phUi1zXv3VUK5PmKlYLp2rD8889cwlEnhS+QsvY8D0EV7XNx pkDMR7a7jXY4j4oTrdChphCkeTOYTUZvh1+qkInq1p7GWUz46PWhbFEPW5tjieG5FV 7N+/+rFuxl95GGkmShqt4etigEtxMeIKhiLg57+X3zAAixnq6HCqFO/6SCoTrERBkj Ap7no2kIxuclBJIUanfrkZRtqItAD5gdvAW5L/tFstgpczUxWNHy8d5lAWH0gZJBGC WxnXLHUNHMrdw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] module: drain external refs before freeing a failed-init module Reply-To: sashiko-reviews@lists.linux.dev To: "Mahanta Jambigi" Cc: linux-modules@vger.kernel.org, linux-s390@vger.kernel.org, "Petr Pavlu" , "Heiko Carstens" , "Vasily Gorbik" , "Alexander Gordeev" , "Christian Borntraeger" In-Reply-To: <20261201103922.1480284-1-mjambigi@linux.ibm.com> References: <20261201103922.1480284-1-mjambigi@linux.ibm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 11:05:16 +0000 X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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@linu= x.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 > =20 > +/* > + * 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 =3D 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) wait_event(module_refs_wq, atomic_read(&mod->refcnt) <=3D 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 =3D=3D 0 and run free_module() to completion without waitin= g 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/ 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) <=3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201103922.1480= 284-1-mjambigi@linux.ibm.com?part=3D1