* [PATCH v3.1] can: j1939: fix j1939_ecu refcount leak
@ 2026-10-25 14:54 Tetsuo Handa
2026-10-27 0:22 ` [PATCH v3.2] " Tetsuo Handa
0 siblings, 1 reply; 4+ messages in thread
From: Tetsuo Handa @ 2026-10-25 14:54 UTC (permalink / raw)
To: LKML
syzbot is reporting "struct j1939_ecu" refcount leak, for
j1939_ecu_get(ecu);
priv->ents[ecu->addr] = ecu;
in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after
priv->ents[ecu->addr] = NULL;
j1939_ecu_put(ecu);
in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed.
Fix this race condition by canceling address claim timers from
j1939_ecu_unmap_all().
gpt-6-astra, opus-5-5, opus-5 and sashiko-gemini are reporting that
above-mentioned change cannot fully close the race condition. Fix
remaining race condition by also checking netdev state from locations
where new refcount on an ECU is taken.
gpt-6-astra, opus-5-5, opus-5 and sonnet-5 are reporting that there is
a possibility of deadlock between j1939_ecu_timer_handler() and
j1939_ecu_timer_cancel(), for the former holds priv->lock while the latter
calls hrtimer_cancel() (which synchronously waits for the former to
complete) with priv->lock already held. Fix this deadlock by using
ac_timer_cancel counter.
sashiko-gemini is reporting that there is a possibility of list corruption
in __j1939_ecu_release(), for not all callers are calling j1939_ecu_put()
with priv->lock held for write. Since we don't want to track whether the
caller of j1939_ecu_put() is holding priv->lock for write (or we can't if
somewhere is calling j1939_ecu_put() with priv->lock held for read), fix
this problem by using a dedicated spinlock for list modification and
changing to use RCU for list traversal and refcount acquiring.
Change j1939_ecu_get() to return bool, for it might be called after
__j1939_ecu_release() was called. Also, enable context analysis support
for the j1939 module, in order to be able to catch missing RCU protection
at build time.
Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd
Link: https://lkml.kernel.org/r/179202402875.794844.18141702206663586949.b4-review@b4
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Assisted-by: Gemini-Pro
Assisted-by: gpt-6-astra
Assisted-by: opus-5-5
Assisted-by: opus-5
Assisted-by: sashiko-gemini
Assisted-by: sonnet-5
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
There might be locations where we can replace priv->lock with RCU, but
this patch simply holds both locks and releases both locks. Analysis of
redundant priv->lock is welcomed.
Changes in v3.1:
- Address findings from the review bot.
net/can/j1939/Makefile | 1 +
net/can/j1939/address-claim.c | 6 ++
net/can/j1939/bus.c | 134 ++++++++++++++++++++++++++++------
net/can/j1939/j1939-priv.h | 13 ++--
net/can/j1939/main.c | 3 +
net/can/j1939/transport.c | 2 +
6 files changed, 133 insertions(+), 26 deletions(-)
diff --git a/net/can/j1939/Makefile b/net/can/j1939/Makefile
index 19181bdae173..ce6f9cc257cf 100644
--- a/net/can/j1939/Makefile
+++ b/net/can/j1939/Makefile
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
+CONTEXT_ANALYSIS := y
obj-$(CONFIG_CAN_J1939) += can-j1939.o
diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
index ca4ad6cdd5cb..6d2f3b1c2d24 100644
--- a/net/can/j1939/address-claim.c
+++ b/net/can/j1939/address-claim.c
@@ -148,6 +148,11 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
}
write_lock_bh(&priv->lock);
+ rcu_read_lock();
+
+ /* Don't get ECU ref if teardown is in progress. */
+ if (priv->ndev->reg_state != NETREG_REGISTERED)
+ goto out_unlock_bh;
/* Few words on the ECU ref counting:
*
@@ -241,6 +246,7 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
out_ecu_put:
j1939_ecu_put(ecu);
out_unlock_bh:
+ rcu_read_unlock();
write_unlock_bh(&priv->lock);
}
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index cdc3c0a71937..d55a95ceaff8 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -14,15 +14,32 @@
#include "j1939-priv.h"
-static void __j1939_ecu_release(struct kref *kref)
+/* The spinlock dedicated for adding/deleting an ECU to/from ECU list.
+ * Reading ECU list is protected by RCU read lock section.
+ */
+static DEFINE_SPINLOCK(j1939_ecu_add_del_lock);
+
+static void __j1939_ecu_release_rcu(struct rcu_head *rcu)
{
- struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+ struct j1939_ecu *ecu = container_of(rcu, struct j1939_ecu, rcu);
struct j1939_priv *priv = ecu->priv;
- list_del(&ecu->list);
netdev_put(priv->ndev, &ecu->priv_dev_tracker);
- kfree(ecu);
j1939_priv_put(priv);
+ kfree(ecu);
+}
+
+static void __j1939_ecu_release(struct kref *kref)
+{
+ struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+
+ /* Immediately remove this ECU from list but wait for RCU grace period,
+ * for j1939_ecu_get() might be called on this ECU under RCU read lock.
+ */
+ spin_lock_bh(&j1939_ecu_add_del_lock);
+ list_del_rcu(&ecu->list);
+ spin_unlock_bh(&j1939_ecu_add_del_lock);
+ call_rcu(&ecu->rcu, __j1939_ecu_release_rcu);
}
void j1939_ecu_put(struct j1939_ecu *ecu)
@@ -30,12 +47,14 @@ void j1939_ecu_put(struct j1939_ecu *ecu)
kref_put(&ecu->kref, __j1939_ecu_release);
}
-static void j1939_ecu_get(struct j1939_ecu *ecu)
+static __must_check bool j1939_ecu_get(struct j1939_ecu *ecu)
+ __must_hold_shared(RCU)
{
- kref_get(&ecu->kref);
+ return kref_get_unless_zero(&ecu->kref);
}
static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu)
+ __must_hold_shared(RCU)
{
struct j1939_priv *priv = ecu->priv;
@@ -47,6 +66,7 @@ static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu)
/* ECU device interface */
/* map ECU to a bus address space */
static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
+ __must_hold_shared(RCU)
{
struct j1939_priv *priv = ecu->priv;
struct j1939_addr_ent *ent;
@@ -56,15 +76,21 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
if (!j1939_address_is_unicast(ecu->addr))
return;
+ /* Don't get an ECU ref if teardown is in progress. */
+ if (priv->ndev->reg_state != NETREG_REGISTERED)
+ return;
+
+ if (!j1939_ecu_get(ecu))
+ return;
+
ent = &priv->ents[ecu->addr];
if (ent->ecu) {
netdev_warn(priv->ndev, "Trying to map already mapped ECU, addr: 0x%02x, name: 0x%016llx. Skip it.\n",
ecu->addr, ecu->name);
+ j1939_ecu_put(ecu);
return;
}
-
- j1939_ecu_get(ecu);
ent->ecu = ecu;
ent->nusers += ecu->nusers;
}
@@ -92,27 +118,47 @@ void j1939_ecu_unmap_locked(struct j1939_ecu *ecu)
void j1939_ecu_unmap(struct j1939_ecu *ecu)
{
write_lock_bh(&ecu->priv->lock);
+ rcu_read_lock();
j1939_ecu_unmap_locked(ecu);
+ rcu_read_unlock();
write_unlock_bh(&ecu->priv->lock);
}
void j1939_ecu_unmap_all(struct j1939_priv *priv)
{
int i;
+ struct j1939_ecu *ecu;
write_lock_bh(&priv->lock);
+ rcu_read_lock();
+ /* Cancel all pending address claim timers before unmapping.
+ * This prevents an orphaned late-firing timer from re-mapping
+ * an ECU after the spaces are cleared.
+ */
+ list_for_each_entry_rcu(ecu, &priv->ecus, list)
+ j1939_ecu_timer_cancel(ecu);
+ /* Unmap any remaining mapped ECUs */
for (i = 0; i < ARRAY_SIZE(priv->ents); i++)
if (priv->ents[i].ecu)
j1939_ecu_unmap_locked(priv->ents[i].ecu);
+ rcu_read_unlock();
write_unlock_bh(&priv->lock);
}
void j1939_ecu_timer_start(struct j1939_ecu *ecu)
{
+ lockdep_assert_held(&ecu->priv->lock);
+
+ /* Don't get an ECU ref if teardown is in progress. */
+ if (ecu->priv->ndev->reg_state != NETREG_REGISTERED)
+ return;
+
/* The ECU is held here and released in the
- * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
+ * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel()
+ * or j1939_ecu_unmap_all().
*/
- j1939_ecu_get(ecu);
+ if (!j1939_ecu_get(ecu))
+ return;
/* Schedule timer in 250 msec to commit address change. */
hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
@@ -121,7 +167,12 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
{
- if (hrtimer_cancel(&ecu->ac_timer))
+ int ret;
+
+ atomic_inc(&ecu->ac_timer_cancel);
+ ret = hrtimer_cancel(&ecu->ac_timer);
+ atomic_dec(&ecu->ac_timer_cancel);
+ if (ret)
j1939_ecu_put(ecu);
}
@@ -131,7 +182,30 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
container_of(hrtimer, struct j1939_ecu, ac_timer);
struct j1939_priv *priv = ecu->priv;
- write_lock_bh(&priv->lock);
+ local_bh_disable();
+ if (!write_trylock(&priv->lock)) {
+ local_bh_enable();
+ /* Since j1939_ecu_timer_cancel() is called under priv->lock
+ * held for write, calling hrtimer_cancel() when the timer was
+ * between "ready to call j1939_ecu_timer_handler()" and "about
+ * to hold priv->lock for write" can cause deadlock. Therefore,
+ * check whether j1939_ecu_timer_handler() should be aborted,
+ * by checking ac_timer_cancel value when "failed to hold
+ * priv->lock for write".
+ *
+ * Drop ref from j1939_ecu_timer_start() and return if someone
+ * is calling j1939_ecu_timer_cancel(). Reschedule this timer
+ * to be fired again after a short delay otherwise.
+ */
+ if (atomic_read(&ecu->ac_timer_cancel)) {
+ j1939_ecu_put(ecu);
+ return HRTIMER_NORESTART;
+ } else {
+ hrtimer_forward_now(hrtimer, ms_to_ktime(1));
+ return HRTIMER_RESTART;
+ }
+ }
+ rcu_read_lock();
/* TODO: can we test if ecu->addr is unicast before starting
* the timer?
*/
@@ -141,7 +215,9 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
* j1939_ecu_timer_start().
*/
j1939_ecu_put(ecu);
- write_unlock_bh(&priv->lock);
+ rcu_read_unlock();
+ write_unlock(&priv->lock);
+ local_bh_enable();
return HRTIMER_NORESTART;
}
@@ -152,6 +228,10 @@ struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name)
lockdep_assert_held(&priv->lock);
+ /* Don't get an ECU ref if teardown is in progress. */
+ if (priv->ndev->reg_state != NETREG_REGISTERED)
+ return ERR_PTR(-ENOMEM);
+
ecu = kzalloc_obj(*ecu, gfp_any());
if (!ecu)
return ERR_PTR(-ENOMEM);
@@ -166,7 +246,9 @@ struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name)
j1939_priv_get(priv);
ecu->priv = priv;
- list_add_tail(&ecu->list, &priv->ecus);
+ spin_lock_bh(&j1939_ecu_add_del_lock);
+ list_add_tail_rcu(&ecu->list, &priv->ecus);
+ spin_unlock_bh(&j1939_ecu_add_del_lock);
return ecu;
}
@@ -189,8 +271,8 @@ struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv, u8 addr)
return NULL;
ecu = j1939_ecu_find_by_addr_locked(priv, addr);
- if (ecu)
- j1939_ecu_get(ecu);
+ if (ecu && !j1939_ecu_get(ecu))
+ ecu = NULL;
return ecu;
}
@@ -200,7 +282,9 @@ struct j1939_ecu *j1939_ecu_get_by_addr(struct j1939_priv *priv, u8 addr)
struct j1939_ecu *ecu;
read_lock_bh(&priv->lock);
+ rcu_read_lock();
ecu = j1939_ecu_get_by_addr_locked(priv, addr);
+ rcu_read_unlock();
read_unlock_bh(&priv->lock);
return ecu;
@@ -209,12 +293,13 @@ struct j1939_ecu *j1939_ecu_get_by_addr(struct j1939_priv *priv, u8 addr)
/* get pointer to ecu without increasing ref counter */
static struct j1939_ecu *j1939_ecu_find_by_name_locked(struct j1939_priv *priv,
name_t name)
+ __must_hold_shared(RCU)
{
struct j1939_ecu *ecu;
lockdep_assert_held(&priv->lock);
- list_for_each_entry(ecu, &priv->ecus, list) {
+ list_for_each_entry_rcu(ecu, &priv->ecus, list) {
if (ecu->name == name)
return ecu;
}
@@ -227,14 +312,12 @@ struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
{
struct j1939_ecu *ecu;
- lockdep_assert_held(&priv->lock);
-
if (!name)
return NULL;
ecu = j1939_ecu_find_by_name_locked(priv, name);
- if (ecu)
- j1939_ecu_get(ecu);
+ if (ecu && !j1939_ecu_get(ecu))
+ ecu = NULL;
return ecu;
}
@@ -244,7 +327,9 @@ struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name)
struct j1939_ecu *ecu;
read_lock_bh(&priv->lock);
+ rcu_read_lock();
ecu = j1939_ecu_get_by_name_locked(priv, name);
+ rcu_read_unlock();
read_unlock_bh(&priv->lock);
return ecu;
@@ -259,11 +344,14 @@ u8 j1939_name_to_addr(struct j1939_priv *priv, name_t name)
return J1939_NO_ADDR;
read_lock_bh(&priv->lock);
+ rcu_read_lock();
+
ecu = j1939_ecu_find_by_name_locked(priv, name);
if (ecu && j1939_ecu_is_mapped_locked(ecu))
/* ecu's SA is registered */
addr = ecu->addr;
+ rcu_read_unlock();
read_unlock_bh(&priv->lock);
return addr;
@@ -281,6 +369,7 @@ int j1939_local_ecu_get(struct j1939_priv *priv, name_t name, u8 sa)
int err = 0;
write_lock_bh(&priv->lock);
+ rcu_read_lock();
if (j1939_address_is_unicast(sa))
priv->ents[sa].nusers++;
@@ -305,6 +394,7 @@ int j1939_local_ecu_get(struct j1939_priv *priv, name_t name, u8 sa)
priv->ents[ecu->addr].nusers++;
done:
+ rcu_read_unlock();
write_unlock_bh(&priv->lock);
return err;
@@ -315,6 +405,7 @@ void j1939_local_ecu_put(struct j1939_priv *priv, name_t name, u8 sa)
struct j1939_ecu *ecu;
write_lock_bh(&priv->lock);
+ rcu_read_lock();
if (j1939_address_is_unicast(sa))
priv->ents[sa].nusers--;
@@ -334,5 +425,6 @@ void j1939_local_ecu_put(struct j1939_priv *priv, name_t name, u8 sa)
j1939_ecu_put(ecu);
done:
+ rcu_read_unlock();
write_unlock_bh(&priv->lock);
}
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..b0092b06c5cc 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -42,6 +42,9 @@ struct j1939_ecu {
/* count users, to help transport protocol decide for interaction */
int nusers;
+
+ atomic_t ac_timer_cancel;
+ struct rcu_head rcu;
};
struct j1939_priv {
@@ -123,18 +126,18 @@ static inline bool j1939_pgn_is_pdu1(pgn_t pgn)
}
/* utility to correctly unmap an ECU */
-void j1939_ecu_unmap_locked(struct j1939_ecu *ecu);
+void j1939_ecu_unmap_locked(struct j1939_ecu *ecu) __must_hold_shared(RCU);
void j1939_ecu_unmap(struct j1939_ecu *ecu);
u8 j1939_name_to_addr(struct j1939_priv *priv, name_t name);
struct j1939_ecu *j1939_ecu_find_by_addr_locked(struct j1939_priv *priv,
- u8 addr);
+ u8 addr) __must_hold_shared(RCU);
struct j1939_ecu *j1939_ecu_get_by_addr(struct j1939_priv *priv, u8 addr);
struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv,
- u8 addr);
+ u8 addr) __must_hold_shared(RCU);
struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name);
struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
- name_t name);
+ name_t name) __must_hold_shared(RCU);
enum j1939_transfer_type {
J1939_TP,
@@ -202,7 +205,7 @@ void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb);
/* network management */
struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name);
-void j1939_ecu_timer_start(struct j1939_ecu *ecu);
+void j1939_ecu_timer_start(struct j1939_ecu *ecu) __must_hold_shared(RCU);
void j1939_ecu_timer_cancel(struct j1939_ecu *ecu);
void j1939_ecu_unmap_all(struct j1939_priv *priv);
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index 5e5e6c228f22..3a7b544c87b8 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -421,6 +421,9 @@ static __init int j1939_module_init(void)
static __exit void j1939_module_exit(void)
{
+ /* Wait for __j1939_ecu_release_rcu() to complete. */
+ rcu_barrier();
+
can_proto_unregister(&j1939_can_proto);
unregister_netdevice_notifier(&j1939_netdev_notifier);
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..1f27f9a700a4 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -235,11 +235,13 @@ static int j1939_xtp_abort_to_errno(struct j1939_priv *priv,
}
static inline void j1939_session_list_lock(struct j1939_priv *priv)
+ __acquires(&priv->active_session_list_lock)
{
spin_lock_bh(&priv->active_session_list_lock);
}
static inline void j1939_session_list_unlock(struct j1939_priv *priv)
+ __releases(&priv->active_session_list_lock)
{
spin_unlock_bh(&priv->active_session_list_lock);
}
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3.2] can: j1939: fix j1939_ecu refcount leak
2026-10-25 14:54 [PATCH v3.1] can: j1939: fix j1939_ecu refcount leak Tetsuo Handa
@ 2026-10-27 0:22 ` Tetsuo Handa
2026-10-27 3:31 ` Karl Mehltretter
0 siblings, 1 reply; 4+ messages in thread
From: Tetsuo Handa @ 2026-10-27 0:22 UTC (permalink / raw)
To: LKML
syzbot is reporting "struct j1939_ecu" refcount leak, for
j1939_ecu_get(ecu);
priv->ents[ecu->addr] = ecu;
in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after
priv->ents[ecu->addr] = NULL;
j1939_ecu_put(ecu);
in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed.
Fix this race condition by canceling address claim timers from
j1939_ecu_unmap_all().
gpt-6-astra, opus-5-5, opus-5 and sashiko-gemini are reporting that
above-mentioned change alone cannot fully close the race condition.
Fix remaining race condition by allow getting a ref on an ECU only when
three conditions (a netdev which is associated with j1939_priv is still
registered, rx_kref which is associated with j1939_priv is still alive,
kref which is associated with j1939_ecu is still alive) are met.
sashiko-gemini is reporting that there is a possibility of list corruption
in __j1939_ecu_release(), for not all callers are calling j1939_ecu_put()
with priv->lock held for write. Fix this problem by deferring release steps
to RCU context where we can unconditionally hold priv->lock for write.
By deferring to RCU context, readers might find ECUs which have already
reached __j1939_ecu_release(). Therefore, allow getting a ref on an ECU
only when above-mentioned three conditions are met.
gpt-6-astra, opus-5-5, opus-5 and sonnet-5 are reporting that there is
a possibility of deadlock between j1939_ecu_timer_handler() and
j1939_ecu_timer_cancel(), for the former holds priv->lock while the latter
calls hrtimer_cancel() (which synchronously waits for the former to
complete) with priv->lock already held. Fix this problem by using
hrtimer_try_to_cancel() with "release priv->lock, call hrtimer_cancel(),
hold priv->lock again and restart from the beginning" fallback.
Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd
Link: https://lkml.kernel.org/r/179202402875.794844.18141702206663586949.b4-review@b4
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Assisted-by: Gemini-Pro
Assisted-by: gpt-6-astra
Assisted-by: opus-5-5
Assisted-by: opus-5
Assisted-by: sashiko-gemini
Assisted-by: sonnet-5
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
Changes in v3.2:
- Address findings from the review bot.
net/can/j1939/address-claim.c | 10 ++-
net/can/j1939/bus.c | 117 +++++++++++++++++++++++++++++-----
net/can/j1939/j1939-priv.h | 4 +-
net/can/j1939/main.c | 3 +
4 files changed, 115 insertions(+), 19 deletions(-)
diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
index ca4ad6cdd5cb..2f27948d6e87 100644
--- a/net/can/j1939/address-claim.c
+++ b/net/can/j1939/address-claim.c
@@ -147,6 +147,7 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
return;
}
+restart:
write_lock_bh(&priv->lock);
/* Few words on the ECU ref counting:
@@ -212,7 +213,14 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
goto out_unlock_bh;
/* cancel pending (previous) address claim */
- j1939_ecu_timer_cancel(ecu);
+ if (!j1939_ecu_timer_stopped(ecu)) {
+ write_unlock_bh(&priv->lock);
+ if (hrtimer_cancel(&ecu->ac_timer))
+ j1939_ecu_put(ecu);
+ /* Drop ref from j1939_ecu_get_by_name_locked() and retry. */
+ j1939_ecu_put(ecu);
+ goto restart;
+ }
if (j1939_address_is_idle(skcb->addr.sa)) {
j1939_ecu_unmap_locked(ecu);
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index cdc3c0a71937..1a82a9de2119 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -14,25 +14,57 @@
#include "j1939-priv.h"
-static void __j1939_ecu_release(struct kref *kref)
+static void __j1939_ecu_release_rcu(struct rcu_head *rcu)
{
- struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+ struct j1939_ecu *ecu = container_of(rcu, struct j1939_ecu, rcu);
struct j1939_priv *priv = ecu->priv;
+ write_lock_bh(&priv->lock);
list_del(&ecu->list);
+ write_unlock_bh(&priv->lock);
netdev_put(priv->ndev, &ecu->priv_dev_tracker);
kfree(ecu);
j1939_priv_put(priv);
}
+static void __j1939_ecu_release(struct kref *kref)
+{
+ struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+
+ /* Since not all callers are calling j1939_ecu_put() with priv->lock
+ * held for write, defer actual release to RCU context. By deferring to
+ * RCU context, priv->ecus readers might find ECUs with ecu->kref == 0.
+ * Therefore, priv->ecus readers must validate with j1939_ecu_valid() or
+ * j1939_ecu_get().
+ */
+ call_rcu(&ecu->rcu, __j1939_ecu_release_rcu);
+}
+
void j1939_ecu_put(struct j1939_ecu *ecu)
{
kref_put(&ecu->kref, __j1939_ecu_release);
}
-static void j1939_ecu_get(struct j1939_ecu *ecu)
+static __must_check bool j1939_ecu_get(struct j1939_ecu *ecu)
{
- kref_get(&ecu->kref);
+ struct j1939_priv *priv = ecu->priv;
+
+ /* Don't get an ECU ref if netdev is no longer registered or
+ * __j1939_rx_release() was called or __j1939_ecu_release() was called.
+ */
+ return priv->ndev->reg_state == NETREG_REGISTERED &&
+ kref_read(&priv->rx_kref) && kref_get_unless_zero(&ecu->kref);
+}
+
+static __must_check bool j1939_ecu_valid(struct j1939_ecu *ecu)
+{
+ struct j1939_priv *priv = ecu->priv;
+
+ /* Treat an ECU as invalid if netdev is no longer registered or
+ * __j1939_rx_release() was called or __j1939_ecu_release() was called.
+ */
+ return priv->ndev->reg_state == NETREG_REGISTERED &&
+ kref_read(&priv->rx_kref) && kref_read(&ecu->kref);
}
static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu)
@@ -56,15 +88,17 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
if (!j1939_address_is_unicast(ecu->addr))
return;
+ if (!j1939_ecu_get(ecu))
+ return;
+
ent = &priv->ents[ecu->addr];
if (ent->ecu) {
netdev_warn(priv->ndev, "Trying to map already mapped ECU, addr: 0x%02x, name: 0x%016llx. Skip it.\n",
ecu->addr, ecu->name);
+ j1939_ecu_put(ecu);
return;
}
-
- j1939_ecu_get(ecu);
ent->ecu = ecu;
ent->nusers += ecu->nusers;
}
@@ -99,8 +133,36 @@ void j1939_ecu_unmap(struct j1939_ecu *ecu)
void j1939_ecu_unmap_all(struct j1939_priv *priv)
{
int i;
+ struct j1939_ecu *ecu;
+restart:
write_lock_bh(&priv->lock);
+ /* Cancel all pending address claim timers before unmapping.
+ * This prevents an orphaned late-firing timer from re-mapping
+ * an ECU after the spaces are cleared. Since j1939_ecu_put()
+ * might make , we need to skip invalid ECUs.
+ */
+ list_for_each_entry(ecu, &priv->ecus, list) {
+ /* It is OK to call j1939_ecu_timer_stopped() even if there is
+ * no ref on this ECU, for __j1939_ecu_release_rcu() has not
+ * removed this ECU from priv->ecus list due to priv->lock.
+ */
+ if (j1939_ecu_timer_stopped(ecu))
+ continue;
+ /* Keep this ECU valid while calling hrtimer_cancel(). But
+ * don't use j1939_ecu_get() here, for we want to wait for
+ * completion of timer callback regardless of netdev's state.
+ */
+ if (!kref_get_unless_zero(&ecu->kref))
+ continue;
+ write_unlock_bh(&priv->lock);
+ if (hrtimer_cancel(&ecu->ac_timer))
+ j1939_ecu_put(ecu);
+ /* OK to release this ECU. */
+ j1939_ecu_put(ecu);
+ goto restart;
+ }
+ /* Unmap any remaining mapped ECUs */
for (i = 0; i < ARRAY_SIZE(priv->ents); i++)
if (priv->ents[i].ecu)
j1939_ecu_unmap_locked(priv->ents[i].ecu);
@@ -109,20 +171,33 @@ void j1939_ecu_unmap_all(struct j1939_priv *priv)
void j1939_ecu_timer_start(struct j1939_ecu *ecu)
{
+ lockdep_assert_held(&ecu->priv->lock);
+
/* The ECU is held here and released in the
- * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
+ * j1939_ecu_timer_handler() or j1939_ecu_timer_stopped()
+ * or j1939_ecu_unmap_all() or j1939_ac_process().
*/
- j1939_ecu_get(ecu);
+ if (!j1939_ecu_get(ecu))
+ return;
/* Schedule timer in 250 msec to commit address change. */
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_stopped(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. */
+ return true;
+ case 1: /* Successfully canceled. */
+ /* Drop a ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
+ return true;
+ default: /* Currently executing. */
+ /* Leave how to handle to the caller. */
+ return false;
+ }
}
static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
@@ -152,6 +227,13 @@ struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name)
lockdep_assert_held(&priv->lock);
+ /* Don't create a new ECU if netdev is no longer registered or
+ * __j1939_rx_release() was called.
+ */
+ if (priv->ndev->reg_state != NETREG_REGISTERED ||
+ !kref_read(&priv->rx_kref))
+ return ERR_PTR(-ENOMEM);
+
ecu = kzalloc_obj(*ecu, gfp_any());
if (!ecu)
return ERR_PTR(-ENOMEM);
@@ -189,8 +271,8 @@ struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv, u8 addr)
return NULL;
ecu = j1939_ecu_find_by_addr_locked(priv, addr);
- if (ecu)
- j1939_ecu_get(ecu);
+ if (ecu && !j1939_ecu_get(ecu))
+ ecu = NULL;
return ecu;
}
@@ -215,7 +297,7 @@ static struct j1939_ecu *j1939_ecu_find_by_name_locked(struct j1939_priv *priv,
lockdep_assert_held(&priv->lock);
list_for_each_entry(ecu, &priv->ecus, list) {
- if (ecu->name == name)
+ if (ecu->name == name && j1939_ecu_valid(ecu))
return ecu;
}
@@ -232,11 +314,12 @@ struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
if (!name)
return NULL;
- ecu = j1939_ecu_find_by_name_locked(priv, name);
- if (ecu)
- j1939_ecu_get(ecu);
+ list_for_each_entry(ecu, &priv->ecus, list) {
+ if (ecu->name == name && j1939_ecu_get(ecu))
+ return ecu;
+ }
- return ecu;
+ return NULL;
}
struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name)
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..0cfb48eb61fe 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -42,6 +42,8 @@ struct j1939_ecu {
/* count users, to help transport protocol decide for interaction */
int nusers;
+
+ struct rcu_head rcu;
};
struct j1939_priv {
@@ -203,7 +205,7 @@ void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb);
struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name);
void j1939_ecu_timer_start(struct j1939_ecu *ecu);
-void j1939_ecu_timer_cancel(struct j1939_ecu *ecu);
+bool __must_check j1939_ecu_timer_stopped(struct j1939_ecu *ecu);
void j1939_ecu_unmap_all(struct j1939_priv *priv);
struct j1939_priv *j1939_netdev_start(struct net_device *ndev);
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index 5e5e6c228f22..90a925a5c123 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -424,6 +424,9 @@ static __exit void j1939_module_exit(void)
can_proto_unregister(&j1939_can_proto);
unregister_netdevice_notifier(&j1939_netdev_notifier);
+
+ /* Wait for __j1939_ecu_release_rcu() to complete. */
+ rcu_barrier();
}
module_init(j1939_module_init);
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3.2] can: j1939: fix j1939_ecu refcount leak
2026-10-27 0:22 ` [PATCH v3.2] " Tetsuo Handa
@ 2026-10-27 3:31 ` Karl Mehltretter
2026-10-27 14:35 ` Tetsuo Handa
0 siblings, 1 reply; 4+ messages in thread
From: Karl Mehltretter @ 2026-10-27 3:31 UTC (permalink / raw)
To: Tetsuo Handa
Cc: Karl Mehltretter, Robin van der Gracht, Oleksij Rempel, kernel,
Oliver Hartkopp, Marc Kleine-Budde, linux-can, linux-kernel
> list_for_each_entry(ecu, &priv->ecus, list) {
> - if (ecu->name == name)
> + if (ecu->name == name && j1939_ecu_valid(ecu))
> return ecu;
This also hides a live, named ECU from j1939_local_ecu_put() during
unregister. The netdev core sets reg_state to NETREG_UNREGISTERING before
issuing NETDEV_UNREGISTER, so j1939_ecu_valid() is false when a bound
socket reaches this cleanup path:
j1939_netdev_notifier()
j1939_sk_netdev_event_unregister()
j1939_local_ecu_put()
j1939_ecu_find_by_name_locked()
The lookup then returns NULL. j1939_local_ecu_put() warns and returns
without dropping the socket's ECU reference. That keeps the ECU's netdev
and j1939_priv references alive, preventing netdev_wait_allrefs_any()
from completing.
This is based on source review with AI assistance. I have not reproduced
the unregister hang at runtime.
Could teardown use a lookup that permits NETREG_UNREGISTERING and a dead
rx_kref, while retaining the nonzero ecu->kref check? The registration
and receive-lifetime restrictions are needed when acquiring new users,
but cleanup must still find the ECU whose reference it owns.
The ecu->kref check remains necessary because zero-reference entries stay on
the list until the RCU callback removes them.
Thanks,
Karl
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3.2] can: j1939: fix j1939_ecu refcount leak
2026-10-27 3:31 ` Karl Mehltretter
@ 2026-10-27 14:35 ` Tetsuo Handa
0 siblings, 0 replies; 4+ messages in thread
From: Tetsuo Handa @ 2026-10-27 14:35 UTC (permalink / raw)
To: Karl Mehltretter
Cc: Robin van der Gracht, Oleksij Rempel, kernel, Oliver Hartkopp,
Marc Kleine-Budde, linux-can, linux-kernel
On 2026/10/27 12:31, Karl Mehltretter wrote:
> This is based on source review with AI assistance. I have not reproduced
> the unregister hang at runtime.
>
> Could teardown use a lookup that permits NETREG_UNREGISTERING and a dead
> rx_kref, while retaining the nonzero ecu->kref check? The registration
> and receive-lifetime restrictions are needed when acquiring new users,
> but cleanup must still find the ECU whose reference it owns.
>
> The ecu->kref check remains necessary because zero-reference entries stay on
> the list until the RCU callback removes them.
Thank you. Sashiko has the same opinion
( https://sashiko.dev/#/patchset/3958917c-1484-41e8-8d62-99575edbf980%40I-love.SAKURA.ne.jp )
and I followed it in the v4 patch.
Sashiko thinks the v4 patch is OK
( https://sashiko.dev/#/patchset/4b2c8d95-d17e-4f36-a340-abf84693d24a%40I-love.SAKURA.ne.jp ).
It is time to check using multiple models via the review bot.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-10-27 14:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-25 14:54 [PATCH v3.1] can: j1939: fix j1939_ecu refcount leak Tetsuo Handa
2026-10-27 0:22 ` [PATCH v3.2] " Tetsuo Handa
2026-10-27 3:31 ` Karl Mehltretter
2026-10-27 14:35 ` Tetsuo Handa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®