Linux CAN drivers development
 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 v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context
Date: Tue, 17 Nov 2026 04:02:45 +0000	[thread overview]
Message-ID: <179488816525.3132.2669816626182950239@kernel.org> (raw)
In-Reply-To: <1ad7d101-4549-420f-a827-6bf21bae884e@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] The commit message's main claim is wrong.
- [Medium] Moving the whole destroy path into
  j1939_session_destroy_work() makes several user-visible side effects…

--

can: j1939: defer j1939_session_destroy() to WQ context

This patch moves the j1939 session teardown from the final kref put into
a work item on j1939_wq, so that j1939_module_exit() can wait for it via
destroy_workqueue(). It also cancels session->txtimer and session->rxtimer
before the session is freed.

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..87a932d10ffea 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
> @@ -259,8 +259,11 @@ static void __j1939_session_drop(struct j1939_session *session)
>  	sock_put(session->sk);
>  }
>  
> -static void j1939_session_destroy(struct j1939_session *session)
> +static void j1939_session_destroy_work(struct work_struct *work)
>  {
> +	struct j1939_session *session = container_of(work, struct j1939_session,
> +						     release_work);
> +	struct j1939_priv *priv = session->priv;
>  	struct sk_buff *skb;
>  
>  	if (session->transmission) {

[ ... ]

> @@ -283,9 +286,18 @@ static void j1939_session_destroy(struct j1939_session *session)
>  		kfree_skb(skb);
>  	}
>  	__j1939_session_drop(session);

[Severity: Medium]
Moving the whole destroy path onto j1939_wq makes several side effects
that userspace can see asynchronous. The commit message doesn't mention
this. Is it intended for a stable fix?

The TX_ACK, TX_ABORT and RX_ABORT notifications from j1939_sk_errqueue()
are now built from the workqueue. __j1939_sk_errqueue() stamps the skb when
it builds it:

	skb->tstamp = ktime_get_real();

Doesn't that mean the SCM_TSTAMP_ACK timestamp for a finished TX session
now records when the work item ran, not when the session completed?

The ordering of notifications also seems to change. The final put of the
finished session often happens in j1939_sk_queue_activate_next_locked():

	list_del_init(&first->sk_session_queue_entry);
	j1939_session_put(first);

After that put, the function activates the next session. If there was no
error, it calls j1939_tp_schedule_txtimer(first, 0). Can the soft hrtimer
for session N+1 then emit TX_SCHED (via j1939_xtp_rx_cts_one()) before
the deferred work emits TX_ACK for session N? Before this patch, TX_ACK(N)
was always queued first.

Several releases now also wait for the work item:

- j1939_sock_pending_del() and the waitq wakeup in __j1939_session_drop()
- sock_put(session->sk)
- netdev_put()

So a close() blocked in j1939_sk_release() on:

	wait_event_interruptible(jsk->waitq,
				 !j1939_sock_pending_get(&jsk->sk))

and netdev unregister refcount waits now depend on j1939_wq making
progress. Should the commit message describe these changes?

> -	netdev_put(session->priv->ndev, &session->priv_dev_tracker);
> -	j1939_priv_put(session->priv);
> +
> +	/* There can't be any ref on this session because __j1939_session_release()
> +	 * was already called. But we need to make sure that j1939_tp_txtimer() and
> +	 * j1939_tp_rxtimer() are no longer running before kfree().
> +	 */
> +	WARN_ON(hrtimer_cancel(&session->txtimer));
> +	WARN_ON(hrtimer_cancel(&session->rxtimer));
> +
> +	netdev_put(priv->ndev, &session->priv_dev_tracker);
> +
>  	kfree(session);
> +	j1939_priv_put(priv);
>  }
>  
>  static void __j1939_session_release(struct kref *kref)
> @@ -293,7 +305,12 @@ static void __j1939_session_release(struct kref *kref)
>  	struct j1939_session *session = container_of(kref, struct j1939_session,
>  						     kref);
>  
> -	j1939_session_destroy(session);
> +	/* To close a race window that j1939_module_exit() starts as soon as
> +	 * j1939_priv_put() dropped the module's refcount, defer actual release
> +	 * to WQ context where j1939_module_exit() can wait for completion.
> +	 */
> +	INIT_WORK(&session->release_work, j1939_session_destroy_work);
> +	queue_work(j1939_wq, &session->release_work);
>  }

[Severity: Medium]
The commit message says this closes:

  "a race window that j1939_module_exit() starts as soon as
  j1939_priv_put(session->priv) from j1939_session_destroy() dropped
  the module's refcount"

Can that race actually happen?

Since the earlier "can: j1939: fix possibility of UAF on module unload"
change, j1939_priv_put() only queues __j1939_priv_release_work(). That
work item does module_put() from j1939_wq. In the old code, the module
text that still ran after that point was kfree(session) and the return
through __j1939_session_release() -> j1939_session_put() -> caller.

With this patch, __j1939_session_release() calls queue_work() and then
returns through the same j1939_session_put() -> caller path. The window
between dropping the last reference and leaving module text looks the
same. It now ends in queue_work() instead of j1939_priv_put().

Every caller that can drop the last session reference already seems to
be covered:

- CAN RX softirq: j1939_can_recv() runs under rcu_read_lock() in
  can_receive(), and j1939_module_exit()->can_proto_unregister() calls
  synchronize_rcu().
- txtimer/rxtimer: these use HRTIMER_MODE_REL_SOFT, so they run in
  softirq with BH disabled, and the same synchronize_rcu() waits for
  them.
- sendmsg/release syscalls: the socket holds a reference on
  j1939_proto.owner.
- netdev notifier: it runs under rtnl, and unregister_netdevice_notifier()
  takes rtnl_lock().
- j1939_wq work items: destroy_workqueue() drains them.

The patch leaves other j1939_priv_put() calls of the same shape, which
may also drop the last reference, unchanged. For example, in
j1939_can_recv():

done:
	j1939_priv_put(priv);
	kfree_skb(skb);

and in j1939_netdev_notify():

	j1939_priv_put(priv);

notify_done:
	return NOTIFY_DONE;

The behaviour changes here seem to be that hrtimer_cancel() now waits for
a running j1939_tp_txtimer() or j1939_tp_rxtimer() before kfree(), and
that j1939_priv_put() now runs after kfree(session).

Since this is tagged for stable, could the commit message describe what
the deferral actually fixes? Alternatively, could it say which caller
context was unprotected before this patch?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/1d96e742-0479-4c36-ad0a-a0aaeda0a4fc%40I-love.SAKURA.ne.jp

      reply	other threads:[~2026-11-17  4:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
2026-11-17  4:02   ` 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=179488816525.3132.2669816626182950239@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