* [PATCH v3 1/4] can: annotate mtu accesses with READ_ONCE()
2025-09-23 6:37 [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Vincent Mailhol
@ 2025-09-23 6:37 ` Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 2/4] can: dev: turn can_set_static_ctrlmode() into a non-inline function Vincent Mailhol
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Vincent Mailhol @ 2025-09-23 6:37 UTC (permalink / raw)
To: Oliver Hartkopp, Marc Kleine-Budde
Cc: linux-can, linux-kernel, Vincent Mailhol
As hinted in commit 501a90c94510 ("inet: protect against too small mtu
values."), net_device->mtu is vulnerable to race conditions if it is
written and read without holding the RTNL.
At the moment, all the writes are done while the interface is down,
either in the devices' probe() function or in can_changelink(). So
there are no such issues yet. But upcoming changes will allow to
modify the MTU while the CAN XL devices are up.
In preparation to the introduction of CAN XL, annotate all the
net_device->mtu accesses which are not yet guarded by the RTNL with a
READ_ONCE().
Note that all the write accesses are already either guarded by the
RTNL or are already annotated and thus need no changes.
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
net/can/af_can.c | 2 +-
net/can/isotp.c | 2 +-
net/can/raw.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/can/af_can.c b/net/can/af_can.c
index b2387a46794a576973f3d865a5ca8e2ba696d167..770173d8db42813d5c085248d1bcf5fbe717955b 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -221,7 +221,7 @@ int can_send(struct sk_buff *skb, int loop)
}
/* Make sure the CAN frame can pass the selected CAN netdevice. */
- if (unlikely(skb->len > skb->dev->mtu)) {
+ if (unlikely(skb->len > READ_ONCE(skb->dev->mtu))) {
err = -EMSGSIZE;
goto inval_skb;
}
diff --git a/net/can/isotp.c b/net/can/isotp.c
index dee1412b3c9c1ffcfc43a109b448701459fcf8b9..74ee1e52249b232813a06c5d2c6e404a38dce990 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1313,7 +1313,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
err = -ENODEV;
goto out;
}
- if (dev->mtu < so->ll.mtu) {
+ if (READ_ONCE(dev->mtu) < so->ll.mtu) {
dev_put(dev);
err = -EINVAL;
goto out;
diff --git a/net/can/raw.c b/net/can/raw.c
index 76b867d21def209f5c6d236604c0e434a1c55a4d..6cb959e3dcd3e54972628ee59572a749009d1323 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -950,7 +950,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
err = -EINVAL;
/* check for valid CAN (CC/FD/XL) frame content */
- txmtu = raw_check_txframe(ro, skb, dev->mtu);
+ txmtu = raw_check_txframe(ro, skb, READ_ONCE(dev->mtu));
if (!txmtu)
goto free_skb;
--
2.49.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 2/4] can: dev: turn can_set_static_ctrlmode() into a non-inline function
2025-09-23 6:37 [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 1/4] can: annotate mtu accesses with READ_ONCE() Vincent Mailhol
@ 2025-09-23 6:37 ` Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 3/4] can: populate the minimum and maximum MTU values Vincent Mailhol
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Vincent Mailhol @ 2025-09-23 6:37 UTC (permalink / raw)
To: Oliver Hartkopp, Marc Kleine-Budde
Cc: linux-can, linux-kernel, Vincent Mailhol
can_set_static_ctrlmode() is declared as a static inline. But it is
only called in the probe function of the devices and so does not
really benefit from any kind of optimization.
Transform it into a "normal" function by moving it to
drivers/net/can/dev/dev.c
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
This also serves as a preparation for the next patch in which we are
adding can_set_default_mtu(). That function will only be used by the
can_dev.ko module and so we do not need to export its GPL
symbol. However, if can_set_static_ctrlmode() stays as a static
inline, then the call to set_default_mtu(), which we plan to add in
can_set_static_ctrlmode(), would also be inlined and thus would become
visible to the users of can_set_static_ctrlmode().
Making can_set_static_ctrlmode() a non-inline function resolves this
dependency.
---
drivers/net/can/dev/dev.c | 21 +++++++++++++++++++++
include/linux/can/dev.h | 23 ++---------------------
2 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 3913971125de0ab16b4ad9f36712954141014ddf..a0ae659beedcd1fa0979662c12614ae7846032c0 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -347,6 +347,27 @@ int can_change_mtu(struct net_device *dev, int new_mtu)
}
EXPORT_SYMBOL_GPL(can_change_mtu);
+/* helper to define static CAN controller features at device creation time */
+int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
+{
+ struct can_priv *priv = netdev_priv(dev);
+
+ /* alloc_candev() succeeded => netdev_priv() is valid at this point */
+ if (priv->ctrlmode_supported & static_mode) {
+ netdev_warn(dev,
+ "Controller features can not be supported and static at the same time\n");
+ return -EINVAL;
+ }
+ priv->ctrlmode = static_mode;
+
+ /* override MTU which was set by default in can_setup()? */
+ if (static_mode & CAN_CTRLMODE_FD)
+ dev->mtu = CANFD_MTU;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(can_set_static_ctrlmode);
+
/* generic implementation of netdev_ops::ndo_eth_ioctl for CAN devices
* supporting hardware timestamps
*/
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 9a92cbe5b2cb7ccdfca3121718856d096e9ecfa6..5dc58360c2d74a1711d4e02d28fe52ae20b146e0 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -125,27 +125,6 @@ static inline s32 can_get_relative_tdco(const struct can_priv *priv)
return (s32)priv->fd.tdc.tdco - sample_point_in_tc;
}
-/* helper to define static CAN controller features at device creation time */
-static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
- u32 static_mode)
-{
- struct can_priv *priv = netdev_priv(dev);
-
- /* alloc_candev() succeeded => netdev_priv() is valid at this point */
- if (priv->ctrlmode_supported & static_mode) {
- netdev_warn(dev,
- "Controller features can not be supported and static at the same time\n");
- return -EINVAL;
- }
- priv->ctrlmode = static_mode;
-
- /* override MTU which was set by default in can_setup()? */
- if (static_mode & CAN_CTRLMODE_FD)
- dev->mtu = CANFD_MTU;
-
- return 0;
-}
-
static inline u32 can_get_static_ctrlmode(struct can_priv *priv)
{
return priv->ctrlmode & ~priv->ctrlmode_supported;
@@ -188,6 +167,8 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
int open_candev(struct net_device *dev);
void close_candev(struct net_device *dev);
int can_change_mtu(struct net_device *dev, int new_mtu);
+int __must_check can_set_static_ctrlmode(struct net_device *dev,
+ u32 static_mode);
int can_eth_ioctl_hwts(struct net_device *netdev, struct ifreq *ifr, int cmd);
int can_ethtool_op_get_ts_info_hwts(struct net_device *dev,
struct kernel_ethtool_ts_info *info);
--
2.49.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 3/4] can: populate the minimum and maximum MTU values
2025-09-23 6:37 [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 1/4] can: annotate mtu accesses with READ_ONCE() Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 2/4] can: dev: turn can_set_static_ctrlmode() into a non-inline function Vincent Mailhol
@ 2025-09-23 6:37 ` Vincent Mailhol
2025-09-23 6:37 ` [PATCH v3 4/4] can: enable CAN XL for virtual CAN devices by default Vincent Mailhol
2025-09-23 9:10 ` [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Marc Kleine-Budde
4 siblings, 0 replies; 6+ messages in thread
From: Vincent Mailhol @ 2025-09-23 6:37 UTC (permalink / raw)
To: Oliver Hartkopp, Marc Kleine-Budde
Cc: linux-can, linux-kernel, Vincent Mailhol
By populating:
net_device->min_mtu
and
net_device->max_mtu
the net core infrastructure will automatically:
1. validate that the user's inputs are in range.
2. report those min and max MTU values through the netlink
interface.
Add can_set_default_mtu() which sets the default mtu value as well as
the minimum and maximum values. The logic for the default mtu value
remains unchanged:
- CANFD_MTU if the device has a static CAN_CTRLMODE_FD.
- CAN_MTU otherwise.
Call can_set_default_mtu() each time the CAN_CTRLMODE_FD is modified.
This will guarantee that the MTU value is always consistent with the
control mode flags.
With this, the checks done in can_change_mtu() become fully redundant
and will be removed in an upcoming change and it is now possible to
confirm the minimum and maximum MTU values on a physical CAN interface
by doing:
$ ip --details link show can0
The virtual interfaces (vcan and vxcan) are not impacted by this
change.
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
With this, when adding the CAN XL netlink interface, all we have to do
is to add one if branch to can_set_default_mtu() like this:
void can_set_default_mtu(struct net_device *dev)
{
struct can_priv *priv = netdev_priv(dev);
if (priv->ctrlmode & CAN_CTRLMODE_XL) {
if (can_is_canxl_dev_mtu(dev->mtu))
return;
dev->mtu = CANXL_MTU;
dev->min_mtu = CANXL_MIN_MTU;
dev->max_mtu = CANXL_MAX_MTU;
} else if (priv->ctrlmode & CAN_CTRLMODE_FD) {
dev->mtu = CANFD_MTU;
dev->min_mtu = CANFD_MTU;
dev->max_mtu = CANFD_MTU;
} else {
dev->mtu = CAN_MTU;
dev->min_mtu = CAN_MTU;
dev->max_mtu = CAN_MTU;
}
}
and we will be done!
---
drivers/net/can/dev/dev.c | 21 ++++++++++++++++++---
drivers/net/can/dev/netlink.c | 9 ++++-----
include/linux/can/dev.h | 1 +
3 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index a0ae659beedcd1fa0979662c12614ae7846032c0..69c00720e9956cad986ea86fbba49285d76f51db 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -239,11 +239,12 @@ EXPORT_SYMBOL_GPL(can_bus_off);
void can_setup(struct net_device *dev)
{
dev->type = ARPHRD_CAN;
- dev->mtu = CAN_MTU;
dev->hard_header_len = 0;
dev->addr_len = 0;
dev->tx_queue_len = 10;
+ can_set_default_mtu(dev);
+
/* New-style flags. */
dev->flags = IFF_NOARP;
dev->features = NETIF_F_HW_CSUM;
@@ -309,6 +310,21 @@ void free_candev(struct net_device *dev)
}
EXPORT_SYMBOL_GPL(free_candev);
+void can_set_default_mtu(struct net_device *dev)
+{
+ struct can_priv *priv = netdev_priv(dev);
+
+ if (priv->ctrlmode & CAN_CTRLMODE_FD) {
+ dev->mtu = CANFD_MTU;
+ dev->min_mtu = CANFD_MTU;
+ dev->max_mtu = CANFD_MTU;
+ } else {
+ dev->mtu = CAN_MTU;
+ dev->min_mtu = CAN_MTU;
+ dev->max_mtu = CAN_MTU;
+ }
+}
+
/* changing MTU and control mode for CAN/CANFD devices */
int can_change_mtu(struct net_device *dev, int new_mtu)
{
@@ -361,8 +377,7 @@ int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
priv->ctrlmode = static_mode;
/* override MTU which was set by default in can_setup()? */
- if (static_mode & CAN_CTRLMODE_FD)
- dev->mtu = CANFD_MTU;
+ can_set_default_mtu(dev);
return 0;
}
diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c
index d9f6ab3efb9767409c318b714f19df8a30e51137..248f607e3864ffbda6f0b8daf4e2484179cf9cd5 100644
--- a/drivers/net/can/dev/netlink.c
+++ b/drivers/net/can/dev/netlink.c
@@ -223,17 +223,16 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
priv->ctrlmode &= ~cm->mask;
priv->ctrlmode |= maskedflags;
- /* CAN_CTRLMODE_FD can only be set when driver supports FD */
- if (priv->ctrlmode & CAN_CTRLMODE_FD) {
- dev->mtu = CANFD_MTU;
- } else {
- dev->mtu = CAN_MTU;
+ /* Wipe potential leftovers from previous CAN FD config */
+ if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {
memset(&priv->fd.data_bittiming, 0,
sizeof(priv->fd.data_bittiming));
priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;
memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc));
}
+ can_set_default_mtu(dev);
+
fd_tdc_flag_provided = cm->mask & CAN_CTRLMODE_FD_TDC_MASK;
/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
* exclusive: make sure to turn the other one off
diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
index 5dc58360c2d74a1711d4e02d28fe52ae20b146e0..3354f70ed2c684d7d482549560d4cb5838cbebd5 100644
--- a/include/linux/can/dev.h
+++ b/include/linux/can/dev.h
@@ -166,6 +166,7 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
int open_candev(struct net_device *dev);
void close_candev(struct net_device *dev);
+void can_set_default_mtu(struct net_device *dev);
int can_change_mtu(struct net_device *dev, int new_mtu);
int __must_check can_set_static_ctrlmode(struct net_device *dev,
u32 static_mode);
--
2.49.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 4/4] can: enable CAN XL for virtual CAN devices by default
2025-09-23 6:37 [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Vincent Mailhol
` (2 preceding siblings ...)
2025-09-23 6:37 ` [PATCH v3 3/4] can: populate the minimum and maximum MTU values Vincent Mailhol
@ 2025-09-23 6:37 ` Vincent Mailhol
2025-09-23 9:10 ` [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Marc Kleine-Budde
4 siblings, 0 replies; 6+ messages in thread
From: Vincent Mailhol @ 2025-09-23 6:37 UTC (permalink / raw)
To: Oliver Hartkopp, Marc Kleine-Budde
Cc: linux-can, linux-kernel, Vincent Mailhol
In commit 97edec3a11cf ("can: enable CAN FD for virtual CAN devices by
default"), vcan and vxcan default MTU was set to CANFD_MTU by default.
The reason was that users were confused on how to activate CAN FD on
virtual interfaces.
Following the introduction of CAN XL, the same logic should be
applied. Set the MTU to CANXL_MTU by default.
The users who really wish to use a Classical CAN only or a CAN FD
virtual device can do respectively:
$ ip link set vcan0 mtu 16
or
$ ip link set vcan0 mtu 72
to force the old behaviour.
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
drivers/net/can/vcan.c | 2 +-
drivers/net/can/vxcan.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/net/can/vcan.c b/drivers/net/can/vcan.c
index f67e858071007acd5f34fa00a76212f1a77997a6..fdc662aea2798125b3aa373f09958363b427ced2 100644
--- a/drivers/net/can/vcan.c
+++ b/drivers/net/can/vcan.c
@@ -156,7 +156,7 @@ static const struct ethtool_ops vcan_ethtool_ops = {
static void vcan_setup(struct net_device *dev)
{
dev->type = ARPHRD_CAN;
- dev->mtu = CANFD_MTU;
+ dev->mtu = CANXL_MTU;
dev->hard_header_len = 0;
dev->addr_len = 0;
dev->tx_queue_len = 0;
diff --git a/drivers/net/can/vxcan.c b/drivers/net/can/vxcan.c
index 99a78a75716749bf858cc78eadb41ca2588fcf94..b2c19f8c5f8e5101b8be343401afe9a4f388c4da 100644
--- a/drivers/net/can/vxcan.c
+++ b/drivers/net/can/vxcan.c
@@ -156,7 +156,7 @@ static void vxcan_setup(struct net_device *dev)
struct can_ml_priv *can_ml;
dev->type = ARPHRD_CAN;
- dev->mtu = CANFD_MTU;
+ dev->mtu = CANXL_MTU;
dev->hard_header_len = 0;
dev->addr_len = 0;
dev->tx_queue_len = 0;
--
2.49.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3)
2025-09-23 6:37 [PATCH v3 0/4] can: rework the CAN MTU logic (CAN XL preparation step 2/3) Vincent Mailhol
` (3 preceding siblings ...)
2025-09-23 6:37 ` [PATCH v3 4/4] can: enable CAN XL for virtual CAN devices by default Vincent Mailhol
@ 2025-09-23 9:10 ` Marc Kleine-Budde
4 siblings, 0 replies; 6+ messages in thread
From: Marc Kleine-Budde @ 2025-09-23 9:10 UTC (permalink / raw)
To: Vincent Mailhol; +Cc: Oliver Hartkopp, linux-can, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 478 bytes --]
On 23.09.2025 15:37:07, Vincent Mailhol wrote:
> The CAN MTU logic is currently broken. can_change_mtu() will update
> both the MTU and the CAN_CTRLMODE_FD flag.
Added to linux-can-next.
Thanks,
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread