From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796121607; b=PTVrP/+xPEhVGP5PF5AiMuwwSlSgnyrroXYg2DT1Pt6flZu4EOUkEt27XGNVKjDYEvz+ gCCNKar2foa2zrnYi89G6IzlSO/fhqM7Voap3iH+sGJf45wqdJeHn0zI65fMMeSQQNmBh HlYUxA3noRcPGzt0VEzfpLsOeH9YQXVP3biBIaGBfMk1ZSDZJloxmsMOpJHaATobPI+3e osOqUEGU4AGqVD0llJxwz1m9BeIcxns6oQG4XCXfLsYwLSbuanw51HyrVdeqebmPyZGsD +gEqqAabuNsrbJT/eTEVyzNPX+WtIB0BtHnLj8xVAO9OYHDkwebigedUhN/pjpvtgbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796121607; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:Received:Received:Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-TM-AS-GCONF:X-Proofpoint-Reinject:X-Proofpoint-Spam-Info: X-Proofpoint-ORIG-GUID:X-Proofpoint-GUID:X-Authority-Analysis: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=fzp7MkRjqoN4KhxfAbhCPXOdgO8Zr4Fs9SB4tvjX6qQ=; b=aoPwGPoghejID3afd+q6fAniJKg5WcHyYtKcc5RPos8jVagZZgKxX+OUPG7sR49W5sy6 N9qDNuT6ejbrf2CcFCq5HmH5bKApYgveyxPJ2w0x4vIIU4+xt2DCPw8Yxxky8ajtWIytn mlzKiBAhR3L3wrPqwXU100+j0miv5nf+OpjfzcRhAf4c+dj+adZniJG5gs8WGaGOPTQew fd4gLWA3WFzeP+f1VtN54qNTg/J2ToGAx8OlTYwPghJ09f6wuDcY3Qsp2B78fLscsJBSV nZNn8OO4mvbjEdlc0vJjkoOzYPrh+5/lNPf+LbsEMOsISX38FDSnx6rgghZVqvVveuA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=YqegDem7; arc=none smtp.remote-ip=148.163.156.1 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37C64606A320; Tue, 1 Dec 2026 10:40:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 37C64606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=YqegDem7 Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1AZ1h61729171; Tue, 1 Dec 2026 10:39:59 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:message-id:mime-version :subject:to; s=pp1; bh=fzp7MkRjqoN4KhxfAbhCPXOdgO8Zr4Fs9SB4tvjX6 qQ=; b=YqegDem7elr/kX/mHWO1qgEiT/pvTEcNaRXQcpP0t1igx/aXlKNvXbuJG 0U9WqRYEWzdI6uEtpCx2+3Uj7asovdlKDsAPJJQqTckFVXetleXmsi4vEneBLXLh K34Cfxd0X+5CgVEVr4xbQ7Wtw3TZVv5Zy9Ltym90x4AsHrWEmvu6S+6ytiaVSEcT n0q6Gtp8bcc9DXawpU3PqicVt57VVjlgS/yXU3eg7KfFM+1GgBuP/Mgv66HI4s7p +RvB5GjRvZAOiWx9b8tKQbF3vW0eIAm04uvqWMphviIEFlZeJk/KjHhr9zrzKXZ7 byKW2NVZe2+Kb5sNm4x7i8PCmLm9A== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4j7pf3v9n7-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 10:39:58 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1AYJ7p1470528; Tue, 1 Dec 2026 10:39:57 GMT Received: from smtprelay02.dal12v.mail.ibm.com ([172.16.1.4]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4j9v5w0334-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 10:39:57 +0000 (GMT) Received: from smtpav02.wdc07v.mail.ibm.com (smtpav02.wdc07v.mail.ibm.com [10.39.53.229]) by smtprelay02.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B1AduJl12845602 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 1 Dec 2026 10:39:56 GMT Received: from smtpav02.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7547F58060; Tue, 1 Dec 2026 10:39:56 +0000 (GMT) Received: from smtpav02.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1271458059; Tue, 1 Dec 2026 10:39:56 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.87.85.9]) by smtpav02.wdc07v.mail.ibm.com (Postfix) with ESMTPS; Tue, 1 Dec 2026 10:39:56 +0000 (GMT) Received: by tuxmaker.boeblingen.de.ibm.com (Postfix, from userid 56341) id 9B4F1162999; Tue, 01 Dec 2026 11:39:54 +0100 (CET) From: Mahanta Jambigi To: mcgrof@kernel.org, petr.pavlu@suse.com, da.gomez@kernel.org, samitolvanen@google.com, atomlin@atomlin.com Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, pasic@linux.ibm.com, wintera@linux.ibm.com, Mahanta Jambigi Subject: [PATCH v2] module: drain external refs before freeing a failed-init module Date: Tue, 1 Dec 2026 11:39:22 +0100 Message-ID: <20261201103922.1480284-1-mjambigi@linux.ibm.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDA0MiBTYWx0ZWRfX4y68+yWhwAz9 lU6Ef4TcRLBJ2MY9ODeswyHk5hWTPJEFhtoXnAdX1DJ183XU9Yda1+uLs/AzbPZ0W/Mz44g3Qow dU/BvDw/tPV0D/FRPWFkLitJNyX3Eck= X-Proofpoint-ORIG-GUID: FUFpVjXT00NOdq9WWnR87BoCB9SDakZq X-Proofpoint-GUID: sFirHauU6sQOEUiR6KhmDG0EsXPifymR X-Authority-Analysis: v=2.4 cv=R6eWitRX c=1 sm=1 tr=0 ts=6b0ea3fe cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=iQ6ETzBq9ecOQQE5vZCe:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=nbrXC89k-Zn08c6dyHkA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDA0MiBTYWx0ZWRfX2gtYXMRWIZKA NY0dFkEerL1tUzG1irQT/9QqtZh2yO+nGuvrruxFwYD4DUz1eIQjF1/eW9jyMfGLhVThc4eGJ2T cD6O7RtQdgs3kOoP0QHfzE8UzL2Yw1jpKinwlh5Kee7jKspF735AjGwGeFj/CWxhGfFOSlaS2Et qbT8h3aH7Yvd6AbiCkCzpz/3UEQ4coZilLFPwHFdH+kdU8sA3ig8vz7uXa8g1oyDUS4IpANmkDr uxDBoYOuvI2mFPaDXLoYQBoS7u/L4peBzZgLRHOuEqclKcSCYE8kkepzSM5kxkwEMi9GzRR0BX5 RnCrp407vInHiV0w5+hw5KM/oqstd5lu6jw3ksR5IdIdjlHrL4D/qWGbJpHbTBxz0JFQAblNvYf 1C0rDIxBLPlsWUgAbeARd1yS7zVChkT396ECOqUXqpgRCuhbYctFWlP/ZYA+sKuGM7SJwaEth0y i0p/pIYi+a1ZO61CEwQ== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_02,2026-11-30_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 priorityscore=1501 adultscore=0 spamscore=0 bulkscore=0 phishscore=0 suspectscore=0 malwarescore=0 clxscore=1015 impostorscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010042 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 --- 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); }