public inbox for linux-can@vger.kernel.org
 help / color / mirror / Atom feed
From: Dudu Lu <phx0fer@gmail.com>
To: linux-can@vger.kernel.org
Cc: robin@protonic.nl, o.rempel@pengutronix.de, Dudu Lu <phx0fer@gmail.com>
Subject: [PATCH] can: j1939: Use hrtimer_cancel in j1939_cancel_active_session
Date: Mon, 13 Apr 2026 16:45:48 +0800	[thread overview]
Message-ID: <20260413084548.69294-1-phx0fer@gmail.com> (raw)

j1939_cancel_active_session() uses hrtimer_try_to_cancel() for both
txtimer and rxtimer. When hrtimer_try_to_cancel() returns -1, it means
the timer callback is currently executing. In this case, the function
neither cancels the timer nor drops the session reference via
j1939_session_put(). The session is then deactivated while the timer
callback may still be running, leading to a potential use-after-free if
the callback accesses the session after it has been cleaned up.

Replace hrtimer_try_to_cancel() with hrtimer_cancel() which will wait
for an in-progress callback to complete before returning, ensuring the
timer is fully stopped before the session is deactivated.

Note: This changes the function to potentially sleep (hrtimer_cancel
waits for the callback). The function is called with
active_session_list_lock held (a spinlock), so an alternative approach
would be to handle the -1 return from hrtimer_try_to_cancel() without
blocking. However, if the lock can be converted or the cancel moved
outside the lock, hrtimer_cancel() is the cleaner fix.

Signed-off-by: Dudu Lu <phx0fer@gmail.com>
---
 net/can/j1939/transport.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index df93d57907da..996d4c2cbbb0 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -2230,9 +2230,9 @@ int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk)
 				 &priv->active_session_list,
 				 active_session_list_entry) {
 		if (!sk || sk == session->sk) {
-			if (hrtimer_try_to_cancel(&session->txtimer) == 1)
+			if (hrtimer_cancel(&session->txtimer))
 				j1939_session_put(session);
-			if (hrtimer_try_to_cancel(&session->rxtimer) == 1)
+			if (hrtimer_cancel(&session->rxtimer))
 				j1939_session_put(session);
 
 			session->err = ESHUTDOWN;
-- 
2.39.3 (Apple Git-145)


             reply	other threads:[~2026-04-13  8:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13  8:45 Dudu Lu [this message]
2026-04-13 13:01 ` [PATCH] can: j1939: Use hrtimer_cancel in j1939_cancel_active_session Oleksij Rempel

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=20260413084548.69294-1-phx0fer@gmail.com \
    --to=phx0fer@gmail.com \
    --cc=linux-can@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=robin@protonic.nl \
    /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