Skip to content

Commit 7d4a101

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: dev: add sanity check in can_set_static_ctrlmode()
Previous patch removed can_priv::ctrlmode_static to replace it with can_get_static_ctrlmode(). A condition sine qua non for this to work is that the controller static modes should never be set in can_priv::ctrlmode_supported (c.f. the comment on can_priv::ctrlmode_supported which states that it is for "options that can be *modified* by netlink"). Also, this condition is already correctly fulfilled by all existing drivers which rely on the ctrlmode_static feature. Nonetheless, we added an extra safeguard in can_set_static_ctrlmode() to return an error value and to warn the developer who would be adventurous enough to set to static a given feature that is already set to supported. The drivers which rely on the static controller mode are then updated to check the return value of can_set_static_ctrlmode(). Link: https://lore.kernel.org/all/[email protected] Signed-off-by: Vincent Mailhol <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent c9e1d8e commit 7d4a101

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

drivers/net/can/m_can/m_can.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ static bool m_can_niso_supported(struct m_can_classdev *cdev)
14561456
static int m_can_dev_setup(struct m_can_classdev *cdev)
14571457
{
14581458
struct net_device *dev = cdev->net;
1459-
int m_can_version;
1459+
int m_can_version, err;
14601460

14611461
m_can_version = m_can_check_core_release(cdev);
14621462
/* return if unsupported version */
@@ -1486,7 +1486,9 @@ static int m_can_dev_setup(struct m_can_classdev *cdev)
14861486
switch (cdev->version) {
14871487
case 30:
14881488
/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1489-
can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1489+
err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1490+
if (err)
1491+
return err;
14901492
cdev->can.bittiming_const = cdev->bit_timing ?
14911493
cdev->bit_timing : &m_can_bittiming_const_30X;
14921494

@@ -1496,7 +1498,9 @@ static int m_can_dev_setup(struct m_can_classdev *cdev)
14961498
break;
14971499
case 31:
14981500
/* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1499-
can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1501+
err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1502+
if (err)
1503+
return err;
15001504
cdev->can.bittiming_const = cdev->bit_timing ?
15011505
cdev->bit_timing : &m_can_bittiming_const_31X;
15021506

drivers/net/can/rcar/rcar_canfd.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,9 @@ static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch,
16991699
&rcar_canfd_data_bittiming_const;
17001700

17011701
/* Controller starts in CAN FD only mode */
1702-
can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
1702+
err = can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD);
1703+
if (err)
1704+
goto fail;
17031705
priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
17041706
} else {
17051707
/* Controller starts in Classical CAN only mode */

include/linux/can/dev.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,24 @@ static inline s32 can_get_relative_tdco(const struct can_priv *priv)
131131
}
132132

133133
/* helper to define static CAN controller features at device creation time */
134-
static inline void can_set_static_ctrlmode(struct net_device *dev,
135-
u32 static_mode)
134+
static inline int __must_check can_set_static_ctrlmode(struct net_device *dev,
135+
u32 static_mode)
136136
{
137137
struct can_priv *priv = netdev_priv(dev);
138138

139139
/* alloc_candev() succeeded => netdev_priv() is valid at this point */
140+
if (priv->ctrlmode_supported & static_mode) {
141+
netdev_warn(dev,
142+
"Controller features can not be supported and static at the same time\n");
143+
return -EINVAL;
144+
}
140145
priv->ctrlmode = static_mode;
141146

142147
/* override MTU which was set by default in can_setup()? */
143148
if (static_mode & CAN_CTRLMODE_FD)
144149
dev->mtu = CANFD_MTU;
150+
151+
return 0;
145152
}
146153

147154
static inline u32 can_get_static_ctrlmode(struct can_priv *priv)

0 commit comments

Comments
 (0)