Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups
@ 2026-11-28 17:48 Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 01/22] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
                   ` (22 more replies)
  0 siblings, 23 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can
  Cc: Marc Kleine-Budde, Oliver Hartkopp, Sashiko, Zihan Xi,
	Oleksij Rempel, Wentian Ye

Patches 1-3 are the remaining CAN patches of the former lock_sock() and
memcg series, whose first patches have been applied via the net tree.
They are followed by CAN_ISOTP fixes and cleanups that have been found
while preparing the CAN XL support for CAN_ISOTP.

Patches 1-2 read the CAN_RAW and CAN_ISOTP socket options under
conf_lock, so that a concurrent setsockopt() can not lead to a mix of
old and new values in getsockopt().

Patch 3 enables the memcg accounting for the CAN allocations that can
be triggered by unprivileged users (socket structures, receivers,
CAN_BCM ops and frames, CAN_RAW/J1939 filters, the CAN_ISOTP TX buffer
for large PDUs and the J1939 per device data).

Patch 4 fixes a CAN_ISOTP socket in listen mode that sent a FC.OVFLW on
the bus. It carries a Fixes tag and applies to net as well.

Patches 5-10 improve the CAN_ISOTP socket options and timing:
- check the CAN_ISOTP_OPTS values before applying them
- add the frame transmission time (N_As/N_Ar) to the CF broadcast gap
  and to the protocol timeouts and limit it to the N_As/N_Ar timeout
- reject undefined CAN FD tx_flags and reserved STmin values
- ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING as
  documented

Patches 11-14 fix the CAN_ISOTP reception against ISO 15765-2:
- drop frames without N_PCI and FFs with an invalid length extension
- check FF_DLmin and the data length of the last CF

Patches 15-22 are cleanups in preparation of the CAN XL support for
CAN_ISOTP.

Patches 5-14 only affect invalid configurations and frames or tighten
the uAPI and therefore do not carry Fixes tags.

Changes in v2:
- can: enable memcg accounting for user triggered allocations:
  added Acked-by from Oleksij Rempel
- can: isotp: do not send FC.OVFLW in listen mode:
  moved to patch 4, describe the impact on a correct listen mode
  configuration, add Fixes tag and Cc stable
- new patch: can: isotp: add the frame transmission time to the CF
  broadcast gap (also fixes the unit of the forced stmin values in
  iso15765-2.rst)
- can: isotp: add the frame transmission time to the protocol timeouts:
  limit frame_txtime to the N_As/N_Ar timeout of 1 s from ISO 15765-2
  instead of 100 ms, document the limit in isotp.h and iso15765-2.rst
- can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS:
  state the intentional uAPI tightening, always set CANFD_FDF for
  CAN FD, document the accepted tx_flags
- can: isotp: reject CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
  replaced by "can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without
  CAN_ISOTP_RX_PADDING" which fixes check_pad() as documented instead
  of rejecting the option
- can: isotp: drop frames without N_PCI, check the minimum FF_DL, check
  the data length of the last CF: describe the impact and the reason
  for the missing Fixes tag in the commit messages
- dropped: can: isotp: ignore invalid SF and FF frames during a reception
- can: isotp: rename rx_lock as it has become state machine lock:
  fixed typo in the subject, updated the locking comment in isotp_rcv()
- can: isotp: change return type of functions to void:
  removed the trailing return statements
- can: isotp: introduce CAN_ISOTP_MIN_TX_DL:
  use it only for TX_DL checks, separate block in isotp.h
- Link to v1: https://patch.msgid.link/20261126-getsockopt-memcg-isotp-v1-0-681ef8d8c2c9@hartkopp.net

---
Oliver Hartkopp (22):
      can: raw: read socket options under conf_lock in raw_getsockopt()
      can: isotp: read socket options under conf_lock in isotp_getsockopt()
      can: enable memcg accounting for user triggered allocations
      can: isotp: do not send FC.OVFLW in listen mode
      can: isotp: check the CAN_ISOTP_OPTS values before applying them
      can: isotp: add the frame transmission time to the CF broadcast gap
      can: isotp: add the frame transmission time to the protocol timeouts
      can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
      can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
      can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
      can: isotp: drop frames without N_PCI
      can: isotp: drop FF frames with invalid length extension
      can: isotp: check the minimum FF_DL
      can: isotp: check the data length of the last CF
      can: isotp: drop inline keyword, let the compiler decide
      can: isotp: rename rx_lock as it has become state machine lock
      can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs
      can: isotp: rename variable for the required content length
      can: isotp: rename checkpad functions returning bool values
      can: isotp: change return type of functions to void
      can: isotp: rename pointer in isotp_rcv() prototype
      can: isotp: introduce CAN_ISOTP_MIN_TX_DL

 Documentation/networking/iso15765-2.rst |  27 ++-
 include/uapi/linux/can/isotp.h          |   9 +-
 net/can/af_can.c                        |  12 +-
 net/can/bcm.c                           |  10 +-
 net/can/isotp.c                         | 346 ++++++++++++++++++--------------
 net/can/j1939/main.c                    |   2 +-
 net/can/j1939/socket.c                  |  12 +-
 net/can/raw.c                           |  78 +++----
 8 files changed, 291 insertions(+), 205 deletions(-)
---
base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
change-id: 20261126-getsockopt-memcg-isotp-052433c7adda

Best regards,
--  
Oliver Hartkopp <socketcan@hartkopp.net>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 01/22] can: raw: read socket options under conf_lock in raw_getsockopt()
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 02/22] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
                   ` (21 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

raw_getsockopt() only takes conf_lock for CAN_RAW_FILTER. All other
CAN_RAW socket options are read without the mutex, while
raw_setsockopt() updates them under conf_lock. CAN_RAW_XL_VCID_OPTS is
even copied to user space directly from the socket, so a concurrent
setsockopt() can lead to a mix of old and new values.

Read the option values under conf_lock into a local variable and copy
them to user space after mutex_unlock(), like raw_setsockopt() does it
in the other direction. So no user space access happens while the
mutex is held and a stalled copy can not block raw_notify() under RTNL.

CAN_RAW_FILTER is handled in front of the switch statement, as it takes
the mutex on its own for the duplication of the filters. Its code is
not changed.

The socket options have been read without a lock before commit
"can: raw: use a private mutex instead of lock_sock()" as well, so this
is no fix. As the only visible change, a failing copy to user space for
CAN_RAW_XL_VCID_OPTS now returns the clamped option length together
with -EFAULT, like the other socket options.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/raw.c | 64 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/net/can/raw.c b/net/can/raw.c
index b69eaadc5e93..cf1770432dad 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -101,11 +101,11 @@ struct raw_sock {
 	can_err_mask_t err_mask;
 	int count;                 /* number of active filters */
 	struct can_filter dfilter; /* default/single filter */
 	struct can_filter *filter; /* pointer to filter(s) */
 	struct uniqframe __percpu *uniq;
-	struct mutex conf_lock; /* protects bind, filters and setsockopt */
+	struct mutex conf_lock; /* protects bind, filters and sockopts */
 };
 
 static LIST_HEAD(raw_notifier_list);
 static DEFINE_SPINLOCK(raw_notifier_lock);
 static struct raw_sock *raw_busy_notifier;
@@ -817,24 +817,26 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 static int raw_getsockopt(struct socket *sock, int level, int optname,
 			  sockopt_t *opt)
 {
 	struct sock *sk = sock->sk;
 	struct raw_sock *ro = raw_sk(sk);
-	int flag;
+	union {
+		can_err_mask_t err_mask;
+		int flag;
+		struct can_raw_vcid_options vcid_opts;
+	} val;
 	int len;
-	void *val;
+	int err = 0;
 
 	if (level != SOL_CAN_RAW)
 		return -EINVAL;
 	len = opt->optlen;
 	if (len < 0)
 		return -EINVAL;
 
-	switch (optname) {
-	case CAN_RAW_FILTER: {
+	if (optname == CAN_RAW_FILTER) {
 		struct can_filter *filter = NULL;
-		int err = 0;
 
 		mutex_lock(&ro->conf_lock);
 		if (ro->count > 0) {
 			int fsize = ro->count * sizeof(struct can_filter);
 
@@ -864,76 +866,76 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 
 		if (!err)
 			opt->optlen = len;
 		return err;
 	}
+
+	/* Read the option value under ro->conf_lock and copy it to user
+	 * space after mutex_unlock(), see raw_setsockopt().
+	 */
+	mutex_lock(&ro->conf_lock);
+
+	switch (optname) {
 	case CAN_RAW_ERR_FILTER:
 		if (len > sizeof(can_err_mask_t))
 			len = sizeof(can_err_mask_t);
-		val = &ro->err_mask;
+		val.err_mask = ro->err_mask;
 		break;
 
 	case CAN_RAW_LOOPBACK:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->loopback;
-		val = &flag;
+		val.flag = ro->loopback;
 		break;
 
 	case CAN_RAW_RECV_OWN_MSGS:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->recv_own_msgs;
-		val = &flag;
+		val.flag = ro->recv_own_msgs;
 		break;
 
 	case CAN_RAW_FD_FRAMES:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->fd_frames;
-		val = &flag;
+		val.flag = ro->fd_frames;
 		break;
 
 	case CAN_RAW_XL_FRAMES:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->xl_frames;
-		val = &flag;
+		val.flag = ro->xl_frames;
 		break;
 
-	case CAN_RAW_XL_VCID_OPTS: {
-		int err = 0;
-
+	case CAN_RAW_XL_VCID_OPTS:
 		/* user space buffer to small for VCID opts? */
 		if (len < sizeof(ro->raw_vcid_opts)) {
 			/* return -ERANGE and needed space in optlen */
 			err = -ERANGE;
 			opt->optlen = sizeof(ro->raw_vcid_opts);
 		} else {
-			if (len > sizeof(ro->raw_vcid_opts))
-				len = sizeof(ro->raw_vcid_opts);
-			if (copy_to_iter(&ro->raw_vcid_opts, len,
-					 &opt->iter_out) != len)
-				err = -EFAULT;
+			len = sizeof(ro->raw_vcid_opts);
+			val.vcid_opts = ro->raw_vcid_opts;
 		}
-		if (!err)
-			opt->optlen = len;
-		return err;
-	}
+		break;
+
 	case CAN_RAW_JOIN_FILTERS:
 		if (len > sizeof(int))
 			len = sizeof(int);
-		flag = ro->join_filters;
-		val = &flag;
+		val.flag = ro->join_filters;
 		break;
 
 	default:
-		return -ENOPROTOOPT;
+		err = -ENOPROTOOPT;
 	}
 
+	mutex_unlock(&ro->conf_lock);
+
+	if (err)
+		return err;
+
 	opt->optlen = len;
-	if (copy_to_iter(val, len, &opt->iter_out) != len)
+	if (copy_to_iter(&val, len, &opt->iter_out) != len)
 		return -EFAULT;
 	return 0;
 }
 
 static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 02/22] can: isotp: read socket options under conf_lock in isotp_getsockopt()
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 01/22] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
                   ` (20 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_getsockopt() copies the CAN_ISOTP socket options to user space
directly from the socket without taking conf_lock, while
isotp_setsockopt() updates them under conf_lock. For the options that
consist of several values, i.e. CAN_ISOTP_OPTS, CAN_ISOTP_RECV_FC and
CAN_ISOTP_LL_OPTS, a concurrent setsockopt() can lead to a mix of old
and new values.

Read the option values under conf_lock into a local variable and copy
them to user space after mutex_unlock(), like isotp_setsockopt() does
it in the other direction. So no user space access happens while the
mutex is held and a stalled copy can not block isotp_notify() under
RTNL.

The socket options have been read without a lock before commit
"can: isotp: use a private mutex instead of lock_sock()" as well, so
this is no fix.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 642fec500af9..98a7e489ca3e 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -180,11 +180,11 @@ struct isotp_sock {
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
 	spinlock_t rx_lock; /* protect single thread state machine */
-	struct mutex conf_lock; /* protects bind and setsockopt */
+	struct mutex conf_lock; /* protects bind and sockopts */
 };
 
 static LIST_HEAD(isotp_notifier_list);
 static DEFINE_SPINLOCK(isotp_notifier_lock);
 static struct isotp_sock *isotp_busy_notifier;
@@ -1860,53 +1860,69 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
 static int isotp_getsockopt(struct socket *sock, int level, int optname,
 			    char __user *optval, int __user *optlen)
 {
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
+	union {
+		struct can_isotp_options opt;
+		struct can_isotp_fc_options rxfc;
+		struct can_isotp_ll_options ll;
+		u32 stmin;
+	} val;
 	int len;
-	void *val;
+	int err = 0;
 
 	if (level != SOL_CAN_ISOTP)
 		return -EINVAL;
 	if (get_user(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
 
+	/* Read the option value under so->conf_lock and copy it to user
+	 * space after mutex_unlock(), see isotp_setsockopt().
+	 */
+	mutex_lock(&so->conf_lock);
+
 	switch (optname) {
 	case CAN_ISOTP_OPTS:
 		len = min_t(int, len, sizeof(struct can_isotp_options));
-		val = &so->opt;
+		val.opt = so->opt;
 		break;
 
 	case CAN_ISOTP_RECV_FC:
 		len = min_t(int, len, sizeof(struct can_isotp_fc_options));
-		val = &so->rxfc;
+		val.rxfc = so->rxfc;
 		break;
 
 	case CAN_ISOTP_TX_STMIN:
 		len = min_t(int, len, sizeof(u32));
-		val = &so->force_tx_stmin;
+		val.stmin = so->force_tx_stmin;
 		break;
 
 	case CAN_ISOTP_RX_STMIN:
 		len = min_t(int, len, sizeof(u32));
-		val = &so->force_rx_stmin;
+		val.stmin = so->force_rx_stmin;
 		break;
 
 	case CAN_ISOTP_LL_OPTS:
 		len = min_t(int, len, sizeof(struct can_isotp_ll_options));
-		val = &so->ll;
+		val.ll = so->ll;
 		break;
 
 	default:
-		return -ENOPROTOOPT;
+		err = -ENOPROTOOPT;
 	}
 
+	mutex_unlock(&so->conf_lock);
+
+	if (err)
+		return err;
+
 	if (put_user(len, optlen))
 		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
 	return 0;
 }
 
 static void isotp_notify(struct isotp_sock *so, unsigned long msg,

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 01/22] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 02/22] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:48 ` [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
                   ` (19 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can
  Cc: Marc Kleine-Budde, Sashiko, Zihan Xi, Oleksij Rempel,
	Oliver Hartkopp

Unprivileged users can create CAN sockets and trigger allocations that
stay in the kernel for the lifetime of the socket or of the related
objects. E.g. each CAN_BCM RX_SETUP for a new CAN ID creates a bcm_op
with optional frame arrays of up to 257 CAN FD frames and a CAN
receiver, without requiring a CAN device (ifindex 0) and without a
limit for the number of ops per socket.

None of these allocations are accounted to the memory cgroup of the
process that triggers them. So a process can bypass the memory limit
of its cgroup, e.g. of a container or a systemd service, and exhaust
the memory of the whole system. The OOM killer can not attribute this
kernel memory to the responsible process either, which has nearly no
RSS, and terminates unrelated processes instead.

Enable memcg accounting for these allocations, as it has been done for
other objects that can be created by unprivileged users. This does not
introduce any new CAN specific limit and does not make assumptions
about the number of sockets, filters or BCM operations an application
needs. The CAN related kernel memory is just charged to the cgroup of
the allocating process, so that the memory limits which are already
configured for this cgroup apply to it as well. Without a configured
limit the behaviour is unchanged.

A per socket limit for the number or the memory of the BCM operations,
as suggested in the reports, does not bound the memory consumption, as
a process can open many sockets and fork. Hard-coded limits can also
break existing applications, e.g. one that monitors all 2048 SFF CAN
IDs with individual RX operations, without a possibility to adjust
them.

The following allocations are accounted:

- af_can: the CAN socket structures (e.g. struct isotp_sock contains
  two DEFAULT_MAX_PDU_SIZE buffers), the CAN receivers of all CAN
  protocols and the per-netns receive lists and statistics
- bcm: the bcm_op structures and their frames/last_frames arrays
- raw: the per-cpu uniq data and the CAN_RAW_FILTER filters
- isotp: the TX buffer for PDUs > DEFAULT_MAX_PDU_SIZE
- j1939: the SO_J1939_FILTER filters and the per device j1939_priv,
  which is created by the first bind() to a CAN device

The CAN socket structures are allocated with GFP_KERNEL_ACCOUNT in
sk_alloc() instead of creating dedicated slab caches with
proto_register(), as the CAN protocols copy socket options directly
from their socket structures to user space, which would require
usercopy whitelists for these caches.

memdup_sockptr() does not support memcg accounting, so the filters are
allocated with the GFP_USER | __GFP_NOWARN flags of memdup_sockptr()
plus __GFP_ACCOUNT and copied explicitly.

The isotp RX buffer for PDUs > DEFAULT_MAX_PDU_SIZE is allocated in
softirq context and is not accounted. It could be charged to the memory
cgroup of the socket with set_active_memcg(), but a charge in atomic
context can not reclaim memory. With GFP_ATOMIC the charge is forced,
which allows a cgroup to exceed its limit by up to max_pdu_size per
socket. Without __GFP_HIGH the allocation fails at the limit, so the
reception of the PDU fails, and it loses the access to the atomic
reserves on systems without a memory limit as well. This buffer is only
used when the administrator has increased the max_pdu_size module
parameter. The j1939 RX sessions are allocated in softirq context
before a socket is involved and can not be attributed to a process.
The j1939 TX sessions only exist for the duration of a transfer and the
can-gw jobs require CAP_NET_ADMIN in the initial user namespace, so
they are not changed. The local j1939 ECUs created by bind() are
allocated with BH disabled and their number is limited by the number of
bound j1939 sockets, which are accounted. Temporary buffers that are
freed before the syscall returns are not accounted either.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260707-bcm_fixes-v5-0-c3c74f8d8ff7@hartkopp.net
Reported-by: Zihan Xi <zihanx@nebusec.ai>
Closes: https://lore.kernel.org/linux-can/cover.1793089479.git.zihanx@nebusec.ai/
Link: https://lore.kernel.org/linux-can/350df96b-5f69-490e-b39e-ed6e9bca4754@hartkopp.net/
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 net/can/af_can.c       | 12 +++++++-----
 net/can/bcm.c          | 10 +++++-----
 net/can/isotp.c        |  9 +++++++--
 net/can/j1939/main.c   |  2 +-
 net/can/j1939/socket.c | 12 +++++++++---
 net/can/raw.c          | 14 ++++++++++----
 6 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/net/can/af_can.c b/net/can/af_can.c
index 0f3051a61aab..6f958c2bb49d 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -153,11 +153,11 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
 		goto errout;
 	}
 
 	sock->ops = cp->ops;
 
-	sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot, kern);
+	sk = sk_alloc(net, PF_CAN, GFP_KERNEL_ACCOUNT, cp->prot, kern);
 	if (!sk) {
 		err = -ENOMEM;
 		goto errout;
 	}
 
@@ -828,18 +828,20 @@ void can_proto_unregister(const struct can_proto *cp)
 EXPORT_SYMBOL(can_proto_unregister);
 
 static int can_pernet_init(struct net *net)
 {
 	spin_lock_init(&net->can.rcvlists_lock);
-	net->can.rx_alldev_list = kzalloc_obj(*net->can.rx_alldev_list);
+	net->can.rx_alldev_list = kzalloc_obj(*net->can.rx_alldev_list,
+					      GFP_KERNEL_ACCOUNT);
 	if (!net->can.rx_alldev_list)
 		goto out;
 	spin_lock_init(&net->can.rx_alldev_list->lock);
-	net->can.pkg_stats = kzalloc_obj(*net->can.pkg_stats);
+	net->can.pkg_stats = kzalloc_obj(*net->can.pkg_stats, GFP_KERNEL_ACCOUNT);
 	if (!net->can.pkg_stats)
 		goto out_free_rx_alldev_list;
-	net->can.rcv_lists_stats = kzalloc_obj(*net->can.rcv_lists_stats);
+	net->can.rcv_lists_stats = kzalloc_obj(*net->can.rcv_lists_stats,
+					       GFP_KERNEL_ACCOUNT);
 	if (!net->can.rcv_lists_stats)
 		goto out_free_pkg_stats;
 
 	if (IS_ENABLED(CONFIG_PROC_FS)) {
 		/* the statistics are updated every second (timer triggered) */
@@ -917,11 +919,11 @@ static __init int can_init(void)
 		     offsetof(struct canfd_frame, data));
 
 	pr_info("can: controller area network core\n");
 
 	rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
-				      0, 0, NULL);
+				      0, SLAB_ACCOUNT, NULL);
 	if (!rcv_cache)
 		return -ENOMEM;
 
 	err = register_pernet_subsys(&can_pernet_ops);
 	if (err)
diff --git a/net/can/bcm.c b/net/can/bcm.c
index dc9e2890490a..f3d93a203071 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -1159,11 +1159,11 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 		kfree(new_frames);
 
 	} else {
 		/* insert new BCM operation for the given can_id */
 
-		op = kzalloc(OPSIZ, GFP_KERNEL);
+		op = kzalloc(OPSIZ, GFP_KERNEL_ACCOUNT);
 		if (!op)
 			return -ENOMEM;
 
 		spin_lock_init(&op->bcm_tx_lock);
 		op->can_id = msg_head->can_id;
@@ -1176,11 +1176,11 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 
 		/* create array for CAN frames and copy the data */
 		if (msg_head->nframes > 1) {
 			op->frames = kmalloc_array(msg_head->nframes,
 						   op->cfsiz,
-						   GFP_KERNEL);
+						   GFP_KERNEL_ACCOUNT);
 			if (!op->frames) {
 				kfree(op);
 				return -ENOMEM;
 			}
 		} else
@@ -1407,11 +1407,11 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 		 */
 		do_rx_register = (ifindex && !op->rx_reg_dev) ? 1 : 0;
 
 	} else {
 		/* insert new BCM operation for the given can_id */
-		op = kzalloc(OPSIZ, GFP_KERNEL);
+		op = kzalloc(OPSIZ, GFP_KERNEL_ACCOUNT);
 		if (!op)
 			return -ENOMEM;
 
 		spin_lock_init(&op->bcm_tx_lock);
 		spin_lock_init(&op->bcm_rx_update_lock);
@@ -1422,20 +1422,20 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 
 		if (msg_head->nframes > 1) {
 			/* create array for CAN frames and copy the data */
 			op->frames = kmalloc_array(msg_head->nframes,
 						   op->cfsiz,
-						   GFP_KERNEL);
+						   GFP_KERNEL_ACCOUNT);
 			if (!op->frames) {
 				kfree(op);
 				return -ENOMEM;
 			}
 
 			/* create and init array for received CAN frames */
 			op->last_frames = kcalloc(msg_head->nframes,
 						  op->cfsiz,
-						  GFP_KERNEL);
+						  GFP_KERNEL_ACCOUNT);
 			if (!op->last_frames) {
 				kfree(op->frames);
 				kfree(op);
 				return -ENOMEM;
 			}
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 98a7e489ca3e..3b83507a8c21 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -607,11 +607,16 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
 
 	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
 		return 1;
 
-	/* PDU size > default => try max_pdu_size */
+	/* PDU size > default => try max_pdu_size
+	 *
+	 * This buffer is not accounted to a memory cgroup: In softirq
+	 * context the charge would either have to be forced beyond the
+	 * limit of the cgroup or the reception would fail at the limit.
+	 */
 	if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
 		u8 *newbuf = kmalloc(max_pdu_size, GFP_ATOMIC);
 
 		if (newbuf) {
 			so->rx.buf = newbuf;
@@ -1192,11 +1197,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
 	wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
 
 	/* PDU size > default => try max_pdu_size */
 	if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
-		u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL);
+		u8 *newbuf = kmalloc(max_pdu_size, GFP_KERNEL_ACCOUNT);
 
 		if (newbuf) {
 			so->tx.buf = newbuf;
 			so->tx.buflen = max_pdu_size;
 		}
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index d9384cf0e356..77adb817b36c 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -126,11 +126,11 @@ static DEFINE_MUTEX(j1939_netdev_lock);
 
 static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
 {
 	struct j1939_priv *priv;
 
-	priv = kzalloc_obj(*priv);
+	priv = kzalloc_obj(*priv, GFP_KERNEL_ACCOUNT);
 	if (!priv)
 		return NULL;
 
 	rwlock_init(&priv->lock);
 	INIT_LIST_HEAD(&priv->ecus);
diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
index 50a598ef5fd4..85b7e8847c06 100644
--- a/net/can/j1939/socket.c
+++ b/net/can/j1939/socket.c
@@ -714,13 +714,19 @@ static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
 			if (optlen > J1939_FILTER_MAX *
 			    sizeof(struct j1939_filter))
 				return -EINVAL;
 
 			count = optlen / sizeof(*filters);
-			filters = memdup_sockptr(optval, optlen);
-			if (IS_ERR(filters))
-				return PTR_ERR(filters);
+			filters = kmalloc(optlen, GFP_USER | __GFP_NOWARN |
+					  __GFP_ACCOUNT);
+			if (!filters)
+				return -ENOMEM;
+
+			if (copy_from_sockptr(filters, optval, optlen)) {
+				kfree(filters);
+				return -EFAULT;
+			}
 
 			for (f = filters, c = count; c; f++, c--) {
 				f->name &= f->name_mask;
 				f->pgn &= f->pgn_mask;
 				f->addr &= f->addr_mask;
diff --git a/net/can/raw.c b/net/can/raw.c
index cf1770432dad..da462e9874f1 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -416,11 +416,11 @@ static int raw_init(struct sock *sk)
 	ro->fd_frames        = 0;
 	ro->xl_frames        = 0;
 	ro->join_filters     = 0;
 
 	/* alloc_percpu provides zero'ed memory */
-	ro->uniq = alloc_percpu(struct uniqframe);
+	ro->uniq = alloc_percpu_gfp(struct uniqframe, GFP_KERNEL_ACCOUNT);
 	if (unlikely(!ro->uniq))
 		return -ENOMEM;
 
 	sk->sk_destruct = raw_sock_destruct;
 
@@ -761,13 +761,19 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 
 		count = optlen / sizeof(struct can_filter);
 
 		if (count > 1) {
 			/* filter does not fit into dfilter => alloc space */
-			filter = memdup_sockptr(optval, optlen);
-			if (IS_ERR(filter))
-				return PTR_ERR(filter);
+			filter = kmalloc(optlen, GFP_USER | __GFP_NOWARN |
+					 __GFP_ACCOUNT);
+			if (!filter)
+				return -ENOMEM;
+
+			if (copy_from_sockptr(filter, optval, optlen)) {
+				kfree(filter);
+				return -EFAULT;
+			}
 		} else if (count == 1) {
 			if (copy_from_sockptr(&sfilter, optval, sizeof(sfilter)))
 				return -EFAULT;
 			filter = &sfilter;
 		}

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (2 preceding siblings ...)
  2026-11-28 17:48 ` [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:48 ` [PATCH can-next v2 05/22] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
                   ` (18 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_rcv_ff() sends a FC with overflow status when the FF_DL exceeds
the receive buffer before CAN_ISOTP_LISTEN_MODE is checked. So a socket
in listen mode, which is intended to monitor the ISO-TP traffic
passively, sends a FC.OVFLW on the bus when a received FF exceeds its
receive buffer (max_pdu_size). This aborts the transfer between other
nodes using the same CAN IDs.

Move the listen mode check from the callers into isotp_send_fc() before
any state changes to cover all FC frames.

Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 3b83507a8c21..650d93e84c8e 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -292,10 +292,14 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 	struct can_skb_ext *csx;
 	struct canfd_frame *ncf;
 	struct isotp_sock *so = isotp_sk(sk);
 	int can_send_ret;
 
+	/* no creation of flow control frames in listen mode */
+	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
+		return 0;
+
 	nskb = alloc_skb(so->ll.mtu, gfp_any());
 	if (!nskb)
 		return 1;
 
 	csx = can_skb_ext_add(nskb);
@@ -641,14 +645,10 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 
 	/* start rx timeout, isotp_send_fc() restarts it */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
-	/* no creation of flow control frames */
-	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
-
 	/* send our first FC frame */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 	return 0;
 }
 
@@ -742,14 +742,10 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* perform blocksize handling, if enabled */
 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
 		return 0;
 
-	/* no creation of flow control frames */
-	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
-
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 	return 0;
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 05/22] can: isotp: check the CAN_ISOTP_OPTS values before applying them
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (3 preceding siblings ...)
  2026-11-28 17:48 ` [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-28 17:48 ` [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
                   ` (17 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_setsockopt_locked() copies the CAN_ISOTP_OPTS values into so->opt
before it checks them. When CAN_ISOTP_SF_BROADCAST and
CAN_ISOTP_CF_BROADCAST are set together, CAN_ISOTP_CF_BROADCAST is
removed and the remaining options are applied, although -EINVAL is
returned to give user feedback on the wrong configuration.

Since commit "can: isotp: use a private mutex instead of lock_sock()"
the option values are copied from user space into a local buffer before
conf_lock is taken. Check the broadcast flags in this copy and reject an
invalid combination before so->opt is changed. A rejected setsockopt()
now leaves all socket options untouched instead of applying a modified
set of options.

Use hweight32() to check that only one of the broadcast flags is set, so
that further broadcast flags can be added to ISOTP_ALL_BC_FLAGS without
changing the check.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 650d93e84c8e..8817f0ef7fd9 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1738,34 +1738,32 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 	if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
 	    READ_ONCE(so->rx.state) != ISOTP_IDLE)
 		return -EAGAIN;
 
 	switch (optname) {
-	case CAN_ISOTP_OPTS:
-		memcpy(&so->opt, val, sizeof(so->opt));
+	case CAN_ISOTP_OPTS: {
+		const struct can_isotp_options *opt = val;
+
+		/* only one broadcast flag at a time */
+		if (hweight32(opt->flags & ISOTP_ALL_BC_FLAGS) > 1)
+			return -EINVAL;
+
+		memcpy(&so->opt, opt, sizeof(*opt));
 
 		/* no separate rx_ext_address is given => use ext_address */
 		if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
 			so->opt.rx_ext_address = so->opt.ext_address;
 
-		/* these broadcast flags are not allowed together */
-		if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) {
-			/* CAN_ISOTP_SF_BROADCAST is prioritized */
-			so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
-
-			/* give user feedback on wrong config attempt */
-			ret = -EINVAL;
-		}
-
 		/* check for frame_txtime changes (0 => no changes) */
 		if (so->opt.frame_txtime) {
 			if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
 				so->frame_txtime = 0;
 			else
 				so->frame_txtime = so->opt.frame_txtime;
 		}
 		break;
+	}
 
 	case CAN_ISOTP_RECV_FC:
 		memcpy(&so->rxfc, val, sizeof(so->rxfc));
 		break;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (4 preceding siblings ...)
  2026-11-28 17:48 ` [PATCH can-next v2 05/22] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:48 ` [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts Oliver Hartkopp
                   ` (16 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

With flow control the gap between the consecutive frames is the frame
transmission time (N_As) plus the minimum separation time (N_Cs). N_Cs is
the STmin value from the received FC or the value of the
CAN_ISOTP_TX_STMIN socket option when the CAN_ISOTP_FORCE_TXSTMIN flag is
set.

In CAN_ISOTP_CF_BROADCAST mode there is no FC and the gap is set in
isotp_sendmsg(). With the CAN_ISOTP_FORCE_TXSTMIN flag only the value of
the CAN_ISOTP_TX_STMIN socket option is used and the frame transmission
time is missing.

Add the frame transmission time to the CF broadcast gap in both cases
like in the flow control mode and document the gap in iso15765-2.rst.

Additionally fix the unit of the values of the CAN_ISOTP_TX_STMIN and
CAN_ISOTP_RX_STMIN socket options in iso15765-2.rst. Both values are
handled in nanoseconds by the implementation, as documented in isotp.h.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 9 ++++++---
 net/can/isotp.c                         | 8 ++++----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 37ebb2c417cb..badcd16c070b 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -197,11 +197,14 @@ where the ``can_isotp_options`` structure has the following contents:
 
   * ``CAN_ISOTP_SF_BROADCAST``: use 1-to-N functional addressing (cannot be
     specified alongside ``CAN_ISOTP_CF_BROADCAST``).
 
   * ``CAN_ISOTP_CF_BROADCAST``: use 1-to-N transmission without flow control
-    (cannot be specified alongside ``CAN_ISOTP_SF_BROADCAST``).
+    (cannot be specified alongside ``CAN_ISOTP_SF_BROADCAST``). The gap
+    between the consecutive frames is ``frame_txtime`` plus the value of the
+    ``CAN_ISOTP_TX_STMIN`` socket option when the ``CAN_ISOTP_FORCE_TXSTMIN``
+    flag is set.
     NOTE: this is not covered by the ISO 15765-2 standard.
 
   * ``CAN_ISOTP_DYN_FC_PARMS``: enable dynamic update of flow control
     parameters.
 
@@ -284,11 +287,11 @@ where the ``can_isotp_ll_options`` structure has the following contents:
 
 Transmission stmin
 ~~~~~~~~~~~~~~~~~~
 
 The transmission minimum separation time (stmin) can be forced using the
-``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in microseconds as
+``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in nanoseconds as
 a 32bit unsigned integer; this will overwrite the value sent by the receiver in
 flow control frames:
 
 .. code-block:: C
 
@@ -297,11 +300,11 @@ flow control frames:
 
 Reception stmin
 ~~~~~~~~~~~~~~~
 
 The reception minimum separation time (stmin) can be forced using the
-``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in microseconds as
+``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in nanoseconds as
 a 32bit unsigned integer; received Consecutive Frames (CF) which timestamps
 differ less than this value will be ignored:
 
 .. code-block:: C
 
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 8817f0ef7fd9..7b88b3ace919 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1290,15 +1290,15 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		/* send first frame */
 
 		isotp_create_fframe(cf, so, ae);
 
 		if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) {
-			/* set timer for FC-less operation (STmin = 0) */
+			/* set timer for FC-less operation (N_As + N_Cs) */
+			so->tx_gap = ktime_set(0, so->frame_txtime);
 			if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
-				so->tx_gap = ktime_set(0, so->force_tx_stmin);
-			else
-				so->tx_gap = ktime_set(0, so->frame_txtime);
+				so->tx_gap = ktime_add_ns(so->tx_gap,
+							  so->force_tx_stmin);
 
 			/* disable wait for FCs due to activated block size */
 			so->txfc.bs = 0;
 
 			/* set CF echo tag for isotp_rcv_echo() (CF-mode) */

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (5 preceding siblings ...)
  2026-11-28 17:48 ` [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
@ 2026-11-28 17:48 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:49 ` [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
                   ` (15 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:48 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Wentian Ye, Oliver Hartkopp

The frame_txtime element of struct can_isotp_options is documented as
the frame transmission time (N_As/N_Ar). It is added to the gap between
the consecutive frames in the tx path, but it is missing when the
timeouts are started after the transmission of a CAN frame:

- isotp_send_fc() starts the rx timeout for the next consecutive frame
  without the transmission time of the flow control frame (N_Ar)
- isotp_sendmsg() starts the timeout for the first flow control frame
  without the transmission time of the first frame (N_As)

Add so->frame_txtime to both timeouts to comply with the documentation
in isotp.h. The local echo timeout for single frames and first frames
without flow control uses the same timer start in isotp_sendmsg() and
is extended by the frame transmission time as well.

ISO 15765-2 defines a timeout of 1000 ms for N_As/N_Ar, i.e. for the
transmission of a CAN frame on the sender and the receiver side. So far
frame_txtime accepted any value up to 0xFFFFFFFE ns (about 4.3 s). Reject
frame_txtime values above the N_As/N_Ar timeout of 1 s with -EINVAL
before the options are applied and document the limit in isotp.h and
iso15765-2.rst. Larger gaps between the consecutive frames can be
configured with the CAN_ISOTP_TX_STMIN socket option and the
CAN_ISOTP_FORCE_TXSTMIN flag.

There is no separate timer for N_As/N_Ar, as the frame transmission
time is covered by the protocol timeout that is started after the
transmission of the CAN frame.

Reported-by: Wentian Ye <yewentian395@gmail.com>
Closes: https://lore.kernel.org/linux-can/eb5e9618-3dbf-42cb-a635-752ff9fbcb99@hartkopp.net/
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 12 ++++++++----
 include/uapi/linux/can/isotp.h          |  1 +
 net/can/isotp.c                         | 11 +++++++++--
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index badcd16c070b..1962209f2db5 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -206,14 +206,18 @@ where the ``can_isotp_options`` structure has the following contents:
     NOTE: this is not covered by the ISO 15765-2 standard.
 
   * ``CAN_ISOTP_DYN_FC_PARMS``: enable dynamic update of flow control
     parameters.
 
-* ``frame_txtime``: frame transmission time (defined as N_As/N_Ar inside the
-  ISO standard); if ``0``, the default (or the last set value) is used.
-  To set the transmission time to ``0``, the ``CAN_ISOTP_FRAME_TXTIME_ZERO``
-  macro (equal to 0xFFFFFFFF) shall be used.
+* ``frame_txtime``: frame transmission time in nanoseconds (defined as
+  N_As/N_Ar inside the ISO standard); if ``0``, the default (or the last set
+  value) is used. To set the transmission time to ``0``, the
+  ``CAN_ISOTP_FRAME_TXTIME_ZERO`` macro (equal to 0xFFFFFFFF) shall be used.
+  The frame transmission time is limited to the N_As/N_Ar timeout of 1 s
+  (1000000000 ns) defined in the ISO standard. It is added to the gap between
+  the consecutive frames and to the protocol timeouts that are started after
+  the transmission of a CAN frame.
 
 * ``ext_address``: extended addressing byte, used if the
   ``CAN_ISOTP_EXTEND_ADDR`` flag is specified.
 
 * ``txpad_content``: byte used as padding value for transmitted frames.
diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index bd990917f7c4..a2e5aa6f9e82 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -72,10 +72,11 @@ struct can_isotp_options {
 	__u32 flags;		/* set flags for isotp behaviour.	*/
 				/* __u32 value : flags see below	*/
 
 	__u32 frame_txtime;	/* frame transmission time (N_As/N_Ar)	*/
 				/* __u32 value : time in nano secs	*/
+				/* max. 1 s (N_As/N_Ar timeout)		*/
 
 	__u8  ext_address;	/* set address for extended addressing	*/
 				/* __u8 value : extended address	*/
 
 	__u8  txpad_content;	/* set content of padding byte (tx)	*/
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 7b88b3ace919..ce541e57d197 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -125,10 +125,11 @@ MODULE_PARM_DESC(max_pdu_size, "maximum isotp pdu size (default "
 #define ISOTP_FC_WT 1		/* wait */
 #define ISOTP_FC_OVFLW 2	/* overflow */
 
 #define ISOTP_FC_TIMEOUT 1	/* 1 sec */
 #define ISOTP_ECHO_TIMEOUT 2	/* 2 secs */
+#define ISOTP_MAX_FRAME_TXTIME_NS NSEC_PER_SEC /* N_As/N_Ar timeout */
 
 /* so->tx_result[so->tx_gen % ISOTP_TX_RESULT_SLOTS] holds the packed value
  * (err << ISOTP_TX_RESULT_GEN_BITS | gen) for each tx generation slot, so it
  * can be handled with a single READ_ONCE()/WRITE_ONCE() access.
  */
@@ -351,11 +352,12 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 
 	/* reset last CF frame rx timestamp for rx stmin enforcement */
 	so->lastrxcf_tstamp = ktime_set(0, 0);
 
 	/* start rx timeout watchdog */
-	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+	hrtimer_start(&so->rxtimer,
+		      ktime_set(ISOTP_FC_TIMEOUT, so->frame_txtime),
 		      HRTIMER_MODE_REL_SOFT);
 	return 0;
 }
 
 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
@@ -1327,11 +1329,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		wake_up_interruptible(&so->wait);
 		return -EADDRNOTAVAIL;
 	}
 	/* WAIT_FIRST_FC for standard FF, else stays ISOTP_SENDING */
 	WRITE_ONCE(so->tx.state, new_state);
-	hrtimer_start(tx_hrt, ktime_set(hrtimer_sec, 0),
+	hrtimer_start(tx_hrt, ktime_set(hrtimer_sec, so->frame_txtime),
 		      HRTIMER_MODE_REL_SOFT);
 	spin_unlock_bh(&so->rx_lock);
 
 	/* send the first or only CAN frame */
 	cf->flags = so->ll.tx_flags;
@@ -1745,10 +1747,15 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 
 		/* only one broadcast flag at a time */
 		if (hweight32(opt->flags & ISOTP_ALL_BC_FLAGS) > 1)
 			return -EINVAL;
 
+		/* frame_txtime must not exceed the N_As/N_Ar timeout */
+		if (opt->frame_txtime != CAN_ISOTP_FRAME_TXTIME_ZERO &&
+		    opt->frame_txtime > ISOTP_MAX_FRAME_TXTIME_NS)
+			return -EINVAL;
+
 		memcpy(&so->opt, opt, sizeof(*opt));
 
 		/* no separate rx_ext_address is given => use ext_address */
 		if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
 			so->opt.rx_ext_address = so->opt.ext_address;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (6 preceding siblings ...)
  2026-11-28 17:48 ` [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:49 ` [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
                   ` (14 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The tx_flags of struct can_isotp_ll_options are set into the flags of
the created CAN FD frames. For CAN CC they have to be zero but for CAN
FD any value was accepted and copied into the CAN FD frames, including
CANFD_ESI and undefined flags.

For the ISO-TP transmission only the bit rate switch (CANFD_BRS) is a
relevant property of the CAN FD frames. The error state indicator
(CANFD_ESI) reflects the error state of the transmitting CAN controller
and is no transmission parameter of the ISO-TP protocol.

This is an intentional tightening of the uAPI and no bug fix: accept
CANFD_BRS and the CANFD_FDF marker (which is set by the CAN core anyway)
and reject other tx_flags with -EINVAL. CAN FD frames with CANFD_ESI
can still be sent with CAN_RAW sockets, e.g. for testing purposes.

The CAN core sets the CANFD_FDF marker in the CAN FD frames when they
are sent. Add CANFD_FDF to the stored tx_flags for CAN FD already in
setsockopt(), so that getsockopt() returns the flags of the created CAN
FD frames. The CAN frames on the bus are not changed by this.

Document the accepted tx_flags in isotp.h and iso15765-2.rst.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 Documentation/networking/iso15765-2.rst | 6 +++++-
 include/uapi/linux/can/isotp.h          | 2 ++
 net/can/isotp.c                         | 9 +++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
index 1962209f2db5..1cbe488b6f28 100644
--- a/Documentation/networking/iso15765-2.rst
+++ b/Documentation/networking/iso15765-2.rst
@@ -285,11 +285,15 @@ where the ``can_isotp_ll_options`` structure has the following contents:
 * ``tx_dl``: maximum payload length for transmitted frames, can have one value
   among: 8, 12, 16, 20, 24, 32, 48, 64. Values above 8 only apply to CAN FD
   traffic (i.e.: ``mtu = CANFD_MTU``).
 
 * ``tx_flags``: flags set into ``struct canfd_frame.flags`` at frame creation.
-  Only applies to CAN FD traffic (i.e.: ``mtu = CANFD_MTU``).
+  Only applies to CAN FD traffic (i.e.: ``mtu = CANFD_MTU``) and has to be
+  ``0`` for classical CAN frames. Only the ``CANFD_BRS`` flag (and the
+  ``CANFD_FDF`` marker) can be set, other flags are rejected with ``-EINVAL``.
+  The ``CANFD_FDF`` marker is always added for CAN FD traffic, so that
+  ``getsockopt()`` returns the flags of the created CAN FD frames.
 
 Transmission stmin
 ~~~~~~~~~~~~~~~~~~
 
 The transmission minimum separation time (stmin) can be forced using the
diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index a2e5aa6f9e82..eb8b5298f7a5 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -117,10 +117,12 @@ struct can_isotp_ll_options {
 				/* __u8 value : 8,12,16,20,24,32,48,64	*/
 				/* => rx path supports all LL_DL values */
 
 	__u8  tx_flags;		/* set into struct canfd_frame.flags	*/
 				/* at frame creation: e.g. CANFD_BRS	*/
+				/* only CANFD_BRS and CANFD_FDF allowed */
+				/* CANFD_FDF is always set for CAN FD	*/
 				/* Obsolete when the BRS flag is fixed	*/
 				/* by the CAN netdriver configuration	*/
 };
 
 /* flags for isotp behaviour */
diff --git a/net/can/isotp.c b/net/can/isotp.c
index ce541e57d197..ea3790a85582 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1794,12 +1794,21 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 
 		if (ll->mtu == CAN_MTU &&
 		    (ll->tx_dl > CAN_MAX_DLEN || ll->tx_flags != 0))
 			return -EINVAL;
 
+		/* only CANFD_BRS (and the CANFD_FDF marker) for CAN FD frames */
+		if (ll->mtu == CANFD_MTU &&
+		    (ll->tx_flags & ~(CANFD_BRS | CANFD_FDF)))
+			return -EINVAL;
+
 		memcpy(&so->ll, ll, sizeof(*ll));
 
+		/* CAN FD frames are always marked with CANFD_FDF */
+		if (so->ll.mtu == CANFD_MTU)
+			so->ll.tx_flags |= CANFD_FDF;
+
 		/* set ll_dl for tx path to similar place as for rx */
 		so->tx.ll_dl = ll->tx_dl;
 		break;
 	}
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (7 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:49 ` [PATCH can-next v2 10/22] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
                   ` (13 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The STmin value of struct can_isotp_fc_options is sent in the flow
control frames of the receiver. ISO 15765-2 reserves the values 0x80 -
0xF0 and 0xFA - 0xFF, but CAN_ISOTP_RECV_FC accepted any value and the
reserved values were sent on the bus. The sender side replaces received
reserved values with 0x7F already.

Reject the reserved STmin values with -EINVAL before the options are
applied.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index ea3790a85582..22bfc1c7b131 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1768,13 +1768,21 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
 				so->frame_txtime = so->opt.frame_txtime;
 		}
 		break;
 	}
 
-	case CAN_ISOTP_RECV_FC:
-		memcpy(&so->rxfc, val, sizeof(so->rxfc));
+	case CAN_ISOTP_RECV_FC: {
+		const struct can_isotp_fc_options *fc = val;
+
+		/* reject the reserved STmin values 0x80 - 0xF0, 0xFA - 0xFF */
+		if (fc->stmin > 0x7F &&
+		    (fc->stmin < 0xF1 || fc->stmin > 0xF9))
+			return -EINVAL;
+
+		memcpy(&so->rxfc, fc, sizeof(*fc));
 		break;
+	}
 
 	case CAN_ISOTP_TX_STMIN:
 		so->force_tx_stmin = *(const u32 *)val;
 		break;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 10/22] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (8 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI Oliver Hartkopp
                   ` (12 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

CAN_ISOTP_CHK_PAD_DATA checks the padding bytes of received frames
against the rxpad_content, which is only defined together with
CAN_ISOTP_RX_PADDING. iso15765-2.rst documents that CAN_ISOTP_CHK_PAD_DATA
is ignored when CAN_ISOTP_RX_PADDING is not specified.

Together with CAN_ISOTP_CHK_PAD_LEN the flag is ignored as documented and
only the optimized frame length is checked. But when
CAN_ISOTP_CHK_PAD_DATA is set without CAN_ISOTP_RX_PADDING and without
CAN_ISOTP_CHK_PAD_LEN, check_pad() does not ignore the flag but rejects
every received frame: SFs and the last CF of a PDU are dropped with
EBADMSG and every FC aborts the transmission with EBADMSG.

Ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING in this case
as well, as documented. This only affects a misconfiguration that has
never been able to receive any PDU. Therefore the patch has no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 22bfc1c7b131..b237b1fb9d99 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -424,12 +424,12 @@ static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
 	/* no RX_PADDING value => check length of optimized frame length */
 	if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
 		if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
 			return check_optimized(cf, start_index);
 
-		/* no valid test against empty value => ignore frame */
-		return 1;
+		/* no rx padding content => ignore CAN_ISOTP_CHK_PAD_DATA */
+		return 0;
 	}
 
 	/* check datalength of correctly padded CAN frame */
 	if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
 	    cf->len != padlen(cf->len))

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (9 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 10/22] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:49 ` [PATCH can-next v2 12/22] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
                   ` (11 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

isotp_rcv() reads the address extension and the N_PCI without checking
the CAN frame data length. So frames with CAN_DL < ae + N_PCI_SZ are
processed with the data bytes beyond CAN_DL, e.g. as SF, CF or FC.

As the data of the CAN frame structure is always present, the reads stay
within the skb. But with the zero-filled data from CAN drivers a frame
with CAN_DL 0 was processed as SF with SF_DL 0, which terminated an
ongoing reception.

Drop these frames as they are no valid N_PDUs. As only invalid frames
are affected, there is no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index b237b1fb9d99..798c1b4c6f6a 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -773,10 +773,14 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		return;
 	}
 
 	cf = (struct canfd_frame *)skb->data;
 
+	/* frames without N_PCI (and AE) are no N_PDUs */
+	if (cf->len < ae + N_PCI_SZ)
+		return;
+
 	/* if enabled: check reception of my configured extended address */
 	if (ae && cf->data[0] != so->opt.rx_ext_address)
 		return;
 
 	n_pci_type = cf->data[ae] & 0xF0;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 12/22] can: isotp: drop FF frames with invalid length extension
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (10 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 13/22] can: isotp: check the minimum FF_DL Oliver Hartkopp
                   ` (10 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The FF data length usually consists of 12 bits for values up to 4095 byte.
ISO 15765-2:2016 introduced an escape sequence where the 32 bit length
information is retrieved from the following 4 byte when the FF data length
is set to the reserved value 0.

This escape sequence is only allowed for FF data length values greater than
4095 and therefore PDUs with length values that would also fit into the 12
bit value representation shall be ignored according to ISO 15765-2:2016.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 798c1b4c6f6a..e73e996752af 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -605,10 +605,14 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		so->rx.len = cf->data[ae + 2] << 24;
 		so->rx.len += cf->data[ae + 3] << 16;
 		so->rx.len += cf->data[ae + 4] << 8;
 		so->rx.len += cf->data[ae + 5];
 		ff_pci_sz = FF_PCI_SZ32;
+
+		/* unneeded escape sequence => ignore PDU according to spec */
+		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
+			return 1;
 	}
 
 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
 	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 13/22] can: isotp: check the minimum FF_DL
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (11 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 12/22] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 14/22] can: isotp: check the data length of the last CF Oliver Hartkopp
                   ` (9 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

ISO 15765-2 requires FF_DL >= FF_DLmin: a PDU that fits into a SF must
not be segmented.

The rx check in isotp_rcv_ff() added a SF_DL escape offset for LL_DL > 8
on the wrong side and only rejected FF_DL < LL_DL - ae - off - ff_pci_sz.
So it did not even ensure that a CF is needed. It accepted FFs whose
FF_DL fits into the FF itself (e.g. FF_DL 6 for LL_DL 8, FF_DL 61 and 62
for LL_DL 64) and FFs whose FF_DL fits into a SF (e.g. FF_DL 7 for
LL_DL 8). In both cases the receiver sent a FC and waited for a CF that
a compliant sender never sends.

Check the FF_DL against the maximum SF_DL of the sender LL_DL like
isotp_sendmsg() does on the tx path. As only FF_DL is on the left side
the check does not wrap around for FF_DL values close to 2^32.

As only FFs from a non-compliant sender are affected and the copied FF
data stays within the receive buffer, there is no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index e73e996752af..e1e6f94ede06 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -577,12 +577,12 @@ static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
 }
 
 static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 {
 	struct isotp_sock *so = isotp_sk(sk);
+	unsigned int sf_max;
 	int i;
-	int off;
 	int ff_pci_sz;
 
 	hrtimer_cancel(&so->rxtimer);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
@@ -611,14 +611,17 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		/* unneeded escape sequence => ignore PDU according to spec */
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
 			return 1;
 	}
 
-	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
-	off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
+	/* the PDU must not fit into a SF (FF_DLmin) */
+	if (so->rx.ll_dl == CAN_MAX_DLEN)
+		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
+	else
+		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
-	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
+	if (so->rx.len <= sf_max)
 		return 1;
 
 	/* PDU size > default => try max_pdu_size
 	 *
 	 * This buffer is not accounted to a memory cgroup: In softirq

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 14/22] can: isotp: check the data length of the last CF
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (12 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 13/22] can: isotp: check the minimum FF_DL Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 15/22] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
                   ` (8 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

A CF shorter than LL_DL is only allowed as last CF and therefore has to
carry all missing bytes of the PDU. But the missing bytes were compared
with the payload of a full CF. So a short CF without all missing bytes
was accepted and the reception continued with further CFs.

Drop a short CF when the missing PDU bytes exceed its possible payload
(cf->len - ae - N_PCI_SZ). Padding is still possible as it only
increases cf->len. isotp_rcv() ensures cf->len >= ae + N_PCI_SZ, so
the payload size can not underflow.

As only CFs from a non-compliant sender are affected and the copied CF
data stays within the receive buffer, there is no Fixes tag.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index e1e6f94ede06..3c10a14495f1 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -672,12 +672,14 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 	if (cf->len > so->rx.ll_dl)
 		return 1;
 
 	/* CFs have usually the LL_DL length */
 	if (cf->len < so->rx.ll_dl) {
-		/* this is only allowed for the last CF */
-		if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
+		/* only the last CF may be shorter: drop it when the
+		 * missing PDU bytes exceed its possible payload
+		 */
+		if (so->rx.len - so->rx.idx > cf->len - ae - N_PCI_SZ)
 			return 1;
 	}
 
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
 		return 0;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 15/22] can: isotp: drop inline keyword, let the compiler decide
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (13 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 14/22] can: isotp: check the data length of the last CF Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
                   ` (7 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Modern kernel style avoids forcing inlining via the inline keyword on
static functions - the compiler heuristics decide better on their own.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 3c10a14495f1..0780a0ec9867 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -247,11 +247,11 @@ static bool isotp_tx_gen_done(struct isotp_sock *so, u32 gen)
 
 	return state == ISOTP_IDLE || state == ISOTP_SHUTDOWN ||
 	       READ_ONCE(so->tx_gen) != gen;
 }
 
-static inline struct isotp_sock *isotp_sk(const struct sock *sk)
+static struct isotp_sock *isotp_sk(const struct sock *sk)
 {
 	return (struct isotp_sock *)sk;
 }
 
 static u32 isotp_bc_flags(struct isotp_sock *so)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (14 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 15/22] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-30 20:25   ` netdev-bot+sashiko
  2026-11-28 17:49 ` [PATCH can-next v2 17/22] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
                   ` (6 subsequent siblings)
  22 siblings, 1 reply; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

rx_lock has been used to lock some receive path functionalities.
With the latest changes the semantic has been extended as a full
state machine locking primitive so give it a proper name sm_lock.

Update the comment in isotp_rcv() which still describes the locking
as not needed in real world use cases.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 57 +++++++++++++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 28 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 0780a0ec9867..1590627e34bd 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -180,11 +180,11 @@ struct isotp_sock {
 	u32 tx_gen; /* transfer generation, increased per new tx transfer */
 	u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
 	struct tpcon rx, tx;
 	struct list_head notifier;
 	wait_queue_head_t wait;
-	spinlock_t rx_lock; /* protect single thread state machine */
+	spinlock_t sm_lock; /* protect single thread state machine */
 	struct mutex conf_lock; /* protects bind and sockopts */
 };
 
 static LIST_HEAD(isotp_notifier_list);
 static DEFINE_SPINLOCK(isotp_notifier_lock);
@@ -793,14 +793,15 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		return;
 
 	n_pci_type = cf->data[ae] & 0xF0;
 
 	/* Make sure the state changes and data structures stay consistent at
-	 * CAN frame reception time. This locking is not needed in real world
-	 * use cases but the inconsistency can be triggered with syzkaller.
+	 * CAN frame reception time: so->sm_lock serializes the rx path with
+	 * the claim of a transfer in isotp_sendmsg(), with isotp_rcv_echo()
+	 * and with isotp_release().
 	 */
-	spin_lock(&so->rx_lock);
+	spin_lock(&so->sm_lock);
 
 	if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
 		/* check rx/tx path half duplex expectations */
 		if ((READ_ONCE(so->tx.state) != ISOTP_IDLE &&
 		     n_pci_type != N_PCI_FC) ||
@@ -855,11 +856,11 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 		isotp_rcv_cf(sk, cf, ae, skb);
 		break;
 	}
 
 out_unlock:
-	spin_unlock(&so->rx_lock);
+	spin_unlock(&so->sm_lock);
 }
 
 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 				 int ae, int off)
 {
@@ -1007,14 +1008,14 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 		return;
 
 	if (WARN_ON_ONCE(!csx))
 		return;
 
-	/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->rx_lock
+	/* unlike isotp_rcv_fc()/isotp_rcv_cf(), not already under so->sm_lock
 	 * (no isotp_rcv() caller here), so take it ourselves
 	 */
-	spin_lock(&so->rx_lock);
+	spin_lock(&so->sm_lock);
 
 	/* so->cfecho may since belong to a new transfer; recheck under lock */
 	if (READ_ONCE(so->cfecho) != csx->can_skb_uid)
 		goto out_unlock;
 
@@ -1022,11 +1023,11 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 	hrtimer_cancel(&so->echotimer);
 
 	/* local echo skb with consecutive frame has been consumed */
 	WRITE_ONCE(so->cfecho, 0);
 
-	/* claiming a transfer also takes so->rx_lock, so a plain recheck
+	/* claiming a transfer also takes so->sm_lock, so a plain recheck
 	 * is enough: so->tx.state can't have flipped to ISOTP_SENDING for
 	 * a new claim while we're still in here
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_SENDING)
 		goto out_unlock;
@@ -1060,17 +1061,17 @@ static void isotp_rcv_echo(struct sk_buff *skb, void *data)
 
 	/* start timer to send next consecutive frame with correct delay */
 	hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
 
 out_unlock:
-	spin_unlock(&so->rx_lock);
+	spin_unlock(&so->sm_lock);
 }
 
 /* isotp_tx_timeout: we did not get any flow control or echo frame in time
  *
  * Shared by so->txtimer's and so->echotimer's callbacks. Both timers get
- * cancelled under so->rx_lock elsewhere, so this must stay lock-free.
+ * cancelled under so->sm_lock elsewhere, so this must stay lock-free.
  *
  * tx.state is acquired before tx_gen. Common sequence in isotp_tx_gen_done().
  * cmpxchg() only orders itself, not the two preceding loads.
  */
 static enum hrtimer_restart isotp_tx_timeout(struct isotp_sock *so)
@@ -1155,19 +1156,19 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	int err;
 
 	if (!READ_ONCE(so->bound) || READ_ONCE(so->tx.state) == ISOTP_SHUTDOWN)
 		return -EADDRNOTAVAIL;
 
-	/* claim the socket under so->rx_lock: this serializes the claim
+	/* claim the socket under so->sm_lock: this serializes the claim
 	 * with the RX path and with sendmsg()'s own error paths below, so
 	 * none of them can ever see a transfer mid-claim
 	 */
 	for (;;) {
-		spin_lock_bh(&so->rx_lock);
+		spin_lock_bh(&so->sm_lock);
 		if (READ_ONCE(so->tx.state) == ISOTP_IDLE)
 			break;
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 
 		/* we do not support multiple buffers - for now */
 		if (msg->msg_flags & MSG_DONTWAIT)
 			return -EAGAIN;
 
@@ -1192,11 +1193,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	isotp_set_tx_result(so, my_gen, ECOMM); /* prevent stale slot matching */
 	WRITE_ONCE(so->tx_gen, my_gen);
 	smp_wmb(); /* see smp_load_acquire() in isotp_tx_[timeout|gen_done] */
 	WRITE_ONCE(so->tx.state, ISOTP_SENDING);
 	WRITE_ONCE(so->cfecho, 0);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 
 	/* so->bound is only checked once above - a wakeup may have
 	 * unbound/rebound the socket meanwhile => recheck
 	 */
 	if (!READ_ONCE(so->bound)) {
@@ -1329,26 +1330,26 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 			/* no CF echo tag for isotp_rcv_echo() (FF-mode) */
 			WRITE_ONCE(so->cfecho, 0);
 		}
 	}
 
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	if (READ_ONCE(so->tx.state) == ISOTP_SHUTDOWN) {
 		/* isotp_release() has since taken over and already drained
 		 * our timers - don't send into a socket that's going away
 		 */
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 		kfree_skb(skb);
 		dev_put(dev);
 		wake_up_interruptible(&so->wait);
 		return -EADDRNOTAVAIL;
 	}
 	/* WAIT_FIRST_FC for standard FF, else stays ISOTP_SENDING */
 	WRITE_ONCE(so->tx.state, new_state);
 	hrtimer_start(tx_hrt, ktime_set(hrtimer_sec, so->frame_txtime),
 		      HRTIMER_MODE_REL_SOFT);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 
 	/* send the first or only CAN frame */
 	cf->flags = so->ll.tx_flags;
 
 	skb->dev = dev;
@@ -1357,18 +1358,18 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	dev_put(dev);
 	if (err) {
 		pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
 			       __func__, ERR_PTR(err));
 
-		spin_lock_bh(&so->rx_lock);
+		spin_lock_bh(&so->sm_lock);
 
 		/* new transfer already claimed by a concurrent completion,
 		 * timeout or sendmsg() while we were stuck in can_send()?
 		 */
 		if (READ_ONCE(so->tx_gen) != my_gen) {
 			/* don't touch timers and state of the new transfer */
-			spin_unlock_bh(&so->rx_lock);
+			spin_unlock_bh(&so->sm_lock);
 			return err;
 		}
 
 		/* no transmission -> no timeout monitoring */
 		hrtimer_cancel(tx_hrt);
@@ -1407,34 +1408,34 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 
 	return size;
 
 err_out_drop:
 	/* claimed but nothing sent yet - no timer to cancel */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	goto err_out_drop_locked;
 err_event_drop:
 	/* interrupted or shut down while waiting on our own transfer */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 
 	/* new transfer already started by concurrent sendmsg()? */
 	if (READ_ONCE(so->tx_gen) != my_gen) {
 		/* don't touch timers and states of the new transfer */
-		spin_unlock_bh(&so->rx_lock);
+		spin_unlock_bh(&so->sm_lock);
 		return err;
 	}
 
 	hrtimer_cancel(&so->txfrtimer);
 	hrtimer_cancel(&so->txtimer);
 	hrtimer_cancel(&so->echotimer);
 err_out_drop_locked:
-	/* release the claim; so->rx_lock still held from above */
+	/* release the claim; so->sm_lock still held from above */
 	WRITE_ONCE(so->cfecho, 0);
 
 	/* only claim to IDLE if isotp_release() has not taken over */
 	if (READ_ONCE(so->tx.state) != ISOTP_SHUTDOWN)
 		WRITE_ONCE(so->tx.state, ISOTP_IDLE);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 	wake_up_interruptible(&so->wait);
 
 	return err;
 }
 
@@ -1500,17 +1501,17 @@ static int isotp_release(struct socket *sock)
 	while (READ_ONCE(so->tx.state) != ISOTP_IDLE &&
 	       wait_event_interruptible(so->wait,
 					READ_ONCE(so->tx.state) == ISOTP_IDLE) == 0)
 		;
 
-	/* claim the socket under so->rx_lock like sendmsg() does, so its
+	/* claim the socket under so->sm_lock like sendmsg() does, so its
 	 * claim can't race the forced ISOTP_SHUTDOWN below; force it
 	 * unconditionally, even when a signal cut the wait above short
 	 */
-	spin_lock_bh(&so->rx_lock);
+	spin_lock_bh(&so->sm_lock);
 	WRITE_ONCE(so->tx.state, ISOTP_SHUTDOWN);
-	spin_unlock_bh(&so->rx_lock);
+	spin_unlock_bh(&so->sm_lock);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 	/* forced SHUTDOWN may have skipped IDLE (gave up on a signal) */
 	wake_up_interruptible(&so->wait);
 
@@ -2098,11 +2099,11 @@ static int isotp_init(struct sock *sk)
 		      CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 	hrtimer_setup(&so->txfrtimer, isotp_txfr_timer_handler,
 		      CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 
 	init_waitqueue_head(&so->wait);
-	spin_lock_init(&so->rx_lock);
+	spin_lock_init(&so->sm_lock);
 
 	spin_lock(&isotp_notifier_lock);
 	list_add_tail(&so->notifier, &isotp_notifier_list);
 	spin_unlock(&isotp_notifier_lock);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 17/22] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (15 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 18/22] can: isotp: rename variable for the required content length Oliver Hartkopp
                   ` (5 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Move duplicated code into a separate function as preparation for the
CAN XL support which will need this functionality too.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 1590627e34bd..d41fc2c54ce0 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1568,10 +1568,27 @@ static int isotp_release(struct socket *sock)
 	sock_put(sk);
 
 	return 0;
 }
 
+static bool isotp_invalid_canid(canid_t canid)
+{
+	canid_t sanitized_id = canid;
+
+	/* sanitize CAN identifier */
+	if (sanitized_id & CAN_EFF_FLAG)
+		sanitized_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
+	else
+		sanitized_id &= CAN_SFF_MASK;
+
+	/* give feedback on wrong CAN-ID value */
+	if (sanitized_id != canid)
+		return true;
+
+	return false;
+}
+
 static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len)
 {
 	struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
@@ -1586,31 +1603,17 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
 		return -EINVAL;
 
 	if (addr->can_family != AF_CAN)
 		return -EINVAL;
 
-	/* sanitize tx CAN identifier */
-	if (tx_id & CAN_EFF_FLAG)
-		tx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
-	else
-		tx_id &= CAN_SFF_MASK;
-
-	/* give feedback on wrong CAN-ID value */
-	if (tx_id != addr->can_addr.tp.tx_id)
+	/* check for correct tx CAN identifier */
+	if (isotp_invalid_canid(tx_id))
 		return -EINVAL;
 
-	/* sanitize rx CAN identifier (if needed) */
-	if (isotp_register_rxid(so)) {
-		if (rx_id & CAN_EFF_FLAG)
-			rx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
-		else
-			rx_id &= CAN_SFF_MASK;
-
-		/* give feedback on wrong CAN-ID value */
-		if (rx_id != addr->can_addr.tp.rx_id)
-			return -EINVAL;
-	}
+	/* check for correct rx CAN identifier (if needed) */
+	if (isotp_register_rxid(so) && isotp_invalid_canid(rx_id))
+		return -EINVAL;
 
 	if (!addr->can_ifindex)
 		return -ENODEV;
 
 	/* so->conf_lock serializes the binding with isotp_notify() */

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 18/22] can: isotp: rename variable for the required content length
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (16 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 17/22] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 19/22] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
                   ` (4 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Rename the meaningless 'num' variable to 'reqlen' to simplify the review
for the CAN XL integration.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index d41fc2c54ce0..d5341441d00b 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -864,17 +864,17 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 				 int ae, int off)
 {
 	int pcilen = N_PCI_SZ + ae + off;
 	int space = so->tx.ll_dl - pcilen;
-	int num = min_t(int, so->tx.len - so->tx.idx, space);
+	int reqlen = min_t(int, so->tx.len - so->tx.idx, space);
 	int i;
 
 	cf->can_id = so->txid;
-	cf->len = num + pcilen;
+	cf->len = reqlen + pcilen;
 
-	if (num < space) {
+	if (reqlen < space) {
 		if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
 			/* user requested padding */
 			cf->len = padlen(cf->len);
 			memset(cf->data, so->opt.txpad_content, cf->len);
 		} else if (cf->len > CAN_MAX_DLEN) {
@@ -883,11 +883,11 @@ static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
 			memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
 			       cf->len);
 		}
 	}
 
-	for (i = 0; i < num; i++)
+	for (i = 0; i < reqlen; i++)
 		cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
 
 	if (ae)
 		cf->data[0] = so->opt.ext_address;
 }

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 19/22] can: isotp: rename checkpad functions returning bool values
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (17 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 18/22] can: isotp: rename variable for the required content length Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 20/22] can: isotp: change return type of functions to void Oliver Hartkopp
                   ` (3 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The checkpad functions return true when failing. Rename the functions
to reflect their functionality together with a conversion to bool
return values.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index d5341441d00b..e3c93109b004 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -393,12 +393,12 @@ static u8 padlen(u8 datalen)
 		return 64;
 
 	return plen[datalen];
 }
 
-/* check for length optimization and return 1/true when the check fails */
-static int check_optimized(struct canfd_frame *cf, int start_index)
+/* check for length optimization and return true when the check fails */
+static bool chk_optimized_fail(struct canfd_frame *cf, int start_index)
 {
 	/* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the
 	 * padding would start at this point. E.g. if the padding would
 	 * start at cf.data[7] cf->len has to be 7 to be optimal.
 	 * Note: The data[] index starts with zero.
@@ -413,37 +413,38 @@ static int check_optimized(struct canfd_frame *cf, int start_index)
 	 * point the padlen(cf->len) is always equal to cf->len.
 	 */
 	return (cf->len != padlen(start_index));
 }
 
-/* check padding and return 1/true when the check fails */
-static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
-		     int start_index, u8 content)
+/* check padding and return true when the check fails */
+static bool chk_pad_fail(struct isotp_sock *so, struct canfd_frame *cf,
+			 int start_index, u8 content)
 {
 	int i;
 
 	/* no RX_PADDING value => check length of optimized frame length */
 	if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
 		if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
-			return check_optimized(cf, start_index);
+			return chk_optimized_fail(cf, start_index);
 
 		/* no rx padding content => ignore CAN_ISOTP_CHK_PAD_DATA */
-		return 0;
+		return false;
 	}
 
 	/* check datalength of correctly padded CAN frame */
 	if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
 	    cf->len != padlen(cf->len))
-		return 1;
+		return true;
 
 	/* check padding content */
 	if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
 		for (i = start_index; i < cf->len; i++)
 			if (cf->data[i] != content)
-				return 1;
+				return true;
 	}
-	return 0;
+
+	return false;
 }
 
 static void isotp_send_cframe(struct isotp_sock *so);
 
 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
@@ -464,11 +465,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
 		return 1;
 
 	if ((cf->len < ae + FC_CONTENT_SZ) ||
 	    ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-	     check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
+	     chk_pad_fail(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 
@@ -554,11 +555,11 @@ static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
 
 	if (!len || len > cf->len - pcilen)
 		return 1;
 
 	if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-	    check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
+	    chk_pad_fail(so, cf, pcilen + len, so->opt.rxpad_content)) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 		return 1;
@@ -724,11 +725,11 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 	if (so->rx.idx >= so->rx.len) {
 		/* we are done */
 		WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 		if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
-		    check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
+		    chk_pad_fail(so, cf, i + 1, so->opt.rxpad_content)) {
 			/* malformed PDU - report 'not a data message' */
 			sk->sk_err = EBADMSG;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk_error_report(sk);
 			return 1;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 20/22] can: isotp: change return type of functions to void
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (18 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 19/22] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 21/22] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
                   ` (2 subsequent siblings)
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The return values of some functions have never been used and needed.
Make those functions void to remove unused return values.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 67 ++++++++++++++++++++++++++-------------------------------
 1 file changed, 31 insertions(+), 36 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index e3c93109b004..991d1ccd3197 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -284,37 +284,37 @@ static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer)
 	}
 
 	return HRTIMER_NORESTART;
 }
 
-static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
+static void isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 {
 	struct net_device *dev;
 	struct sk_buff *nskb;
 	struct can_skb_ext *csx;
 	struct canfd_frame *ncf;
 	struct isotp_sock *so = isotp_sk(sk);
 	int can_send_ret;
 
 	/* no creation of flow control frames in listen mode */
 	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
-		return 0;
+		return;
 
 	nskb = alloc_skb(so->ll.mtu, gfp_any());
 	if (!nskb)
-		return 1;
+		return;
 
 	csx = can_skb_ext_add(nskb);
 	if (!csx) {
 		kfree_skb(nskb);
-		return 1;
+		return;
 	}
 
 	dev = dev_get_by_index(sock_net(sk), so->ifindex);
 	if (!dev) {
 		kfree_skb(nskb);
-		return 1;
+		return;
 	}
 
 	csx->can_iif = dev->ifindex;
 	nskb->dev = dev;
 	can_skb_set_owner(nskb, sk);
@@ -355,11 +355,10 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
 
 	/* start rx timeout watchdog */
 	hrtimer_start(&so->rxtimer,
 		      ktime_set(ISOTP_FC_TIMEOUT, so->frame_txtime),
 		      HRTIMER_MODE_REL_SOFT);
-	return 0;
 }
 
 static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk)
 {
 	struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
@@ -445,27 +444,27 @@ static bool chk_pad_fail(struct isotp_sock *so, struct canfd_frame *cf,
 	return false;
 }
 
 static void isotp_send_cframe(struct isotp_sock *so);
 
-static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
+static void isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 {
 	struct sock *sk = &so->sk;
 	int tx_err = EBADMSG; /* default for unknown FC status */
 
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
-		return 0;
+		return;
 
 	hrtimer_cancel(&so->txtimer);
 
 	/* isotp_tx_timeout() may have given up on this job while
 	 * hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->tx.state) != ISOTP_WAIT_FC &&
 	    READ_ONCE(so->tx.state) != ISOTP_WAIT_FIRST_FC)
-		return 1;
+		return;
 
 	if ((cf->len < ae + FC_CONTENT_SZ) ||
 	    ((so->opt.flags & ISOTP_CHECK_PADDING) &&
 	     chk_pad_fail(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
 		/* malformed PDU - report 'not a data message' */
@@ -475,11 +474,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 
 		isotp_set_tx_result(so, so->tx_gen, EBADMSG);
 		/* set to IDLE after publishing tx_result */
 		smp_store_release(&so->tx.state, ISOTP_IDLE);
 		wake_up_interruptible(&so->wait);
-		return 1;
+		return;
 	}
 
 	/* get static/dynamic communication params from first/every FC frame */
 	if (READ_ONCE(so->tx.state) == ISOTP_WAIT_FIRST_FC ||
 	    so->opt.flags & CAN_ISOTP_DYN_FC_PARMS) {
@@ -539,47 +538,45 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 		isotp_set_tx_result(so, so->tx_gen, tx_err);
 		/* set to IDLE after publishing tx_result */
 		smp_store_release(&so->tx.state, ISOTP_IDLE);
 		wake_up_interruptible(&so->wait);
 	}
-	return 0;
 }
 
-static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
-			struct sk_buff *skb, int len)
+static void isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen,
+			 struct sk_buff *skb, int len)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	struct sk_buff *nskb;
 
 	hrtimer_cancel(&so->rxtimer);
 	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
 
 	if (!len || len > cf->len - pcilen)
-		return 1;
+		return;
 
 	if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
 	    chk_pad_fail(so, cf, pcilen + len, so->opt.rxpad_content)) {
 		/* malformed PDU - report 'not a data message' */
 		sk->sk_err = EBADMSG;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
-		return 1;
+		return;
 	}
 
 	nskb = alloc_skb(len, gfp_any());
 	if (!nskb)
-		return 1;
+		return;
 
 	memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
 
 	nskb->tstamp = skb->tstamp;
 	nskb->dev = skb->dev;
 	isotp_rcv_skb(nskb, sk);
-	return 0;
 }
 
-static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
+static void isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	unsigned int sf_max;
 	int i;
 	int ff_pci_sz;
@@ -590,11 +587,11 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	/* get the used sender LL_DL from the (first) CAN frame data length */
 	so->rx.ll_dl = padlen(cf->len);
 
 	/* the first frame has to use the entire frame up to LL_DL length */
 	if (cf->len != so->rx.ll_dl)
-		return 1;
+		return;
 
 	/* get the FF_DL */
 	so->rx.len = (cf->data[ae] & 0x0F) << 8;
 	so->rx.len += cf->data[ae + 1];
 
@@ -609,21 +606,21 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		so->rx.len += cf->data[ae + 5];
 		ff_pci_sz = FF_PCI_SZ32;
 
 		/* unneeded escape sequence => ignore PDU according to spec */
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
-			return 1;
+			return;
 	}
 
 	/* the PDU must not fit into a SF (FF_DLmin) */
 	if (so->rx.ll_dl == CAN_MAX_DLEN)
 		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
 	else
 		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
 	if (so->rx.len <= sf_max)
-		return 1;
+		return;
 
 	/* PDU size > default => try max_pdu_size
 	 *
 	 * This buffer is not accounted to a memory cgroup: In softirq
 	 * context the charge would either have to be forced beyond the
@@ -639,11 +636,11 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	}
 
 	if (so->rx.len > so->rx.buflen) {
 		/* send FC frame with overflow status */
 		isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
-		return 1;
+		return;
 	}
 
 	/* copy the first received data bytes */
 	so->rx.idx = 0;
 	for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
@@ -657,41 +654,40 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* send our first FC frame */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
-	return 0;
 }
 
-static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
-			struct sk_buff *skb)
+static void isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
+			 struct sk_buff *skb)
 {
 	struct isotp_sock *so = isotp_sk(sk);
 	struct sk_buff *nskb;
 	int i;
 
 	/* CFs are never longer than the FF */
 	if (cf->len > so->rx.ll_dl)
-		return 1;
+		return;
 
 	/* CFs have usually the LL_DL length */
 	if (cf->len < so->rx.ll_dl) {
 		/* only the last CF may be shorter: drop it when the
 		 * missing PDU bytes exceed its possible payload
 		 */
 		if (so->rx.len - so->rx.idx > cf->len - ae - N_PCI_SZ)
-			return 1;
+			return;
 	}
 
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
-		return 0;
+		return;
 
 	/* drop if timestamp gap is less than force_rx_stmin nano secs */
 	if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
 		if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
 		    so->force_rx_stmin)
-			return 0;
+			return;
 
 		so->lastrxcf_tstamp = skb->tstamp;
 	}
 
 	/* stop the rx timeout only for valid CFs (checked above) */
@@ -699,21 +695,21 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* isotp_rx_timer_handler() may have raced us for so->rx.state
 	 * while hrtimer_cancel() above waited for it to finish => recheck
 	 */
 	if (READ_ONCE(so->rx.state) != ISOTP_WAIT_DATA)
-		return 1;
+		return;
 
 	if ((cf->data[ae] & 0x0F) != so->rx.sn) {
 		/* wrong sn detected - report 'illegal byte sequence' */
 		sk->sk_err = EILSEQ;
 		if (!sock_flag(sk, SOCK_DEAD))
 			sk_error_report(sk);
 
 		/* reset rx state */
 		WRITE_ONCE(so->rx.state, ISOTP_IDLE);
-		return 1;
+		return;
 	}
 	so->rx.sn++;
 	so->rx.sn %= 16;
 
 	for (i = ae + N_PCI_SZ; i < cf->len; i++) {
@@ -730,37 +726,36 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 		    chk_pad_fail(so, cf, i + 1, so->opt.rxpad_content)) {
 			/* malformed PDU - report 'not a data message' */
 			sk->sk_err = EBADMSG;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk_error_report(sk);
-			return 1;
+			return;
 		}
 
 		nskb = alloc_skb(so->rx.len, gfp_any());
 		if (!nskb)
-			return 1;
+			return;
 
 		memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
 		       so->rx.len);
 
 		nskb->tstamp = skb->tstamp;
 		nskb->dev = skb->dev;
 		isotp_rcv_skb(nskb, sk);
-		return 0;
+		return;
 	}
 
 	/* start rx timeout, isotp_send_fc() restarts it */
 	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
 		      HRTIMER_MODE_REL_SOFT);
 
 	/* perform blocksize handling, if enabled */
 	if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
-		return 0;
+		return;
 
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
-	return 0;
 }
 
 static void isotp_rcv(struct sk_buff *skb, void *data)
 {
 	struct sock *sk = (struct sock *)data;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 21/22] can: isotp: rename pointer in isotp_rcv() prototype
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (19 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 20/22] can: isotp: change return type of functions to void Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-28 17:49 ` [PATCH can-next v2 22/22] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp
  2026-11-30  8:57 ` [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

Preparation for CAN XL support where a 'data' pointer is commonly used.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/isotp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 991d1ccd3197..2a74a1ccb1a6 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -754,13 +754,13 @@ static void isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
 
 	/* we reached the specified blocksize so->rxfc.bs */
 	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
 }
 
-static void isotp_rcv(struct sk_buff *skb, void *data)
+static void isotp_rcv(struct sk_buff *skb, void *skdata)
 {
-	struct sock *sk = (struct sock *)data;
+	struct sock *sk = (struct sock *)skdata;
 	struct isotp_sock *so = isotp_sk(sk);
 	struct canfd_frame *cf;
 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
 	u8 n_pci_type, sf_dl;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH can-next v2 22/22] can: isotp: introduce CAN_ISOTP_MIN_TX_DL
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (20 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 21/22] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
@ 2026-11-28 17:49 ` Oliver Hartkopp
  2026-11-30  8:57 ` [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-28 17:49 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Oliver Hartkopp

The minimum TX_DL value is 8 and the CAN_MAX_DLEN is also 8.
To not mix the intention and context of the defines introduce a separate
CAN_ISOTP_MIN_TX_DL definition to perform TX_DL related checks.

Use CAN_ISOTP_MIN_TX_DL where a TX_DL or LL_DL value is checked and keep
CAN_MAX_DLEN for the checks of the CAN_DL of a CAN frame.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 include/uapi/linux/can/isotp.h | 6 +++++-
 net/can/isotp.c                | 6 +++---
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/include/uapi/linux/can/isotp.h b/include/uapi/linux/can/isotp.h
index eb8b5298f7a5..73d0dcaecc16 100644
--- a/include/uapi/linux/can/isotp.h
+++ b/include/uapi/linux/can/isotp.h
@@ -161,14 +161,18 @@ struct can_isotp_ll_options {
  * these default settings can be changed via sockopts.
  * For that reason the STmin value is intentionally _not_ checked for
  * consistency and copied directly into the flow control (FC) frame.
  */
 
+/* minimum TX_DL value (link layer data length of a Classical CAN frame) */
+
+#define CAN_ISOTP_MIN_TX_DL		CAN_MAX_DLEN
+
 /* link layer default values => make use of Classical CAN frames */
 
 #define CAN_ISOTP_DEFAULT_LL_MTU	CAN_MTU
-#define CAN_ISOTP_DEFAULT_LL_TX_DL	CAN_MAX_DLEN
+#define CAN_ISOTP_DEFAULT_LL_TX_DL	CAN_ISOTP_MIN_TX_DL
 #define CAN_ISOTP_DEFAULT_LL_TX_FLAGS	0
 
 /*
  * The CAN_ISOTP_DEFAULT_FRAME_TXTIME has become a non-zero value as
  * it only makes sense for isotp implementation tests to run without
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 2a74a1ccb1a6..330832e7357c 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -610,12 +610,12 @@ static void isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
 		if (so->rx.len <= MAX_12BIT_PDU_SIZE)
 			return;
 	}
 
 	/* the PDU must not fit into a SF (FF_DLmin) */
-	if (so->rx.ll_dl == CAN_MAX_DLEN)
-		sf_max = CAN_MAX_DLEN - SF_PCI_SZ4 - ae;
+	if (so->rx.ll_dl == CAN_ISOTP_MIN_TX_DL)
+		sf_max = CAN_ISOTP_MIN_TX_DL - SF_PCI_SZ4 - ae;
 	else
 		sf_max = so->rx.ll_dl - SF_PCI_SZ8 - ae;
 
 	if (so->rx.len <= sf_max)
 		return;
@@ -1219,11 +1219,11 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		err = -EINVAL;
 		goto err_out_drop;
 	}
 
 	/* take care of a potential SF_DL ESC offset for TX_DL > 8 */
-	off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
+	off = (so->tx.ll_dl > CAN_ISOTP_MIN_TX_DL) ? 1 : 0;
 
 	/* does the given data fit into a single frame for SF_BROADCAST? */
 	if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
 	    (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
 		err = -EINVAL;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups
  2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
                   ` (21 preceding siblings ...)
  2026-11-28 17:49 ` [PATCH can-next v2 22/22] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp
@ 2026-11-30  8:57 ` Oliver Hartkopp
  22 siblings, 0 replies; 32+ messages in thread
From: Oliver Hartkopp @ 2026-11-30  8:57 UTC (permalink / raw)
  To: Marc Kleine-Budde
  Cc: linux-can, Sashiko, Zihan Xi, Oleksij Rempel, Wentian Ye

Hello Marc,

I really don't get behind the sending process delay of Sashiko review 
mails after he has finalized the review:

https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

Anyway there are some wording and documentation nitpicks that are worth 
to change for a "fixes and cleanups" patchset. So I'll send a v3 with 
some updated documentation (no code changes).

Best regards,
Oliver

pw-bot: cr

On 28.11.26 18:48, Oliver Hartkopp wrote:
> Patches 1-3 are the remaining CAN patches of the former lock_sock() and
> memcg series, whose first patches have been applied via the net tree.
> They are followed by CAN_ISOTP fixes and cleanups that have been found
> while preparing the CAN XL support for CAN_ISOTP.
> 
> Patches 1-2 read the CAN_RAW and CAN_ISOTP socket options under
> conf_lock, so that a concurrent setsockopt() can not lead to a mix of
> old and new values in getsockopt().
> 
> Patch 3 enables the memcg accounting for the CAN allocations that can
> be triggered by unprivileged users (socket structures, receivers,
> CAN_BCM ops and frames, CAN_RAW/J1939 filters, the CAN_ISOTP TX buffer
> for large PDUs and the J1939 per device data).
> 
> Patch 4 fixes a CAN_ISOTP socket in listen mode that sent a FC.OVFLW on
> the bus. It carries a Fixes tag and applies to net as well.
> 
> Patches 5-10 improve the CAN_ISOTP socket options and timing:
> - check the CAN_ISOTP_OPTS values before applying them
> - add the frame transmission time (N_As/N_Ar) to the CF broadcast gap
>    and to the protocol timeouts and limit it to the N_As/N_Ar timeout
> - reject undefined CAN FD tx_flags and reserved STmin values
> - ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING as
>    documented
> 
> Patches 11-14 fix the CAN_ISOTP reception against ISO 15765-2:
> - drop frames without N_PCI and FFs with an invalid length extension
> - check FF_DLmin and the data length of the last CF
> 
> Patches 15-22 are cleanups in preparation of the CAN XL support for
> CAN_ISOTP.
> 
> Patches 5-14 only affect invalid configurations and frames or tighten
> the uAPI and therefore do not carry Fixes tags.
> 
> Changes in v2:
> - can: enable memcg accounting for user triggered allocations:
>    added Acked-by from Oleksij Rempel
> - can: isotp: do not send FC.OVFLW in listen mode:
>    moved to patch 4, describe the impact on a correct listen mode
>    configuration, add Fixes tag and Cc stable
> - new patch: can: isotp: add the frame transmission time to the CF
>    broadcast gap (also fixes the unit of the forced stmin values in
>    iso15765-2.rst)
> - can: isotp: add the frame transmission time to the protocol timeouts:
>    limit frame_txtime to the N_As/N_Ar timeout of 1 s from ISO 15765-2
>    instead of 100 ms, document the limit in isotp.h and iso15765-2.rst
> - can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS:
>    state the intentional uAPI tightening, always set CANFD_FDF for
>    CAN FD, document the accepted tx_flags
> - can: isotp: reject CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
>    replaced by "can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without
>    CAN_ISOTP_RX_PADDING" which fixes check_pad() as documented instead
>    of rejecting the option
> - can: isotp: drop frames without N_PCI, check the minimum FF_DL, check
>    the data length of the last CF: describe the impact and the reason
>    for the missing Fixes tag in the commit messages
> - dropped: can: isotp: ignore invalid SF and FF frames during a reception
> - can: isotp: rename rx_lock as it has become state machine lock:
>    fixed typo in the subject, updated the locking comment in isotp_rcv()
> - can: isotp: change return type of functions to void:
>    removed the trailing return statements
> - can: isotp: introduce CAN_ISOTP_MIN_TX_DL:
>    use it only for TX_DL checks, separate block in isotp.h
> - Link to v1: https://patch.msgid.link/20261126-getsockopt-memcg-isotp-v1-0-681ef8d8c2c9@hartkopp.net
> 
> ---
> Oliver Hartkopp (22):
>        can: raw: read socket options under conf_lock in raw_getsockopt()
>        can: isotp: read socket options under conf_lock in isotp_getsockopt()
>        can: enable memcg accounting for user triggered allocations
>        can: isotp: do not send FC.OVFLW in listen mode
>        can: isotp: check the CAN_ISOTP_OPTS values before applying them
>        can: isotp: add the frame transmission time to the CF broadcast gap
>        can: isotp: add the frame transmission time to the protocol timeouts
>        can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
>        can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
>        can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING
>        can: isotp: drop frames without N_PCI
>        can: isotp: drop FF frames with invalid length extension
>        can: isotp: check the minimum FF_DL
>        can: isotp: check the data length of the last CF
>        can: isotp: drop inline keyword, let the compiler decide
>        can: isotp: rename rx_lock as it has become state machine lock
>        can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs
>        can: isotp: rename variable for the required content length
>        can: isotp: rename checkpad functions returning bool values
>        can: isotp: change return type of functions to void
>        can: isotp: rename pointer in isotp_rcv() prototype
>        can: isotp: introduce CAN_ISOTP_MIN_TX_DL
> 
>   Documentation/networking/iso15765-2.rst |  27 ++-
>   include/uapi/linux/can/isotp.h          |   9 +-
>   net/can/af_can.c                        |  12 +-
>   net/can/bcm.c                           |  10 +-
>   net/can/isotp.c                         | 346 ++++++++++++++++++--------------
>   net/can/j1939/main.c                    |   2 +-
>   net/can/j1939/socket.c                  |  12 +-
>   net/can/raw.c                           |  78 +++----
>   8 files changed, 291 insertions(+), 205 deletions(-)
> ---
> base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
> change-id: 20261126-getsockopt-memcg-isotp-052433c7adda
> 
> Best regards,
> --
> Oliver Hartkopp <socketcan@hartkopp.net>
> 


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations
  2026-11-28 17:48 ` [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

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

Critical: 0 · High: 0 · Medium: 1 · 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 patch says it charges user-triggered CAN allocations to
  the cgroup so a process cannot bypass its memory limit.

--

can: enable memcg accounting for user triggered allocations

This commit charges user triggered CAN allocations to the memory cgroup
of the allocating process. That covers the socket structures, receivers,
BCM ops and frame arrays, the raw and j1939 filters, the isotp TX buffer
and j1939_priv. The isotp RX buffer, which is allocated in softirq
context, is deliberately left unaccounted.

Link: https://lore.kernel.org/linux-can/350df96b-5f69-490e-b39e-ed6e9bca4754@hartkopp.net/

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index 98a7e489ca3e5..3b83507a8c21b 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -609,7 +609,12 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
>  	if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
>  		return 1;
>  
> -	/* PDU size > default => try max_pdu_size */
> +	/* PDU size > default => try max_pdu_size
> +	 *
> +	 * This buffer is not accounted to a memory cgroup: In softirq
> +	 * context the charge would either have to be forced beyond the
> +	 * limit of the cgroup or the reception would fail at the limit.
> +	 */
>  	if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
>  		u8 *newbuf = kmalloc(max_pdu_size, GFP_ATOMIC);

[Severity: Medium]

Does leaving this buffer uncharged really avoid the overshoot that the
comment describes?

isotp_rcv_ff() stores the buffer in so->rx.buf. It is freed only in
isotp_sock_destruct():

	if (so->rx.buf != so->rx.sbuf)
		kfree(so->rx.buf);

So it stays pinned for the whole life of the socket.

This path only runs once the administrator has raised the max_pdu_size
module parameter (it can go up to MAX_PDU_SIZE, 1025 * 1024). After
that, each isotp socket that receives one large First Frame keeps up to
about 1 MiB that the memcg never sees. The socket itself is now charged
through sk_alloc(GFP_KERNEL_ACCOUNT), and it is only about 17 KB.

The commit message rejects charging because a forced GFP_ATOMIC charge
"allows a cgroup to exceed its limit by up to max_pdu_size per socket".
Leaving the buffer uncharged allows the same overshoot per socket, and
it also hides the memory from the cgroup.

try_charge_memcg() forces the charge for __GFP_HIGH, so a forced charge
would at least count the memory. Later charges in process context, such
as new sk_alloc() calls, would then hit the limit. That would trigger
memcg reclaim or OOM against the cgroup responsible for the memory.

Can an unprivileged user open many isotp sockets and send large First
Frames on the same interface to pin memory well beyond the cgroup limit?
Two ways to send them are a CAN_RAW socket with local loopback, or vcan
in a user-owned netns.

If so, the commit message's own concern ("The OOM killer can not
attribute this kernel memory to the responsible process") still applies
to this buffer. Would charging it to the socket's memcg with
set_active_memcg() and GFP_ATOMIC | __GFP_ACCOUNT be the better choice
here?

>  
> @@ -1194,7 +1199,7 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode
  2026-11-28 17:48 ` [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch moves the CAN_ISOTP_LISTEN_MODE check into
  isotp_send_fc() (net/can/isotp.c:297-299), so the function now returns…

--

can: isotp: do not send FC.OVFLW in listen mode

This patch moves the CAN_ISOTP_LISTEN_MODE check out of isotp_rcv_ff() and
isotp_rcv_cf() and into isotp_send_fc(). With this change a listen mode
socket sends no flow control frames at all. That includes the FC.OVFLW
frame sent when the FF_DL exceeds the receive buffer.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index 3b83507a8c21b..650d93e84c8eb 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -294,6 +294,10 @@ static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus)
>  	struct isotp_sock *so = isotp_sk(sk);
>  	int can_send_ret;
>  
> +	/* no creation of flow control frames in listen mode */
> +	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
> +		return 0;
> +
>  	nskb = alloc_skb(so->ll.mtu, gfp_any());
>  	if (!nskb)
>  		return 1;
> @@ -643,10 +647,6 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
>  	hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
>  		      HRTIMER_MODE_REL_SOFT);
>  
> -	/* no creation of flow control frames */
> -	if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
> -		return 0;
> -
>  	/* send our first FC frame */
>  	isotp_send_fc(sk, ae, ISOTP_FC_CTS);
>  	return 0;

[Severity: Low]
This isn't a bug, but is the comment just above this hrtimer_start() in
isotp_rcv_ff() still accurate?

    /* start rx timeout, isotp_send_fc() restarts it */
    hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
                  HRTIMER_MODE_REL_SOFT);

In listen mode, the new early return in isotp_send_fc() skips three steps
at the end of the function:

    so->rx.bs = 0;
    so->lastrxcf_tstamp = ktime_set(0, 0);
    hrtimer_start(&so->rxtimer, ...);

Before this patch, the listen mode exception was written out at each call
site. Now isotp_send_fc() is called in listen mode but does not restart
rxtimer, so the comment is only true some of the time.

isotp_rcv_cf() has the same comment before its isotp_send_fc() call.

Runtime behavior doesn't change at this commit, because both hrtimer_start()
calls use ktime_set(ISOTP_FC_TIMEOUT, 0). Later in the series
(bf53ca579974), isotp_send_fc() restarts the timer with
ktime_set(ISOTP_FC_TIMEOUT, so->frame_txtime) while the callers still use
0, and the comments stay the same. Someone reading the comment would then
expect a listen mode socket to get the frame_txtime extended timeout.

Could the comment be reworded, for example "isotp_send_fc() restarts it
when an FC is actually sent (not in listen mode)"?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap
  2026-11-28 17:48 ` [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The patch fixes a real uAPI documentation error, but it carries
  no Fixes: tag and bundles that fix with an unrelated change to…

--

can: isotp: add the frame transmission time to the CF broadcast gap

In CAN_ISOTP_CF_BROADCAST mode, this commit adds the frame transmission
time (N_As) to the consecutive frame gap. It does so with and without the
CAN_ISOTP_FORCE_TXSTMIN flag, and documents the gap in iso15765-2.rst. It
also changes the documented unit of the CAN_ISOTP_TX_STMIN and
CAN_ISOTP_RX_STMIN socket options from microseconds to nanoseconds.

> diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
> index 37ebb2c417cb4..badcd16c070bb 100644
> --- a/Documentation/networking/iso15765-2.rst
> +++ b/Documentation/networking/iso15765-2.rst

[ ... ]

> @@ -286,7 +289,7 @@ Transmission stmin
>  ~~~~~~~~~~~~~~~~~~
>  
>  The transmission minimum separation time (stmin) can be forced using the
> -``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in microseconds as
> +``CAN_ISOTP_TX_STMIN`` optname and providing an stmin value in nanoseconds as
>  a 32bit unsigned integer; this will overwrite the value sent by the receiver in
>  flow control frames:
>  
> @@ -299,7 +302,7 @@ Reception stmin
>  ~~~~~~~~~~~~~~~
>  
>  The reception minimum separation time (stmin) can be forced using the
> -``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in microseconds as
> +``CAN_ISOTP_RX_STMIN`` optname and providing an stmin value in nanoseconds as
>  a 32bit unsigned integer; received Consecutive Frames (CF) which timestamps
>  differ less than this value will be ignored:

[Severity: Low]
This isn't a bug, but should this unit correction have a Fixes: tag and
go in its own patch?

The commit message says "Additionally fix the unit of the values of the
CAN_ISOTP_TX_STMIN and CAN_ISOTP_RX_STMIN socket options". The
microseconds wording came from:

  Fixes: 67711e04254c ("Documentation: networking: document ISO 15765-2")

The setsockopt handler stores the value without converting it. The value
is then used as nanoseconds in isotp_rcv_fc(), isotp_sendmsg() and
isotp_rcv_cf(). On a stable kernel, a user who follows the current text
and passes 1000 for CAN_ISOTP_TX_STMIN expects 1 ms but gets 1 us.

This patch also changes the CF broadcast gap in isotp_sendmsg(). Stable
trees can't take the documentation fix without that timing change too.

Other patches in this series explain why they leave out a Fixes: tag, for
example "can: isotp: drop frames without N_PCI" and "can: isotp: check
the minimum FF_DL". This commit doesn't give a reason.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts
  2026-11-28 17:48 ` [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The text this patch adds to
  Documentation/networking/iso15765-2.rst (lines 211-218) says…

--

can: isotp: add the frame transmission time to the protocol timeouts

This patch adds so->frame_txtime to the rx timeout started in
isotp_send_fc() and to the timer started in isotp_sendmsg(). That timer is
the first FC timeout or, for SF and FC-less FF, the echo timeout. It also
rejects frame_txtime values above 1 s and documents the new limit and
behaviour.

Closes: https://lore.kernel.org/linux-can/eb5e9618-3dbf-42cb-a635-752ff9fbcb99@hartkopp.net/

> diff --git a/Documentation/networking/iso15765-2.rst b/Documentation/networking/iso15765-2.rst
> index badcd16c070bb..1962209f2db5c 100644
> --- a/Documentation/networking/iso15765-2.rst
> +++ b/Documentation/networking/iso15765-2.rst
> @@ -208,10 +208,14 @@ where the ``can_isotp_options`` structure has the following contents:

[ ... ]

> +* ``frame_txtime``: frame transmission time in nanoseconds (defined as
> +  N_As/N_Ar inside the ISO standard); if ``0``, the default (or the last set
> +  value) is used. To set the transmission time to ``0``, the
> +  ``CAN_ISOTP_FRAME_TXTIME_ZERO`` macro (equal to 0xFFFFFFFF) shall be used.
> +  The frame transmission time is limited to the N_As/N_Ar timeout of 1 s
> +  (1000000000 ns) defined in the ISO standard. It is added to the gap between
> +  the consecutive frames and to the protocol timeouts that are started after
> +  the transmission of a CAN frame.

[Severity: Low]
Does this sentence match what the code does after this patch?

frame_txtime is only added at two timer starts. One is the rxtimer start in
isotp_send_fc(). The other is the single tx_hrt start in isotp_sendmsg(),
which arms either so->txtimer (first FC) or so->echotimer (SF, or FF with
CAN_ISOTP_CF_BROADCAST).

The same so->echotimer is also used for the CF local echo timeout. Its
other start sites still use no frame_txtime:

net/can/isotp.c:isotp_rcv_fc() {
    ...
		hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
			      HRTIMER_MODE_REL_SOFT);
		isotp_send_cframe(so);
    ...
}

net/can/isotp.c:isotp_rcv_echo() {
    ...
	if (!so->tx_gap) {
		/* enable echo timeout handling */
		hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
			      HRTIMER_MODE_REL_SOFT);
		isotp_send_cframe(so);
    ...
}

net/can/isotp.c:isotp_txfr_timer_handler() {
    ...
	hrtimer_start(&so->echotimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
		      HRTIMER_MODE_REL_SOFT);
    ...
}

The FC wait timeout that isotp_rcv_echo() starts after a block completes
is not extended either:

		WRITE_ONCE(so->tx.state, ISOTP_WAIT_FC);
		hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
			      HRTIMER_MODE_REL_SOFT);

After this patch the SF/FF echo timeout is 2 s + frame_txtime, but the CF
echo timeout stays at 2 s. Both wait for the local echo of a frame that was
just handed to can_send().

With the new 1 s cap and the 2 s ISOTP_ECHO_TIMEOUT, this doesn't look like
it causes a functional failure. The commit message mentions the SF echo
extension as a side effect, but it doesn't explain why the CF echo timers
differ.

Should the documentation be limited to the FC related timeouts? The other
option would be to add so->frame_txtime to the CF echotimer starts as well,
so the documented rule holds.

As far as I can tell, these call sites are still unchanged at the end of
the series.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS
  2026-11-28 17:49 ` [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

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

Critical: 0 · High: 0 · Medium: 1 · 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] In isotp_setsockopt_locked() (net/can/isotp.c), the
  CAN_ISOTP_LL_OPTS handler now stores tx_flags with CANFD_FDF ORed in…

--

can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS

For CAN FD, this commit limits the CAN_ISOTP_LL_OPTS tx_flags to
CANFD_BRS and CANFD_FDF. Any other flag now gets -EINVAL. For CAN FD it
also ORs CANFD_FDF into the stored tx_flags, so that getsockopt()
returns the flags of the created frames, and it documents the accepted
flags.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index ce541e57d1979..ea3790a855827 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -1796,8 +1796,17 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
>  		    (ll->tx_dl > CAN_MAX_DLEN || ll->tx_flags != 0))
>  			return -EINVAL;
>  
> +		/* only CANFD_BRS (and the CANFD_FDF marker) for CAN FD frames */
> +		if (ll->mtu == CANFD_MTU &&
> +		    (ll->tx_flags & ~(CANFD_BRS | CANFD_FDF)))
> +			return -EINVAL;
> +
>  		memcpy(&so->ll, ll, sizeof(*ll));
>  
> +		/* CAN FD frames are always marked with CANFD_FDF */
> +		if (so->ll.mtu == CANFD_MTU)
> +			so->ll.tx_flags |= CANFD_FDF;
> +

[Severity: Medium]
Does storing CANFD_FDF here break a read-modify-write of CAN_ISOTP_LL_OPTS
when switching back to classical CAN?

isotp_getsockopt() returns so->ll unchanged:

	case CAN_ISOTP_LL_OPTS:
		len = min_t(int, len, sizeof(struct can_isotp_ll_options));
		val.ll = so->ll;
		break;

The CAN CC check in isotp_setsockopt_locked() still requires tx_flags to
be exactly 0:

		if (ll->mtu == CAN_MTU &&
		    (ll->tx_dl > CAN_MAX_DLEN || ll->tx_flags != 0))
			return -EINVAL;

So on an unbound socket this sequence now fails:

  setsockopt(LL_OPTS, {mtu=CANFD_MTU, tx_dl=64, tx_flags=0})
  getsockopt(LL_OPTS) -> tx_flags = CANFD_FDF
  change only mtu=CAN_MTU and tx_dl=8, then write it back
  setsockopt(LL_OPTS, {mtu=CAN_MTU, tx_dl=8, tx_flags=CANFD_FDF}) -> -EINVAL

Before this patch the stored tx_flags stayed 0, so the same sequence
succeeded. A stored CANFD_BRS already had to be cleared before switching
to CAN_MTU. That means only the CAN FD without BRS case behaves
differently now.

The commit message says "The CAN frames on the bus are not changed by
this". It does not mention that the tx_flags value returned by
getsockopt() is no longer accepted for CAN_MTU.

Would it make sense to mask CANFD_FDF in the CAN_MTU check? Another
option is to add CANFD_FDF only in isotp_getsockopt() and not store it
in so->ll.

>  		/* set ll_dl for tx path to similar place as for rx */
>  		so->tx.ll_dl = ll->tx_dl;
>  		break;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
  2026-11-28 17:49 ` [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

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

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

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 uAPI header now contradicts the code.
- [Low] The commit message does not say that 'can: isotp: reject reserved
  STmin values in CAN_ISOTP_RECV_FC' changes the uAPI, and it does not…
- [Low] The 'Flow Control options' section of
  Documentation/networking/iso15765-2.rst (lines 253-258) lists the…

--

can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC

This patch makes setsockopt(CAN_ISOTP_RECV_FC) return -EINVAL when the
STmin value is in one of the ISO 15765-2 reserved ranges, 0x80 - 0xF0 or
0xFA - 0xFF. Before this change, any value was accepted and sent on the
bus in the receiver's flow control frames.

> can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC
>
> The STmin value of struct can_isotp_fc_options is sent in the flow
> control frames of the receiver. ISO 15765-2 reserves the values 0x80 -
> 0xF0 and 0xFA - 0xFF, but CAN_ISOTP_RECV_FC accepted any value and the
> reserved values were sent on the bus. The sender side replaces received
> reserved values with 0x7F already.
>
> Reject the reserved STmin values with -EINVAL before the options are
> applied.

[Severity: Low]
Is this meant as a bug fix or as a deliberate tightening of the uAPI? The
message calls the old behaviour a defect ("the reserved values were sent
on the bus"). It has no Fixes: tag, and it gives no reason for leaving one
out.

The sibling patch in this series, "can: isotp: check the CAN FD tx_flags
in CAN_ISOTP_LL_OPTS", says "This is an intentional tightening of the
uAPI and no bug fix". It also names CAN_RAW as the fallback for testing.

Could this commit message say the same kind of thing? It could also
mention that the change reverses the "intentionally _not_ checked"
behaviour documented in include/uapi/linux/can/isotp.h. Without that,
stable/AUTOSEL handling is unclear. Users who set reserved STmin values on
purpose also get no explanation and no alternative.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index ea3790a855827..22bfc1c7b131f 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -1770,9 +1770,17 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
>  		break;
>  	}
>
> -	case CAN_ISOTP_RECV_FC:
> -		memcpy(&so->rxfc, val, sizeof(so->rxfc));
> +	case CAN_ISOTP_RECV_FC: {
> +		const struct can_isotp_fc_options *fc = val;
> +
> +		/* reject the reserved STmin values 0x80 - 0xF0, 0xFA - 0xFF */
> +		if (fc->stmin > 0x7F &&
> +		    (fc->stmin < 0xF1 || fc->stmin > 0xF9))
> +			return -EINVAL;

[Severity: Medium]
Does this now contradict the uAPI header? The "Remark on
CAN_ISOTP_DEFAULT_RECV_* values" block in include/uapi/linux/can/isotp.h
still says:

 * But as we like to be able to behave as a commonly available ECU,
 * these default settings can be changed via sockopts.
 * For that reason the STmin value is intentionally _not_ checked for
 * consistency and copied directly into the flow control (FC) frame.

Before this patch, the value went straight into so->rxfc, and
isotp_send_fc() put it on the bus:

	ncf->data[ae + 2] = so->rxfc.stmin;

Now setsockopt(fd, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fc, 3) with
fc.stmin in 0x80 - 0xF0 or 0xFA - 0xFF fails with -EINVAL. It used to
return 0. Because the whole option is rejected, bs and wftmax from the
same call are not applied either.

Some userspace may rely on the documented behaviour, for example ECU
simulators or robustness tools that send reserved STmin values to check
that the peer clamps them to 0x7F, as isotp_rcv_fc() does here. Is that
break intended?

If it is, should the header comment be updated in this patch? It is
still unchanged at the end of the series, at "can: isotp: introduce
CAN_ISOTP_MIN_TX_DL".

[Severity: Low]
Should Documentation/networking/iso15765-2.rst mention this new error
return? Its "Flow Control options" section lists the stmin ranges and
says only "(others are reserved)". It does not say that setsockopt() now
rejects those values.

The tx_flags entry in the same file does document its restriction:

  ``CANFD_FDF`` marker) can be set, other flags are rejected with ``-EINVAL``.

The stmin entry is still unchanged at the end of the series.

> +
> +		memcpy(&so->rxfc, fc, sizeof(*fc));
>  		break;
> +	}
>
>  	case CAN_ISOTP_TX_STMIN:
>  		so->force_tx_stmin = *(const u32 *)val;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI
  2026-11-28 17:49 ` [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

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

Critical: 0 · High: 0 · Medium: 1 · 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 patch only partly removes the symptom its commit message
  describes.

--

can: isotp: drop frames without N_PCI

This patch adds a length check to isotp_rcv() so that CAN frames with
CAN_DL < ae + N_PCI_SZ are dropped. Previously the AE and N_PCI bytes
were read beyond CAN_DL. A zero-filled CAN_DL 0 frame was then handled
as an SF with SF_DL 0, which ended an ongoing reception.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index b237b1fb9d99c..798c1b4c6f6ae 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -775,6 +775,10 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
>  
>  	cf = (struct canfd_frame *)skb->data;
>  
> +	/* frames without N_PCI (and AE) are no N_PDUs */
> +	if (cf->len < ae + N_PCI_SZ)
> +		return;
> +

[Severity: Medium]
This new check closes the read beyond CAN_DL. Can an invalid SF still
end an ongoing reception, though? A frame one byte longer, with
CAN_DL = ae + 1 and N_PCI byte 0x00, passes this check. It then goes
into isotp_rcv_sf() with SF_DL 0.

isotp_rcv_sf() resets the rx state before it checks the length:

net/can/isotp.c:isotp_rcv_sf() {
    ...
	hrtimer_cancel(&so->rxtimer);
	WRITE_ONCE(so->rx.state, ISOTP_IDLE);

	if (!len || len > cf->len - pcilen)
		return 1;
    ...
}

So the zero-filled frame from the commit message, with its length byte
raised by one, still ends a reception in ISOTP_WAIT_DATA. The same
happens for any SF with SF_DL > CAN_DL - pcilen. It also happens for a
CAN FD SF with CAN_DL > 8, the SF_DL escape value 0, and a real SF_DL
that is 0 or too large.

isotp_rcv_ff() uses the same order:

net/can/isotp.c:isotp_rcv_ff() {
    ...
	hrtimer_cancel(&so->rxtimer);
	WRITE_ONCE(so->rx.state, ISOTP_IDLE);

	/* get the used sender LL_DL from the (first) CAN frame data length */
	so->rx.ll_dl = padlen(cf->len);

	/* the first frame has to use the entire frame up to LL_DL length */
	if (cf->len != so->rx.ll_dl)
		return 1;
    ...
}

padlen() maps 0..8 to 8, so an FF with CAN_DL 2..7 is rejected only
after rx.state has been reset and rx.ll_dl overwritten. None of these
paths set sk_err. The partial PDU is dropped silently, and the
following CFs are ignored because rx.state is no longer
ISOTP_WAIT_DATA.

The SF handling in isotp_rcv() is also inconsistent. An FD SF with
CAN_DL > 8 and SF_DL != 0 is ignored without touching the rx state,
but the invalid SFs above end the reception:

		if (cf->len <= CAN_MAX_DLEN) {
			isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl);
		} else {
			if (can_is_canfd_skb(skb)) {
				...
				if (sf_dl == 0)
					isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb,
						     cf->data[SF_PCI_SZ4 + ae]);

isotp_rcv_cf() checks the frame first and only then changes state:

	/* stop the rx timeout only for valid CFs (checked above) */
	hrtimer_cancel(&so->rxtimer);

The problem is still there at the end of the series.
isotp_rcv_sf() and isotp_rcv_ff() still reset the state first. The
later patches "can: isotp: drop FF frames with invalid length
extension" and "can: isotp: check the minimum FF_DL" add more return
paths after that reset. Their commit messages say these frames "shall
be ignored according to ISO 15765-2:2016". As written, those invalid
FFs abort an ongoing reception instead of being ignored.

Would it make sense to validate SF_DL, the FF CAN_DL and FF_DL before
cancelling rxtimer and setting rx.state to ISOTP_IDLE? The new N_PCI
length check would still be needed in addition to that.

>  	/* if enabled: check reception of my configured extended address */
>  	if (ae && cf->data[0] != so->opt.rx_ext_address)
>  		return;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock
  2026-11-28 17:49 ` [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
@ 2026-11-30 20:25   ` netdev-bot+sashiko
  0 siblings, 0 replies; 32+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 20:25 UTC (permalink / raw)
  To: socketcan; +Cc: linux-can, mkl, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch adds a comment in isotp_rcv() (net/can/isotp.c) saying
  that so->sm_lock "serializes the rx path with the claim of a transfer…

--

can: isotp: rename rx_lock as it has become state machine lock

This patch renames so->rx_lock to so->sm_lock in net/can/isotp.c. The
commit message says the lock is now used as a full state machine lock. It
also rewrites the locking comment in isotp_rcv() to list the paths the lock
serializes against.

> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index 0780a0ec98677..1590627e34bd8 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c

[ ... ]

> @@ -795,10 +795,11 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
>  	n_pci_type = cf->data[ae] & 0xF0;
>  
>  	/* Make sure the state changes and data structures stay consistent at
> -	 * CAN frame reception time. This locking is not needed in real world
> -	 * use cases but the inconsistency can be triggered with syzkaller.
> +	 * CAN frame reception time: so->sm_lock serializes the rx path with
> +	 * the claim of a transfer in isotp_sendmsg(), with isotp_rcv_echo()
> +	 * and with isotp_release().
>  	 */
> -	spin_lock(&so->rx_lock);
> +	spin_lock(&so->sm_lock);

[Severity: Low]
Is the isotp_release() part of this comment accurate?

Together with the commit message's description of the lock as "a full
state machine locking primitive", the comment suggests that the rx path is
serialized against isotp_release(). In fact isotp_release() only holds
sm_lock for the tx.state transition:

net/can/isotp.c:isotp_release() {
    ...
	spin_lock_bh(&so->sm_lock);
	WRITE_ONCE(so->tx.state, ISOTP_SHUTDOWN);
	spin_unlock_bh(&so->sm_lock);
	WRITE_ONCE(so->rx.state, ISOTP_IDLE);
    ...
}

The CAN filters are unregistered later, under conf_lock. So isotp_rcv() can
still be running on another CPU at this point. Under sm_lock,
isotp_rcv()->isotp_rcv_ff() does:

	so->rx.sn = 1;
	WRITE_ONCE(so->rx.state, ISOTP_WAIT_DATA);

and then arms rxtimer. That can overwrite the ISOTP_IDLE that
isotp_release() wrote without holding the lock.

isotp_rx_timer_handler() also reads and writes rx.state without sm_lock.
isotp_rcv_cf() already notes this:

	/* isotp_rx_timer_handler() may have raced us for so->rx.state
	 * while hrtimer_cancel() above waited for it to finish => recheck
	 */

This has no visible effect at runtime today. Later in isotp_release(),
synchronize_rcu() and hrtimer_cancel(&so->rxtimer) drain it before the
socket is orphaned.

The comment and the unlocked rx.state write in isotp_release() are
unchanged at the end of the series, after "can: isotp: introduce
CAN_ISOTP_MIN_TX_DL".

Could the comment and the commit message be limited to the tx.state
ISOTP_SHUTDOWN transition in isotp_release()? Otherwise a later change
could rely on rx path serialization that isn't there. One example would be
reordering synchronize_rcu() and the rxtimer cancel in isotp_release().

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128-getsockopt-memcg-isotp-v2-0-034ba95e17a4%40hartkopp.net

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2026-11-30 20:25 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 17:48 [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp
2026-11-28 17:48 ` [PATCH can-next v2 01/22] can: raw: read socket options under conf_lock in raw_getsockopt() Oliver Hartkopp
2026-11-28 17:48 ` [PATCH can-next v2 02/22] can: isotp: read socket options under conf_lock in isotp_getsockopt() Oliver Hartkopp
2026-11-28 17:48 ` [PATCH can-next v2 03/22] can: enable memcg accounting for user triggered allocations Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:48 ` [PATCH can-next v2 04/22] can: isotp: do not send FC.OVFLW in listen mode Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:48 ` [PATCH can-next v2 05/22] can: isotp: check the CAN_ISOTP_OPTS values before applying them Oliver Hartkopp
2026-11-28 17:48 ` [PATCH can-next v2 06/22] can: isotp: add the frame transmission time to the CF broadcast gap Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:48 ` [PATCH can-next v2 07/22] can: isotp: add the frame transmission time to the protocol timeouts Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:49 ` [PATCH can-next v2 08/22] can: isotp: check the CAN FD tx_flags in CAN_ISOTP_LL_OPTS Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:49 ` [PATCH can-next v2 09/22] can: isotp: reject reserved STmin values in CAN_ISOTP_RECV_FC Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:49 ` [PATCH can-next v2 10/22] can: isotp: ignore CAN_ISOTP_CHK_PAD_DATA without CAN_ISOTP_RX_PADDING Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 11/22] can: isotp: drop frames without N_PCI Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:49 ` [PATCH can-next v2 12/22] can: isotp: drop FF frames with invalid length extension Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 13/22] can: isotp: check the minimum FF_DL Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 14/22] can: isotp: check the data length of the last CF Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 15/22] can: isotp: drop inline keyword, let the compiler decide Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 16/22] can: isotp: rename rx_lock as it has become state machine lock Oliver Hartkopp
2026-11-30 20:25   ` netdev-bot+sashiko
2026-11-28 17:49 ` [PATCH can-next v2 17/22] can: isotp: introduce isotp_invalid_canid() to verify proper CAN-IDs Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 18/22] can: isotp: rename variable for the required content length Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 19/22] can: isotp: rename checkpad functions returning bool values Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 20/22] can: isotp: change return type of functions to void Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 21/22] can: isotp: rename pointer in isotp_rcv() prototype Oliver Hartkopp
2026-11-28 17:49 ` [PATCH can-next v2 22/22] can: isotp: introduce CAN_ISOTP_MIN_TX_DL Oliver Hartkopp
2026-11-30  8:57 ` [PATCH can-next v2 00/22] can: memcg accounting, CAN_ISOTP fixes and cleanups Oliver Hartkopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox