linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: penguin-kernel@I-love.SAKURA.ne.jp
Cc: linux-can@vger.kernel.org, mkl@pengutronix.de,
	o.rempel@pengutronix.de, robin@protonic.nl,
	kernel@pengutronix.de, socketcan@hartkopp.net, kuba@kernel.org
Subject: Re: [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
Date: Mon, 16 Nov 2026 18:51:10 +0000	[thread overview]
Message-ID: <179485507057.3132.5060269371508010060@kernel.org> (raw)
In-Reply-To: <dc9d8b6a-5e6c-4392-9b6a-f5f546bfe872@I-love.SAKURA.ne.jp>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] When the cancel is only logical, both the pending address
  claim and the new one are thrown away, and the ECU's address state is…
- [Medium] The new logical-cancel scheme can abort a freshly armed timer
  instead of the old callback it was aimed at.

--

can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()

j1939_ecu_timer_cancel() used to call hrtimer_cancel() with priv->lock held
for write, while the timer handler takes the same lock. The patch switches
to hrtimer_try_to_cancel() and adds two bool flags to cancel logically when
the handler is already running. In that case, j1939_ac_process() drops the
address claim frame.

> diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
> index ca4ad6cdd5cbf..eb5916ac97bb4 100644
> --- a/net/can/j1939/address-claim.c
> +++ b/net/can/j1939/address-claim.c
> @@ -211,8 +211,11 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
>  	if (IS_ERR_OR_NULL(ecu))
>  		goto out_unlock_bh;
>  
> -	/* cancel pending (previous) address claim */
> -	j1939_ecu_timer_cancel(ecu);
> +	/* Cancel pending (previous) address claim. If we can't cancel,
> +	 * do nothing until pending (previous) address claim commits or aborts.
> +	 */
> +	if (!j1939_ecu_timer_cancel(ecu))
> +		goto out_ecu_put;

[Severity: Medium]
Does this lose both the pending claim and the new one?

Take the -1 case this patch is meant to handle. The soft hrtimer callback
for a claim of address A is blocked in write_lock_bh(&priv->lock).
Meanwhile j1939_ac_process() holds the lock and is handling a new AC frame
from the same NAME with SA B:

j1939_ac_process()
  j1939_ecu_timer_cancel()
    hrtimer_try_to_cancel() returns -1, done_ac_handler is false
    ecu->abort_ac_handler = true;
    return false;
  goto out_ecu_put;

The jump skips the unmap and the ecu->addr = skcb->addr.sa update. It
also skips the comparison with the current owner of B, the kick of that
owner, and j1939_ecu_timer_start().

The handler then gets the lock and takes the abort path:

j1939_ecu_timer_handler()
	if (ecu->abort_ac_handler) {
		ecu->abort_ac_handler = false;
		goto out;
	}

so j1939_ecu_map_locked() is never called.

After that, the ECU is not mapped at any address and no timer is armed.
priv->ents[B] still points at the previous owner, even if that owner lost
on NAME priority. ecu->addr is still A.

Since ecu->addr is still A, a later AC from this NAME with sa == A takes
the early exit in j1939_ac_process():

	if (ecu && ecu->addr == skcb->addr.sa) {
		...
		goto out_ecu_put;
	}

so the ECU stays unmapped until it claims some address other than A.

Without the race, either A would commit and B would then move the ECU,
or A would be cancelled and B processed. Here neither happens.

The comment says "do nothing until pending (previous) address claim
commits or aborts". But the same call forces the previous claim to
abort, and nothing retries the dropped frame. Is this the intended
behavior?

[ ... ]

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 9b40f37546233..6520c91a6e1ec 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -156,15 +156,35 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
>  		return;
>  
>  	/* Schedule timer in 250 msec to commit address change. */
> +	ecu->abort_ac_handler = false;
> +	ecu->done_ac_handler = false;
>  	hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
>  		      HRTIMER_MODE_REL_SOFT);
>  }
>  
> -void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
> +bool j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
>  {
> -	if (hrtimer_cancel(&ecu->ac_timer))
> +	switch (hrtimer_try_to_cancel(&ecu->ac_timer)) {
> +	case 0:	/* Not active or already finished. */
> +		break;
> +	case 1:	/* Successfully canceled. */
>  		/* Drop the ref from j1939_ecu_timer_start(). */
>  		j1939_ecu_put(ecu);
> +		break;
> +	default: /* Currently executing. */
> +		/* OK to call j1939_ecu_timer_start() again if
> +		 * j1939_ecu_timer_handler() already committed or aborted.
> +		 */
> +		if (ecu->done_ac_handler)
> +			break;
> +		/* Tell j1939_ecu_timer_handler() to abort. */
> +		ecu->abort_ac_handler = true;
> +		/* Don't call j1939_ecu_timer_start() until
> +		 * j1939_ecu_timer_handler() commits or aborts.
> +		 */
> +		return false;
> +	}
> +	return true;
>  }

[Severity: Medium]
Can this abort a newly armed timer instead of the old callback?

hrtimer_try_to_cancel() returns -1 whenever the callback is running, even
if the timer has been enqueued again since then:

kernel/time/hrtimer.c:hrtimer_try_to_cancel() {
    ...
	if (!hrtimer_callback_running(timer)) {
		ret = remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
    ...
}

hrtimer_start() can re-enqueue the timer while its callback is still
running, because switch_hrtimer_base() keeps the same base in that case:

	if (unlikely(hrtimer_callback_running(timer)))
		return base;

The handler releases priv->lock before __run_hrtimer() clears
base->running:

	ecu->done_ac_handler = true;
	write_unlock_bh(&priv->lock);

	/* Drop the ref from j1939_ecu_timer_start(). */
	j1939_ecu_put(ecu);

Consider this sequence:

CPU0 (arming T1)
j1939_ecu_timer_handler()
  ecu->done_ac_handler = true;
  write_unlock_bh(&priv->lock);
  /* __run_hrtimer() has not cleared base->running yet */

CPU1, AC frame A' (sa != ecu->addr)
j1939_ac_process()
  j1939_ecu_timer_cancel()
    hrtimer_try_to_cancel() returns -1, done_ac_handler is true
    return true;
  j1939_ecu_timer_start()
    abort_ac_handler = false, done_ac_handler = false
    hrtimer_start()  /* T2 enqueued */

CPU1, AC frame B for the same NAME, still before base->running is cleared
j1939_ac_process()
  j1939_ecu_timer_cancel()
    hrtimer_try_to_cancel() returns -1, done_ac_handler is false
    ecu->abort_ac_handler = true;
    return false;
  goto out_ecu_put;  /* B dropped */

250ms later T2 fires, sees abort_ac_handler and skips
j1939_ecu_map_locked().

Both A' and B are lost, even though no handler for A' was running when B
arrived. If B had arrived a little later, hrtimer_try_to_cancel() would
have returned 1 and B would have been processed normally.

The two bools cannot tell an old callback that is still unwinding in
__run_hrtimer() apart from a handler for the current arming. Would the
abort/done state need to be tied to each arming, for example with a
generation counter?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp

      parent reply	other threads:[~2026-11-16 18:51 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15  5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15  5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15  6:01   ` sashiko-bot
2026-11-15  5:34 ` [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
2026-11-16 18:51   ` netdev-bot+sashiko
2026-11-15  5:35 ` [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-16 18:51   ` netdev-bot+sashiko
2026-11-15  5:35 ` [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
2026-11-16 18:51   ` netdev-bot+sashiko
2026-11-15  5:35 ` [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-16 18:51   ` netdev-bot+sashiko
2026-11-15  5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
2026-11-15  5:51   ` sashiko-bot
2026-11-16 18:51   ` netdev-bot+sashiko [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=179485507057.3132.5060269371508010060@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    --cc=robin@protonic.nl \
    --cc=socketcan@hartkopp.net \
    /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;
as well as URLs for NNTP newsgroup(s).