🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25343

[PATCH net 1/3] can: j1939: avoid address-claim timer deadlock

#25343
From: Marc Kleine-Budde
Date: Tue, 01 Dec 2026 14:46
132 lines
14945 bytes
From: Felix Hoffmann <f3lix.dev@gmx.de>

j1939_ac_process() holds priv->lock while synchronously canceling an
ECU's address-claim hrtimer. The timer callback takes the same lock. If the
callback starts on another CPU after the receive path takes the lock, the
callback waits for priv->lock while hrtimer_cancel() waits for the callback
to finish. This deadlocks both CPUs and makes the system unresponsive.

Replace the callback's blocking lock acquisition with write_trylock(). If
the lock is busy, move the expiry forward and restart the timer. This lets
a concurrent hrtimer_cancel() observe the callback finish and remove the
requeued timer. Without a cancellation, address mapping is retried shortly.

The failed write trylock does not necessarily mean that the address-claim
writer owns priv->lock. Receive processing also takes its read side for
other frames, and an active reader prevents write_trylock() from
succeeding. Document both the address-claim writer and receive-path readers
as possible sources of contention.

Name the 1 ms retry interval J1939_ECU_TIMER_RETRY_DELAY_MS instead of
using a literal in the callback. Track consecutive failures per ECU, reset
the counter when its timer starts, and emit a warning guarded by
net_ratelimit() on the fifth retry. Five is a pragmatic threshold for
detecting unusual delay; it is not specified by J1939.

The timer uses HRTIMER_MODE_REL_SOFT, so its callback runs from
HRTIMER_SOFTIRQ with bottom halves already disabled. write_trylock() does
not change bottom-half state and must therefore pair with write_unlock() on
success. The write_lock_bh()/write_unlock_bh() pair is unnecessary in this
context.

This patch was created with LLM assistance.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Felix Hoffmann <f3lix.dev@gmx.de>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://patch.msgid.link/20260803101503.21023-1-f3lix.dev@gmx.de
[mkl: convert Assisted-by: tag into prose]
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/j1939/bus.c        | 34 ++++++++++++++++++++++++++++++++--
 net/can/j1939/j1939-priv.h |  1 +
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index cdc3c0a71937..eb9a3fd847f4 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -10,10 +10,19 @@
  * Since rtnetlink, no real bus is used.
  */
 
+#include <linux/net.h>
+
 #include <net/sock.h>
 
 #include "j1939-priv.h"
 
+#define J1939_ECU_TIMER_RETRY_DELAY_MS 1
+/*
+ * J1939 does not specify a lock-contention retry count. Five retries are a
+ * pragmatic threshold for warning about an unusual address-claim delay.
+ */
+#define J1939_ECU_TIMER_RETRY_WARN 5
+
 static void __j1939_ecu_release(struct kref *kref)
 {
 	struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
@@ -109,6 +118,8 @@ void j1939_ecu_unmap_all(struct j1939_priv *priv)
 
 void j1939_ecu_timer_start(struct j1939_ecu *ecu)
 {
+	ecu->ac_timer_retries = 0;
+
 	/* The ECU is held here and released in the
 	 * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
 	 */
@@ -131,7 +142,26 @@ 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);
+	/*
+	 * j1939_ac_process() cancels this timer while holding priv->lock.
+	 * Don't block here, otherwise the timer and receive paths can
+	 * deadlock waiting for each other on different CPUs. Retry shortly
+	 * if the lock is held by the address claim writer or by a reader on
+	 * the receive path.
+	 */
+	if (!write_trylock(&priv->lock)) {
+		ecu->ac_timer_retries++;
+		if (ecu->ac_timer_retries == J1939_ECU_TIMER_RETRY_WARN &&
+		    net_ratelimit())
+			netdev_warn(priv->ndev,
+				    "address claim timer retried %u times due to lock contention, name: 0x%016llx\n",
+				    ecu->ac_timer_retries, ecu->name);
+
+		hrtimer_forward_now(hrtimer,
+				    ms_to_ktime(J1939_ECU_TIMER_RETRY_DELAY_MS));
+		return HRTIMER_RESTART;
+	}
+
 	/* TODO: can we test if ecu->addr is unicast before starting
 	 * the timer?
 	 */
@@ -141,7 +171,7 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
 	 * j1939_ecu_timer_start().
 	 */
 	j1939_ecu_put(ecu);
-	write_unlock_bh(&priv->lock);
+	write_unlock(&priv->lock);
 
 	return HRTIMER_NORESTART;
 }
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..5ed4f4b592e3 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -36,6 +36,7 @@ struct j1939_ecu {
 
 	/* indicates that this ecu successfully claimed @sa as its address */
 	struct hrtimer ac_timer;
+	unsigned int ac_timer_retries;
 	struct kref kref;
 	struct j1939_priv *priv;
 	netdevice_tracker priv_dev_tracker;

base-commit: 656683f2e292a8ab28b1863a9a8aa0f9417b58a5
-- 
2.53.0
Technical Details
Message-ID: <20261201145025.2264811-2-mkl@pengutronix.de>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375173-linux-kernel-netdev=dewy.ru
References: <20261201145025.2264811-1-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375173-linux-kernel-netdev=dewy.ru
From: mkl@pengutronix.de (Marc Kleine-Budde)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net 1/3] can: j1939: avoid address-claim timer deadlock
Date: Tue,  1 Dec 2026 15:46:10 +0100
Organization: Usenet Network
Message-ID: <20261201145025.2264811-2-mkl@pengutronix.de>
References: <20261201145025.2264811-1-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1740194"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:605b9eW0LcsWyvLO6oTcyoLoqFE= sha256:LP0bEPmnQnfXINMTogB0r7Qko7qF9BTpWneb6WnL+YM=
	sha1:mmL0IiSn9aLgSwSiqQEYIL27kbo= sha256:BkNaaRJ7UMQGLcBVC56s00/+OBDcR+21s6J+714s+rM=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id ADE2F1173F2; Tue,  1 Dec 2026 17:51:18 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=pengutronix.de
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id pWgExc0hD7He;
	Tue,  1 Dec 2026 17:51:15 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375173-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id E4C5E1173ED; Tue,  1 Dec 2026 17:51:06 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 86AD531B8F; Tue,  1 Dec 2026 14:50:40 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796136638;
	b=IIp8bpbBoqGTnAlUXEvx3G+ZICd3SaCfeQys6QoBmsWEpbthtn3IIX7aRfoSV4s8P+lG
	 vVSH6K1jzGQJIupWoD6UD6wAGzuLcqvKItFPcMkzIIpQTNdf59xIEAlTGTSXbPY57Fw0H
	 GSG7bZ1Ugk/bHKsCnLdEh3gfjYNGYR/3xER2chZdh6IE1CM/G8EPzLIqa1QxWycXIgS/m
	 a4/pb4ndEd2pFnGjvbi/Em+Yu3T+BfJroWc5V5tO+ESnqJiB4dqgUbXNDmpfLuXtad1ID
	 I7WEZHdmGTccAWfAApXPCw/1ZPkZKa7eS38nOmjjAjTCJXWCxFIyAmf3332qYiBaz7Q==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796136638;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=MkHJ23IPc5yCtk9abJ1Ga/TpbllhWMdzVCv+XZwh8Uc=;
	b=esUSAKi8/N8af6GIovJ/17gdnotwZmmLFprq2TG08d5HZcF9J7NXJ8lwgVfp2Ga1Q7Gb
	 LPtnk61RfBeFSExPEq7f33coMmmA2VNoveVEpNrv4WpZDqbIL23soU/y7yhJ1+TuBv+a1
	 KS/brr8K4c86mPlkGv1n+RbHsDiB+wnwEr6TyRUHgpVmdDU3sqe3ZvslxZtp6oL+nrbFM
	 Puiu81iUJ804931+zpSiaaY6+1QtGKcvYd78Ucld8tAF8hvI+iRyTNruQvH8IYbfLSlNS
	 gwgAb4BjdPnkt1r39f4iTjsORlr+7+a9PA1ibc9mHy+zB6EixpJIrImF7k35Mvme0Aw==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; 
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=IyTfOhu6;
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 938EF606A680;
	Tue,  1 Dec 2026 14:50:38 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=IyTfOhu6
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796136637;
	b=yczs3uswG3x1Rf57BSd8p5D91fcNNe7B4pkqLQaMBDvvx5F87ZZYs77i3nPzSbavpool
	 IUz8dHQJ62qtZIXZmgu2fA1SEtPSHZDZ3AXpDmm0p/Y4teWqm/ZmRbnQlOD1H7bbRcNeS
	 d5IlsmmNv4QO8VDTrruQX1z4Xy0igXT2qFeBd0rKmIpDr/4lvecEULf/glApLG9rQHfRF
	 p1QMz1747/pvVhVOySWm+jFbl0Bd5XT1uUrenxpqv4F6hcoA0C/gB32v+nlOlqERwVz1V
	 776QdaiVMPEIEZm1jx49KMsHBJnJOoJ5k1ysdvs81xtz+nqmGoonmv8k+ZdZf0RclUQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796136637;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=MkHJ23IPc5yCtk9abJ1Ga/TpbllhWMdzVCv+XZwh8Uc=;
	b=ciYrdwoXGzPprRHTWeAT7UCvUjHm7WphBRkOpyzT3OxB+YqxePvC+yboWkwjxDXZJKMY
	 C8HiRPOGhhC/h6PkUV5pMw4skOujKGherIOBBeG4Gd6Xa7SE610lm6vPlHQuhf8la6+65
	 V53aQgYf+whTaarScEFzWbX+piF+mJwYgWWC9ANUpUSTPlyPsFvy1MJx0wSVerj9h4cch
	 6Dt3XuofLU8+APoNTB7YGLwrrfKNViblcVdHurOce1nKpEa8xeUxdQDECw229Q8PrAU5Z
	 2460gWC0cLN0X4TEN6lOcLvTqCCjzOyGgJrlY9JSowQkDAjxp3IgJPNyvCBNnY4H+ig==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=IyTfOhu6;
	arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AF34606A330;
	Tue,  1 Dec 2026 14:50:37 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3AF34606A330
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de
X-Received: from pengutronix.de (p20030102af17cb00c4330b233ae7cbb8.dip0.t-ipconnect.de [IPv6:2003:102:af17:cb00:c433:b23:3ae7:cbb8])
	(Authenticated sender: mkl@pengutronix.de)
	by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id 53A2C202860;
	Tue, 01 Dec 2026 15:50:31 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1796136631;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=MkHJ23IPc5yCtk9abJ1Ga/TpbllhWMdzVCv+XZwh8Uc=;
	b=IyTfOhu6kloHAUkGIlyly4smi1PRpg4Ft6csZv9sQXw5kMK/yJYqoL22cPt+vIV0XnDtqz
	PiO7skiDjQVYc2grCN3m2yLunFRnedb4A1SL1IIFWbmRQjaG4MBczz+IaNy63450ZgwlE1
	8WvlUwZrbx9nfrLfF2JLBalWVKct+RnXk9wOxinsr4v5b/Tas+q9cZjDdOqm4JmoHRXkhD
	FhaVCPiPs8wG03Jsg3ppDoP6xOG4cnnVdeWYL6Gl3MyURIysGZEH94PmEH0fN5wMROpku8
	ZJraJSlXQyxjZ0+LIPtSxFWkHLBrHoZ4NQt1Ws/ENslGyiFOUNMRstko1HJUrw==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1796136631;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=MkHJ23IPc5yCtk9abJ1Ga/TpbllhWMdzVCv+XZwh8Uc=;
	b=WDdvGK42TftGlaErlhI9wpl33yGvl/SQnk39rv2SFM6pv+I0+4sfxRKZx1U6McKLAmD2vE
	GmhuvNfvGFCrKm2VSkIugHGlnw5SAMraOMX7R0I5LJTBNkhXw/Z55dojay5hVlb7QyAXTy
	mjhx4KtPrqcrf47c0boerypI4x69gt6op7z7U1L8xj6DN5/ls42RJJbmGAOQt6tUmM5Dp4
	LrmTKDpr3llTKIDoeE0up9dTNEmNvKwaSeZRVm8mKRV0Kc0pIFRTUnrG3Z8tx77boEDUnA
	2ehmXy7GaR9Sm/0ZVXjx5qHF5lj3YvLmIJaTUZbziPC2WmkTvCKROWQ9dZZZeQ==
X-ARC-Authentication-Results: i=1;
	ORIGINATING;
	auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de
X-ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796136631; a=rsa-sha256;
	cv=none;
	b=ZYbxZQLrPUjJiaTEa4PV+pcdsKRjXRSr23CMYGH9VZvmAEmGAKqNoSVYCA+SdmFCpnR5Gg
	np7ZZsIKCswUF2Q+EHNRlu4GiCKpyxqyebIgtCl9to1fqrQ0RqUq9HMFhtObCL307+j6yI
	U+AB2mwaJJmfOTP3i5vBnbwRlO2bYdNoApe624BYGAoak9pN77Q7hLuKkWPlGIzdJbmHTx
	YUvaqjysUmM2jupRORWdyKfJjAk+AZDxo8qDSIGPQj6SxhOKRE4ZPmGMoYzMXeyfsFV1X7
	CiMPsbZGhcVwMGMwKhEoI5V+h76CHItZDeV3p5cwFD8Vq/S9PK4s4qxwsTUjog==
X-Cc: davem@davemloft.net,
	kuba@kernel.org,
	linux-can@vger.kernel.org,
	kernel@pengutronix.de,
	Felix Hoffmann <f3lix.dev@gmx.de>,
	stable@vger.kernel.org,
	Oleksij Rempel <o.rempel@pengutronix.de>,
	Marc Kleine-Budde <mkl@pengutronix.de>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <20261201145025.2264811-1-mkl@pengutronix.de>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list