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=1796121364; b=Ag46spneXtyT9aPZ3tUTH+AvPHhokqW7rlK52BODwomq0scHf+/zJZzMLw4Bc9QBnP6B Q4CpOEsU9T4ro3EOZmFOthUjRUzg+im12Ovtz1rjf7y7NLcSRZOTXplK2Jr4P/+OSy1CT HpOPd9fTjxogywsZTOqUtGo5F+vqlfoVi/2k7JAnXZ7878/q2TW5E1CWP2rJhT8kspZH0 W/WvhaSjEdDJYEPlYk/o5NFAtNW0Ai44RbbtpJMAjsV1rPTG+adYI+YWbbWJ39nFVpudP cwqcS1V57TLkUCt4bc+oHaiwjx/Ei6O3VPe31dDJe2Y+xmuvoe/3ehzEAhl9hKoFeOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796121364; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:Received:Received:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-TM-AS-GCONF: X-Proofpoint-Reinject:X-Proofpoint-GUID:X-Proofpoint-Spam-Info: X-Proofpoint-Spam-Details-Enc:X-Authority-Analysis: X-Proofpoint-ORIG-GUID:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=PssgwabfmTt196w0nNiv+ZkwT0S8lthJbl+hv4xFPj4=; b=PJz5H5fnNqkTqOGuwHEuXaFykd8OK7VL/iGsNzthdFr2wKFwzhMgUOI6I2zvHMKBHYMR +jC8pHhQQOsGSgT6onKVcHesYSiclbas4JpQy44cNO9cd2AujPFzzrEIsQpvrXj1FIfmA QRyiVf006a9KQIl4wvgK1HE67whT5hTh7GyWleLNVaPYWc6XLfhA63DkPBnbGLu1VoMlb TKrJ0S+AoRZGNl+3DXStOSEdfGjEsZLCdErjVnjXrdyBC/sFHqQaDChrlOAiwAsOyzGUz 6gJkZsm+1Dg8E1wLJ3pAxALdtPD5md7x5sPf6IpbSoyw8mbjslP4bY9Ee2H6ByLrgxA== 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=Q9WD5ULQ; 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 8C30D606A327; Tue, 1 Dec 2026 10:36:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C30D606A327 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=Q9WD5ULQ Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1AYV7N1585930; Tue, 1 Dec 2026 10:35:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=Pssgwa bfmTt196w0nNiv+ZkwT0S8lthJbl+hv4xFPj4=; b=Q9WD5ULQDYmjljR21jf+oz 90gqWeSfjN6cTj8VU5DSpkyhAAU9l5axTMe8TWtpJPbft15qI/dY35GoB1DokAuY qBuAmqL6A0I4L4jlhxTGbqHqPn6U3rpW5y58gRoklbAZ5CzBH3KKyfP1f+wisGvk 8TrC6a2FCRMi+wbthp7ZnmC/2f0PtpUb2QeqanhouXpBxU+b51mQ3aqJJLvGulNN /8TxoLbyFzwpXxkwB6ui7tBI04NwTaYi06QCuZ1FN7ZmLPCSW/G77z+cmmazUmF/ sy2NAvEzrW08sDRfKItMTYk0iQpvuKJxPoiSTwOGjwF2ANrD0w2hjPOk/H3GzKDA == 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 4j7pp5v72j-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 10:35:50 +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 6B1AYJsV1470510; Tue, 1 Dec 2026 10:35:49 GMT Received: from smtprelay03.wdc07v.mail.ibm.com ([172.16.1.70]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4j9v5w02rn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 01 Dec 2026 10:35:49 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay03.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B1AYtwC27656884 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 1 Dec 2026 10:34:55 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1D8D358054; Tue, 1 Dec 2026 10:35:47 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2541D5805F; Tue, 1 Dec 2026 10:35:43 +0000 (GMT) Received: from [9.123.8.233] (unknown [9.123.8.233]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 1 Dec 2026 10:35:42 +0000 (GMT) Message-ID: <85cb4573-32dd-44fd-a034-12372a362aa0@linux.ibm.com> Date: Tue, 1 Dec 2026 16:05:41 +0530 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] module: drain external refs before freeing a failed-init module To: Petr Pavlu Cc: mcgrof@kernel.org, da.gomez@kernel.org, samitolvanen@google.com, atomlin@atomlin.com, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, pasic@linux.ibm.com, =wintera@linux.ibm.com References: <20261007083956.2488796-1-mjambigi@linux.ibm.com> Content-Language: en-US From: Mahanta Jambigi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-GUID: 1HfyPlW3ttHVe2SN7jlM4Pc_csLFcgsp X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDA0MiBTYWx0ZWRfX7bpkhNAoE82A JJzt/NoAXKU4HrM+PqBbRl1JHtzo1ZwBPkz8LgXbQ9txeZ8RBiNXUptFs+scRl8E4HXU4qDk87S EmeysuIU1QhnBwxLOhDmkvi9LvGha9k= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDA0MiBTYWx0ZWRfX3s7+znk5sdzj QTsdMaCBQwAfwdA0VJOj4WNsV2K00CfRWTwK3SoXVrwMHIM+Ql4s3hJ/OVT0ve5t33rxhxuE8VH fsfcY+kAIMlyHBCT0lh40hpAaV9GpT/YjqUatmDteSuUWbmyj4sXKhIPrOOR+qczWzvOGtAdNt3 mt8U8jCIGv06mpkNOV9jldBhQqYIGk3qEsUaPkw4758VvMOTTjI46KNCqqhhG666Kob3ynOP8Yy 0E9SyzZMT3lZhVW0O/4SlwxKhzVUVo9JgsIu9wIYdr0gtl+EAhnnePM7ueO8pXOZ1RtIrnBGYG7 49G9kQvC/zvlAUjsJj9pcthUIBWYi7KTNRvXeZig7GvZ9tlkE7gNXP6invXC/BPpzaXtDXNYZEN O1EdeGo5ih9njVM1RZPk6rPj9E9SxmuRpDm9j/37puSH+EJct4oEFn+AkaqTDxnzNXDt4+PynfD JrwWzEW0MoWlLiEqKRg== X-Authority-Analysis: v=2.4 cv=Ae8kjHXG c=1 sm=1 tr=0 ts=6b0ea306 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=IkcTkHD0fZMA:10 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=JTAteSi2bO37L03tIM4A:9 a=QEXdDO2ut3YA:10 X-Proofpoint-ORIG-GUID: kihvTo2o4_qERnmZmg7JjHFRA9WlnHnh 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 suspectscore=0 adultscore=0 spamscore=0 malwarescore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010042 Hi Petr, thanks for the review. v2 is on the way and addresses all three points, plus some things that came out while reworking it. The per-module completion is gone. v2 uses one file-static wait queue in kernel/module/main.c, so struct module and include/linux/module.h are unchanged. The new wake-up path in module_put() touches no module memory: after the decrement it only tests the value it already has and wakes the global queue, so a concurrent delete_module() cannot free the module underneath it. module_put() still calls trace_module_put() after the decrement, which reads mod->name and the count; that predates this change and I left it alone. Duplicate and idempotent loads are released before the wait. idempotent_complete() is called from the failure path of do_init_module(), with struct idempotent threaded through load_module() and init_module_from_file(), and module_wq is woken at the same point. The winner still calls idempotent_complete() when idempotent_init_module() returns, so the request now carries a "completed" flag, set under idem_lock: the second call is a no-op and entries added by a newer request for the same file are not completed by the old one. Beyond your points: - The base reference is dropped and the wait is on the raw count, not on "refcnt == MODULE_REF_BASE". try_module_get() then fails as soon as the count reaches zero, which also closes the case where a getter read the state before mod->state became GOING and is not RCU protected, so synchronize_rcu() does not cover it. module_put() wakes only on the transition to zero. - The wait is placed after the GOING notifier chain, the same order delete_module() uses, so a consumer that a notifier tears down drop its reference before the loader blocks on it. - The guard is "> 0" to wait and "<= 0" to proceed. module_put() cannot decrement below zero, so a negative count means a module dropped a reference it never took; such a module is still freed, as it was before, instead of waiting for a count that no put can move. - The trailing wake_up_all(&module_wq) after free_module() stays. A loader can find the module in MODULE_STATE_UNFORMED between the state change in free_module() and its list_del_rcu(), and wait in module_patient_check_exists(); only that wake releases it. The wait is uninterruptible 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; that is in the commit message. If you would rather the loader return immediately, free_module() can be deferred to a work item once the references drain, but I left that out to keep the fix small. There is no Fixes: tag. The failure handling in do_init_module() and the rule that module_is_live() is true for MODULE_STATE_COMING both predate the recorded history, so there is no commit to name, and I have not Cc'd stable for the same reason. Say the word if you want it backported anyway. On 02/11/26 8:22 pm, Petr Pavlu wrote: > On 10/7/26 10:39 AM, Mahanta Jambigi wrote: >> When a module's __init makes it externally reachable (e.g. by registering a >> protocol family or a bus driver) 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 executing module text. >> >> The normal unload path in try_stop_module() refuses to proceed while >> module_refcount() is non-zero. The init-failure path has no such check and races >> to free_module() immediately after synchronize_rcu(). >> >> Fix it by waiting for all external references to drain before calling >> free_module(). Add a per-module struct completion refs_gone inside the existing >> CONFIG_MODULE_UNLOAD block that is initialised in module_unload_init() and >> completed from module_put() when refcnt drops back to MODULE_REF_BASE and the >> module state is MODULE_STATE_GOING. The fail: path calls wait_for_completion() >> (guarded by the same #ifdef) after dropping the loader's own reference, >> replacing the previous implicit assumption that no external refs exist at that >> point. >> >> Also move wake_up_all(&module_wq) to before the drain so that concurrent loaders >> blocked in module_patient_check_exists() are unblocked immediately once >> MODULE_STATE_GOING is visible, rather than after the drain completes. >> >> Fixes: 1fb9341ac348 ("module: put modules in list much earlier.") >> 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 >> >> include/linux/module.h | 2 ++ >> kernel/module/main.c | 33 +++++++++++++++++++++++++++----- >> 2 files changed, 33 insertions(+), 2 deletions(-) >> >> diff --git a/include/linux/module.h b/include/linux/module.h >> index 96cc98568eea..0dee2ed02a2d 100644 >> --- a/include/linux/module.h >> +++ b/include/linux/module.h >> @@ -562,6 +562,8 @@ struct module { >> void (*exit)(void); >> >> atomic_t refcnt; >> + /* Completed when refcnt drops to MODULE_REF_BASE on init failure. */ >> + struct completion refs_gone; > > Having a per-module completion that is needed only during module loading > is a bit wasteful. I think a global waitqueue, similar to module_wq, > should be sufficient. This would also avoid the use-after-free condition > described in my next comment. > >> #endif >> >> #ifdef CONFIG_CONSTRUCTORS >> diff --git a/kernel/module/main.c b/kernel/module/main.c >> index c1b34dc1e89a..655c693bd8e2 100644 >> --- a/kernel/module/main.c >> +++ b/kernel/module/main.c >> @@ -655,6 +655,7 @@ static int module_unload_init(struct module *mod) >> * refcnt == 0 means module is going. >> */ >> atomic_set(&mod->refcnt, MODULE_REF_BASE); >> + init_completion(&mod->refs_gone); >> >> INIT_LIST_HEAD(&mod->source_list); >> INIT_LIST_HEAD(&mod->target_list); >> @@ -956,6 +957,14 @@ 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_); >> + /* >> + * If refcnt just dropped to MODULE_REF_BASE and the module is going >> + * away due to an init failure, all external references are gone. Wake >> + * any thread waiting in the init-failure drain path. >> + */ >> + if (ret == MODULE_REF_BASE && >> + module->state == MODULE_STATE_GOING) >> + complete(&module->refs_gone); > > module_put() can run concurrently with delete_module(). The module might > be gone after the atomic_dec_if_positive() call so this logic could lead > to a use-after-free. > >> } >> } >> EXPORT_SYMBOL(module_put); >> @@ -3206,16 +3214,36 @@ static noinline int do_init_module(struct module *mod) >> fail_free_freeinit: >> kfree(freeinit); >> fail: >> - /* Try to protect us from buggy refcounters. */ >> mod->state = MODULE_STATE_GOING; >> synchronize_rcu(); >> + >> + /* >> + * Unblock concurrent loaders waiting in module_patient_check_exists() on >> + * module_wq. finished_loading() returns true once state is GOING, so they >> + * can now proceed. Do this before the drain below so a failed init delays >> + * only this task, not every other loader that raced to load the same >> + * module. >> + */ >> + wake_up_all(&module_wq); > > This wakes up only modules waiting in module_patient_check_exists(). > However, most duplicate loads using the newer finit_module() syscall > wait in idempotent_wait_for_completion(). They should be woken up as > well, so at worst only the current load gets stuck waiting on its > references to drain. > >> + >> + /* Drop the loader's own reference taken in module_unload_init(). */ >> module_put(mod); >> + >> +#ifdef CONFIG_MODULE_UNLOAD >> + /* >> + * Wait for all external references taken before MODULE_STATE_GOING became >> + * visible to be released. module_put() completes refs_gone when refcnt >> + * drops to MODULE_REF_BASE. The hung-task detector will catch any module >> + * whose error path leaks a reference. >> + */ >> + wait_for_completion(&mod->refs_gone); >> +#endif >> + >> blocking_notifier_call_chain(&module_notify_list, >> MODULE_STATE_GOING, mod); >> klp_module_going(mod); >> ftrace_release_mod(mod); >> free_module(mod); >> - wake_up_all(&module_wq); >> >> return ret; >> } >