can: rework the CAN MTU logic (CAN XL preparation step 2/3)
Subsystems (2)
Vincent Mailhol
maintainerv3 cover can: rework the CAN MTU logic (CAN XL preparation step 2/3)
The CAN MTU logic is currently broken. can_change_mtu() will update both the MTU and the CAN_CTRLMODE_FD flag.
Back then, commit bc05a8944a34 ("can: allow to change the device mtu for CAN FD capable devices") stated that:
The configuration can be done either with the 'fd { on | off }' option in the 'ip' tool from iproute2 or by setting the CAN netdevice MTU to CAN_MTU (16) or to CANFD_MTU (72).
The problem is that after doing for example:
$ ip link set can0 mtu 72 bittiming 500000
on a CAN FD interface, we are left with a device on which CAN FD is enabled but which does not have the FD databittiming parameters configured.
The same goes on when setting the mtu back to 16:
ip link set can0 type can bitrate 500000 fd on dbitrate 5000000 ip link set can0 mtu 16
The device is now in Classical CAN mode but iproute2 is still reporting the databittiming values (although this time, the issue seems less critical as it is only a reporting problem).
The only way to resolve the problem and bring the device back to a coherent state is to call again the netlink interface using the "fd on" or "fd off" options.
The idea of being able to infer the CAN_CTRLMODE_FD flag from the MTU value is just incorrect for physical devices. Note that this logic remains valid on virtual interfaces (vcan and vxcan) because those do not have control mode flags and thus no conflict occurs.
This series reworks the CAN MTU logic. The goal is to always maintain a coherent state between the MTU and the control mode flags as listed in below table:
fd off, xl off fd on, xl off fd any, xl on
---------------------------------------------------------------------------
default mtu CAN_MTU CANFD_MTU CANXL_MTU
min mtu CAN_MTU CANFD_MTU CANXL_MIN_MTU
max mtu CAN_MTU CANFD_MTU CANXL_MAX_MTU
In order to switch between one column to another, the user must use the fd/xl on/off flags. Directly modifying the MTU from one column to the other is not permitted any more.
The CAN XL is not yet supported at the moment, so the last column is just given as a reference to better understand what is coming up. This series will just implement the first two columns.
While doing the rewrite, the logic is adjusted to reuse as much as possible the net core infrastructure. 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.
Point 1. will allow us to get rid of the can_change_mtu() in a near future for all the physical devices and point 2. allows the end user to see the valid MTU range by doing a:
$ ip --details link show can0
Finally, because using the net core, it will be possible after the removal of can_change_mtu() to modify the MTU while the device is up. As stated previously, the only modifications allowed will be within the MTU range of a given CAN protocol. So for Classical CAN and CAN FD, the MTU is fixed to, respectively, CAN_MTU and CANFD_MTU. For the upcoming CAN XL, the user will be able to change the MTU to anything between CANXL_MIN_MTU and CANXL_MAX_MTU even if the device is up.
The first patch of this series annotates the read access on net_device->mtu. This preparation is needed to prevent any race condition to occur when modifying the MTU while the device is up.
The second patch is another preparation change which moves can_set_static_ctrlmode() from dev.h to dev.c.
The third patch populates the MTU minimum and maximum value.
The fourth patch is just a clean-up to remove the old can_change_mtu().
The fourth and last patch comes as a bonus content and modifies the default MTU of the vcan and vxcan so that CAN XL is on by default.
Note that after this series, the old can_change_mtu() becomes useless. That function can not yet be removed because some pending changes from other maintainers' trees still depend on it. It will be removed in the next development window once all those changes reach net-next.
---
Changes in v3:
- v2 was based on Linus tree and, as a result, my other series which depends on this one did not apply smoothly:
https://lore.kernel.org/linux-can/[email protected]/
Rebase on net-next.
Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Drop RFC prefix
- Patch "can: treewide: remove can_change_mtu()" is removed because it depends on the MTU fix:
https://lore.kernel.org/linux-can/[email protected]/
which at the time of writing, does not yet appear in net-next. It is also still used in the Nuvoton driver which is also not yet merged. This patch will be resent in the next development window.
Link to v1: https://lore.kernel.org/r/[email protected]
---
Vincent Mailhol (4):
can: annotate mtu accesses with READ_ONCE()
can: dev: turn can_set_static_ctrlmode() into a non-inline function
can: populate the minimum and maximum MTU values
can: enable CAN XL for virtual CAN devices by default
drivers/net/can/dev/dev.c | 38 +++++++++++++++++++++++++++++++++++++- drivers/net/can/dev/netlink.c | 9 ++++----- drivers/net/can/vcan.c | 2 +- drivers/net/can/vxcan.c | 2 +- include/linux/can/dev.h | 24 +++--------------------- net/can/af_can.c | 2 +- net/can/isotp.c | 2 +- net/can/raw.c | 2 +- 8 files changed, 49 insertions(+), 32 deletions(-)
---
base-commit: 9b277fca90c39c8b749e659bf5c23e924c46c93b
change-id: 20250915-can-fix-mtu-050a94b563a0
Best regards,
Signature · 1 line
Vincent Mailhol <[email protected]>
Vincent Mailhol
maintainerPATCH v3 1/4 can: annotate mtu accesses with READ_ONCE()
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.
--- net/can/af_can.c | 2 +- net/can/isotp.c | 2 +- net/can/raw.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
net/can/af_can.c +1 -1
@@ -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; }
net/can/isotp.c +1 -1
@@ -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;
net/can/raw.c +1 -1
@@ -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;
Signature · 1 line
2.49.1
Vincent Mailhol
maintainerPATCH v3 2/4 can: dev: turn can_set_static_ctrlmode() into a non-inline function
In reply to Vincent Mailholcan_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
---
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(-)
drivers/net/can/dev/dev.c +21 -0
@@ -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 */
include/linux/can/dev.h +2 -21
@@ -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);
Signature · 1 line
2.49.1
Vincent Mailhol
maintainerPATCH v3 3/4 can: populate the minimum and maximum MTU values
In reply to Vincent MailholBy 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.
---
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(-)
drivers/net/can/dev/dev.c +18 -3
@@ -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 ; }
drivers/net/can/dev/netlink.c +4 -5
@@ -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
include/linux/can/dev.h +1 -0
@@ -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);
Signature · 1 line
2.49.1
Vincent Mailhol
maintainerPATCH v3 4/4 can: enable CAN XL for virtual CAN devices by default
In reply to Vincent MailholIn 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.
--- drivers/net/can/vcan.c | 2 +- drivers/net/can/vxcan.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/can/vcan.c +1 -1
@@ -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 ;
drivers/net/can/vxcan.c +1 -1
@@ -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 ;
Signature · 1 line
2.49.1
Marc Kleine-Budde
maintainerQuoted 2 lines · Vincent Mailhol
The CAN MTU logic is currently broken.
can_change_mtu()will update both the MTU and theCAN_CTRLMODE_FDflag.
Added to linux-can-next.
Thanks,
Marc
Signature · 4 lines
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 |