Linux Modules
 help / color / mirror / Atom feed
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 v3] module: drain external refs before freeing a failed-init module
Date: Fri, 04 Dec 2026 11:02:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-249652@kernel.org> (raw)
In-Reply-To: <20261204104104.1711861-1-mjambigi@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] module: non-freezable fixed msleep(20) drain loop in
  do_init_module()
- [Medium] module: premature idempotent_complete() before drain check
  and free_module()
- [Medium] module: atomic_sub_return() underflow re-enables
  try_module_get()
- [Medium] module: missing acquire ordering in do_init_module() refcnt
  drain loop
--

commit 996c0173d26a483c9d16420ac5d9f3674b09cd43
Author: Mahanta Jambigi <mjambigi@linux.ibm.com>

module: drain external refs before freeing a failed-init module

This commit drains external module references on init failure in
do_init_module() before running MODULE_STATE_GOING teardown and
free_module(), while completing idempotent finit_module() waiters before
the wait.

Link: https://lore.kernel.org/linux-modules/85cb4573-32dd-44fd-a034-12372a362aa0@linux.ibm.com/

> diff --git a/kernel/module/main.c b/kernel/module/main.c
> index 6052baf1be891..d3744f17dd098 100644
> --- a/kernel/module/main.c
> +++ b/kernel/module/main.c

[ ... ]

> @@ -3230,6 +3236,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);

[Severity: Medium]
Should idempotent_complete() only run when references actually need to
be drained?

Calling it unconditionally here wakes and unhashes coalesced
finit_module() waiters before MODULE_STATE_GOING teardown and
free_module() finish, even in the common zero-reference failure case and
when CONFIG_MODULE_UNLOAD is disabled.  An immediate retry of
finit_module() can then hit module_patient_check_exists() and fail with
-EBUSY while the module is still linked.

Concurrent finit_module() calls arriving during teardown also miss
idem_hash coalescing and repeat file loading and validation before
failing with -EBUSY, and delete_module() on a dependency can fail with
-EWOULDBLOCK until module_unload_free() runs.  Moving
idempotent_complete() inside the CONFIG_MODULE_UNLOAD drain branch would
keep the previous post-free_module() ordering whenever no wait occurs.

> +
> +#ifdef CONFIG_MODULE_UNLOAD

[ ... ]

> +	if (atomic_sub_return(MODULE_REF_BASE, &mod->refcnt) > 0) {

[Severity: Medium]
Can atomic_sub_return() underflow mod->refcnt from 0 to -1 and allow a
racing try_module_get() to succeed?

If a buggy mod->init() calls an extra module_put(), the module_put(mod)
above drops mod->refcnt to 0 without warning, and atomic_sub_return()
then sets it to -1.

Because try_module_get() uses atomic_inc_not_zero(), a non-RCU caller
such as bpf_get_raw_tracepoint_module() (unlinked later in the
MODULE_STATE_GOING notifier) can increment -1 back to 0 and return true
while the module is freed:

CPU0                                  CPU1
do_init_module()                      bpf_get_raw_tracepoint_module()
  mod->init() (extra module_put)
                                        try_module_get(mod)
                                          module_is_live(mod) -> true
  mod->state = MODULE_STATE_GOING
  synchronize_rcu()
  module_put(mod)          (refcnt=0)
  atomic_sub_return(...)   (refcnt=-1)
                                          atomic_inc_not_zero() -> true
  free_module(mod)
                                        use mod  <-- use-after-free

Using atomic_dec_if_positive(&mod->refcnt) keeps refcnt clamped at 0 so
atomic_inc_not_zero() stays closed, while still returning -1 to skip
the drain loop.

> +		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) {

[Severity: Medium]
Does atomic_read(&mod->refcnt) need acquire ordering here to pair with
atomic_dec_if_positive() in module_put()?

On weakly ordered architectures, a control dependency after a relaxed
atomic_read() does not order subsequent loads.  Teardown right after the
loop, such as srcu_module_going() reading srcu_readers_active(ssp), can
observe stale pre-put state and trigger a spurious WARN_ON() or leak
per-CPU memory.

Using atomic_read_acquire(&mod->refcnt) in the loop condition would
provide the needed acquire barrier.

> +			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);
> +		}

[Severity: Medium]
Can polling indefinitely with msleep(20) block system suspend and
hibernation?

msleep() sleeps in TASK_UNINTERRUPTIBLE without TASK_FREEZABLE, so
try_to_freeze_tasks() times out and aborts suspend with -EBUSY.  A
userspace holder frozen first cannot release the reference while the
loader waits, and a leaked reference blocks suspend until reboot.

Sleeping without TASK_NOLOAD also inflates the load average by 1 during
the wait, and fixed 20 ms polling without backoff generates 50 Hz
wakeups indefinitely.  Sleeping in TASK_IDLE | TASK_FREEZABLE with
schedule_timeout() and backing off the interval would avoid all three.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204104104.1711861-1-mjambigi@linux.ibm.com?part=1

      reply	other threads:[~2026-12-04 11:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 10:41 [PATCH v3] module: drain external refs before freeing a failed-init module Mahanta Jambigi
2026-12-04 11:02 ` 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-249652@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox