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=1796380875; b=iR9QvCHSSr9XGoTbvIAP4RpeMpjo20HxAFIXO7Iimc8KPe/hEX5JvWsmSdgWzJUSIwrU YY+RH8GDVpxUvsYg0gOGot8gq/ruaFfWR+JQ8vdurWpQ4M7AiuVYNvCNFuTfRV/2vPP4J 2HayUwEdhRnr95h66vk20wXM4I34cpMH6uJxCBcpS+hmOubk2Jsx6SDZ0H94jli0OuCWt mllUrNRgyXOAshx0DBEmi4kFxw5fWrxG9p2WmTa8nxzbKqsrXw1xVz5IqnZz7FH9OOD91 mTdoffjWS8xiN20I0unXEUZQBidBfsbtXs43c7HYMXGFhGkEkvSestAVDhc+E2s48KA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380875; 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-ORIG-GUID: X-Proofpoint-Spam-Details-Enc:X-Authority-Analysis:X-Proofpoint-GUID: X-Proofpoint-Spam-Info:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=tfGktH3uxemJDOkmokGWZrqi86S8oEkkCEeqVjrWDOw=; b=SdXdTcSZLIr+wpN568201RCxyMS1hhaA+p34vXHhJ/NembejceVVYNm1TAZxNKdR6ze2 G9QTKnVheqBoCadfqUW1eauTiptRg/2XCbsw9Ff86JT116d6W/gQH67PnQMdwOqKqJ2nR tzncBjfZFQ+8VwMx2PIo9V61VJxsIEs6zZWBr6lfQDmV+HQs14aJwf5NogN/URUJXbC5e EQstPQVgT3d5gZ/xI0jPhmghzwT+m92BCCbAiaAD7UmMNn1jH64pGUbOp1yfR+aekkhyr zz9Ip5tsObk52c1cNoRFgFl0qFJVfeiGKMopODAdpDnoQdqMkutRfLLq5WLMfpCWnfQ== 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=moatawSx; 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 D7779606A322; Fri, 4 Dec 2026 10:41:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7779606A322 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, unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=moatawSx 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 6B45YpLY1413301; Fri, 4 Dec 2026 10:41:09 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=tfGktH3uxemJDOkmokGWZrqi86S8oEkkCEeqVjrWD Ow=; b=moatawSx8eAMrG8C60evyGI7KDPHYJ5Dk+3RXeoEhLN0kMw1Q3/ONxC2o s1Km5lrjNjQN/swwOArj12Gud8FuT1r8KfGIh41spB1iDT80zseYhLM1U2IWxd/N +OHzcVyqrPNzzPH9FAYXvh9Vv7plwJgYSoZkDO2b7g6soWnqLLAUWIWAgfEw+7dK Tx5cWJ5tLMW7XHATebQxOfjJSqLARkDdnPYBdXTqoG/quskwkObT97+EMRTKAaro Q2c91CqPOgbNcrwVLUhT8LUGl9WqropsV2BU9ddfKNzoD5q795wQYLXEjTyknKOc TgExLaOCy4bV/HXulndmB4LfEH8/w== Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4jbqgc11vg-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 10:41:09 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B4698xv2746326; Fri, 4 Dec 2026 10:41:08 GMT Received: from smtprelay03.dal12v.mail.ibm.com ([172.16.1.5]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4jb2wa5cwy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 10:41:08 +0000 (GMT) Received: from smtpav06.dal12v.mail.ibm.com (smtpav06.dal12v.mail.ibm.com [10.241.53.105]) by smtprelay03.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B4Af6S527722402 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 4 Dec 2026 10:41:06 GMT Received: from smtpav06.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C68C558055; Fri, 4 Dec 2026 10:41:06 +0000 (GMT) Received: from smtpav06.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6F9D258043; Fri, 4 Dec 2026 10:41:06 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.87.85.9]) by smtpav06.dal12v.mail.ibm.com (Postfix) with ESMTPS; Fri, 4 Dec 2026 10:41:06 +0000 (GMT) Received: by tuxmaker.boeblingen.de.ibm.com (Postfix, from userid 56341) id 3CE7E1610D7; Fri, 04 Dec 2026 11:41:05 +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 v3] module: drain external refs before freeing a failed-init module Date: Fri, 4 Dec 2026 11:41:03 +0100 Message-ID: <20261204104104.1711861-1-mjambigi@linux.ibm.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-modules@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-ORIG-GUID: wyH2sWnlDdTA5pXu_RagmM6IbF3RDALL X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjA0MDA0MiBTYWx0ZWRfX9W1Z8Bms0ZaG 3BFbIZli2vSoYHFwsQtSFuPM+nEqXPYNI4qUZgZ6Sl+QGnrSQJAjBRdWDKxsM5Y4FfV0w+sDZy9 PuS/yhIsy8nWS4EuZVvrhV6BS9jYP5MPEs3Kt/GvWGOHMyN0wcZv9gqmieGKx5+0TavsWRCvens DSuQcTrMF5lpUzJA+Jq5u1FJX33rKG9R6A6WbAoTBgPeSZ6EJKnT88xFCQb9cPMoicZKa5WiTSV E8+UeNKQgE3jeBrg5lx/NwuLuxAIskhDJkPIkaGduYMhiahfdIDyvV33opw2NViqF8413nlsRrX 6VrQqVGaC6yVl1iWlMLSw+T2gws5WGY2PqFcCWjzMnsWTqY3ey7DEMamEUkjiINyoRqZZN7/i/j scFt7l8iiJrqTQJ5T3hLXlccg6v0DH14eb1U96FZkzV8p43CWbBnOgBSgS1c2ED13vj9dbfRNEg 9SqKTr5vi9UPVGIsWJw== X-Authority-Analysis: v=2.4 cv=A435rKWG c=1 sm=1 tr=0 ts=6b1298c5 cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=iQ6ETzBq9ecOQQE5vZCe:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=s6otq49xmRZiOtYbJIsA:9 X-Proofpoint-GUID: wp6RZE_W7z98VqseouHu9KGkcQPiCaN_ X-Proofpoint-Spam-Info: AW1haW4tMjYxMjA0MDA0MiBTYWx0ZWRfX1ztGcwhbsOlb +JBUQeKOL3qqdc7bstM99nHoaJrRyPLzoZ7RhqTEi5DJs+RBb47/zpG0c1dkdRGnwx6zBy1KItV q8aWCm4FubRphE5040/7ihqacs7FmRs= 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-04_01,2026-12-03_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 bulkscore=0 adultscore=0 impostorscore=0 spamscore=0 suspectscore=0 clxscore=1015 priorityscore=1501 phishscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612040042 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 and wait for the remaining ones to be put before the GOING teardown and free_module() run, as the unload path does; a getter that raced the state change is either counted and waited for, or finds a zero count. Idempotent loads of the failed module are released before 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; after 10 seconds the loader warns once with the remaining count and keeps waiting. A module that loads successfully and then leaks a reference like this cannot be unloaded either, where rmmod returns -EWOULDBLOCK. A dependent load waiting for a symbol from the failed module does not wait indefinitely: resolve_symbol_wait() gives up after 30 seconds, so a long drain shows up as that error. The missing check predates the git history, so there is no Fixes: tag. Link: https://lore.kernel.org/linux-modules/85cb4573-32dd-44fd-a034-12372a362aa0@linux.ibm.com/ Signed-off-by: Mahanta Jambigi --- Changes in v3: - Drain the references before the GOING notifier chain, klp_module_going() and ftrace_release_mod(), the order try_stop_module() establishes on the unload path. kprobes, SRCU, livepatch and ftrace teardown run there and assume no task is still executing module code. v2 drained after the notifiers so that a consumer torn down there could drop its reference first, but such a consumer would already hold up try_stop_module() and make rmmod fail with -EWOULDBLOCK before the notifiers run. - Poll the count instead of waking it from module_put(). module_put() runs with interrupts disabled under a raw spinlock from the clockevents path, where a regular wait queue cannot be used, and the rare failure path adds no wake-up cost to that hot path; a reference still held after 10 seconds is reported once. - Log when a reference held past the 10 second warning finally drains, so a slow but legitimate holder is distinguishable from a leak. - Drop the early module_wq wake. Waking while the module is still listed in MODULE_STATE_GOING makes resolve_symbol_wait() return -ENOENT for a symbol it found, which bypasses the weak-symbol fallback in simplify_symbols(). A duplicate load that was already waiting on module_wq now sleeps until the trailing wake-up; one that arrives during the drain sees MODULE_STATE_GOING and returns -EBUSY through module_patient_check_exists(). - Keep the idempotent completion before the drain. - Follows the reply to Petr's review of v1 (Link). The three answers still stand: struct module is unchanged, module_put() takes no part in the drain, and duplicate finit_module() loads are completed before the wait. Only the wait queue named in that reply is replaced by the poll above. - Note that the trace_module_put() window between the decrement and its use of mod->name is unchanged and also present on the unload path. 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. - 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 | 77 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 34e0204ea37c..92f84d9965b1 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -3095,13 +3097,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; @@ -3229,6 +3235,52 @@ static noinline int do_init_module(struct module *mod) mod->state = MODULE_STATE_GOING; synchronize_rcu(); module_put(mod); + + /* + * Release idempotent loads before waiting below, so a failed init + * delays only the task that ran it. + */ + if (idem) + idempotent_complete(idem, ret); + +#ifdef CONFIG_MODULE_UNLOAD + /* + * The state change refuses new references. Dropping the base + * reference lets the count reach zero, so a getter that raced the + * state change is either counted and waited for here, or finds + * zero. Wait for the remaining references to go before the GOING + * teardown, which assumes that no reference is held, as + * try_stop_module() guarantees on the unload path. Until then + * module_refcount() reports one less than the references held. + * + * module_put() can run under a raw spinlock with interrupts + * disabled, so a regular wait queue cannot be used and the rare + * failure path should not add a wake-up to that hot path. Poll the + * count instead, and warn once if a reference is still held after + * 10 seconds. + * + * 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) { + unsigned long start = jiffies; + unsigned long warn_at = start + msecs_to_jiffies(10 * 1000); + bool warned = false; + int refs; + + while ((refs = atomic_read(&mod->refcnt)) > 0) { + if (!warned && time_after(jiffies, warn_at)) { + pr_warn("%s: init failed but %d reference(s) are still held after 10s\n", + mod->name, refs); + warned = true; + } + msleep(20); + } + if (warned) + pr_info("%s: init failed, references drained after %lu s\n", + mod->name, (jiffies - start) / HZ); + } +#endif blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); klp_module_going(mod); @@ -3450,7 +3498,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 +3658,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 +3735,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 +3743,7 @@ struct idempotent { struct hlist_node entry; struct completion complete; int ret; + bool completed; }; #define IDEM_HASH_BITS 8 @@ -3710,6 +3759,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 +3781,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 +3794,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 +3805,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 +3832,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 +3869,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 +3881,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); } -- 2.50.1 (Apple Git-155)